<?php

namespace App\Core\Service;

use App\Core\Repository\PropertyChannel\PropertyChannelContactRepository;
use App\Core\Repository\PropertyChannelMapping\PropertyChannelMappingRepository ;


use App;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Config;
use Exception;
use App\Exceptions\ApiErrorException;

class PropertyChannelContactService
{

    private $propertyChannelContactRepository;
    private $propertyChannelMappingRepository;


    public function __construct(

        PropertyChannelContactRepository $propertyChannelContactRepository,
        PropertyChannelMappingRepository $propertyChannelMappingRepository

    )
    {

        $this->propertyChannelContactRepository = $propertyChannelContactRepository;
        $this->propertyChannelMappingRepository = $propertyChannelMappingRepository;


    }

    public function create($params = [])
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {

            $insertData =
                [
                    'property_channel_mapping_id' => fillOnUndefined($params, 'property_channel_mapping_id'),
                    'name' => fillOnUndefined($params, 'name'),
                    'email' => fillOnUndefined($params, 'email'),
                    "status" => fillOnUndefined($params, "status", 1),
                    "created_by" => fillOnUndefined($params, "user_id"),
                    "updated_by" => fillOnUndefined($params, "user_id"),
                    "created_at" => time(),
                    "updated_at" => time(),

                ];

            $userCreateResult = $this->propertyChannelContactRepository->create($insertData);

            if ($userCreateResult['status'] != 'success') {
                throw new Exception('This e-mail address already in list');
            }
            $userData = $userCreateResult["data"];
            $response = [
                'status' => true,
                'data' => $userData,
            ];

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function select($param = [], $column = ['*'])
    {

        $response = ['status' => -1, 'message' => '', 'data' => null];

        try {

            $data = $this->propertyChannelContactRepository->findByCriteria($param, $column);

            $response = [
                'status' => true,
                'data' => $data,
            ];

        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function update($id, $param = [])
    {


        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {


            $updateResult = $this->propertyChannelContactRepository->update($id, $param);
            if ($updateResult['status'] != 'success') {
                throw new Exception('api-unknown_error');
            }
            $updateData = $updateResult["data"];
            $response = [
                'status' => true,
                'data' => $updateData,
            ];

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function add($params = [])
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {

            $insertData =
                [
                    'property_channel_mapping_id' => fillOnUndefined($params, 'property_channel_mapping_id'),
                    'name' => fillOnUndefined($params, 'name'),
                    'email' => fillOnUndefined($params, 'email'),
                    "status" => fillOnUndefined($params, "status", 1),
                    "user_id" => fillOnUndefined($params, "user_id"),
                ];


            $userCreateResult = $this->create($insertData);
            if ($userCreateResult['status'] != 'success') {
                throw new ApiErrorException($userCreateResult['message']);
            }
            $userData = $userCreateResult["data"];
            $response = [
                'status' => true,
                'data' => $userData,
            ];

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function getContact($params = [])
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {

            if(!isset($params['id'])){
                $criteria = [
                    'criteria' => [
                        ['field' => 'status', 'condition' => '=', 'value' => 1],
                        ['field' => 'property_channel_mapping_id', 'condition' => '=', 'value' => $params['property_channel_mapping_id']]
                    ],
                    'orderBy' => [
                        ['field' => 'name', 'value' => 'ASC']
                    ]
                ];

                $contactData = $this->propertyChannelContactRepository->findByCriteria($criteria);
                $contactData = $contactData ? $contactData : [] ;
            }
            else{
                $criteria = [
                    'criteria' => [
                        ['field' => 'status', 'condition' => '=', 'value' => 1],
                        ['field' => 'id', 'condition' => '=', 'value' => $params['id']],
                        ['field' => 'property_channel_mapping_id', 'condition' => '=', 'value' => $params['property_channel_mapping_id']]
                    ],
                    'firstRow' => true
                ];

                $contactData = $this->propertyChannelContactRepository->findByCriteria($criteria);
                if (!$contactData) {
                    throw new ApiErrorException('Unvalid Contact');
                }
            }


            $response = [
                'status' => true,
                'data' => $contactData,
            ];


        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function delete($params){

        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {


            $request = [
                'criteria' => [
                    ['field' => 'property_channel_mapping_id', 'condition' => '=', 'value' => $params['property_channel_mapping_id']],
                    ['field' => 'id', 'condition' => '=', 'value' => $params['id']],
                ]
            ];


            $response = $this->propertyChannelContactRepository->delete($request);
            if(!$response){
                throw new ApiErrorException('No record to delete');

            }

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $response['data']];

        } catch
        (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
            $response['statusCode'] = 400;
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
            $response['statusCode'] = 500;
        }

        return output($response);
    }


}
