<?php

namespace App\Core\Service;

use App\Core\Repository\PropertyWebColorMapping\PropertyWebColorMappingRepository;

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

class PropertyWebColorMappingService
{

    private $propertyWebColorMappingRepository;

    public function __construct(
        PropertyWebColorMappingRepository $propertyWebColorMappingRepository
    )
    {
        $this->propertyWebColorMappingRepository = $propertyWebColorMappingRepository;
    }

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

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

        try {
            $data = $this->propertyWebColorMappingRepository->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 createPropertyWebColorMapping($param = []){

        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {
            // TODO validation yazılmalı!

            $propertyWebMenuMappingResult = $this->propertyWebColorMappingRepository->insert($param);

            if ($propertyWebMenuMappingResult['status'] != 'success') {
                throw new Exception('api-unknown_error');
            }

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

        } 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 getPropertyWebColorMapping($params, $select = ['*']){

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

            $return = [];
            $searchCriteria = [
                'criteria' => [
                    ['field' => 'property_id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'property_web_id', 'condition' => '=', 'value' => $params['property_web_id']],
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                ]
            ];

            $searchResult = $this->select($searchCriteria, $select);

            if($searchResult['status'] != 'success'){
                throw new ApiErrorException($searchResult['message']);
            }

            $return = $searchResult['data'];

            $response = ['statusCode' => 200, 'status' => true, 'message' => '', 'data' => ['property_web_color_mapping' => $return ] ];
        } 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);

    }

    public function updatePropertyWebColorMapping($params = []){

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

            $return  = [];

            $getDeletePropertyWebColorRequest =  [
                'criteria' => [
                    ['field' => 'property_id', 'condition' => '=', 'value' => fillOnUndefined($params, 'property_id')],
                    ['field' => 'property_web_id', 'condition' => '=', 'value' => fillOnUndefined($params, 'property_web_id')]
                ]
            ];

            $status = null;
            if($params['action'] === "publish"){
                $status = 1;
            }else if($params['action'] === "preview"){

                $getDeletePropertyWebColorRequest['criteria'][] = ['field' => 'status', 'condition' => '=', 'value' => 2];
                $status = 2;
            }

            $deleteWebColorId = $this->propertyWebColorMappingRepository->findByCriteria($getDeletePropertyWebColorRequest, ['id']) ;
            $deleteWebColorId = array_column($deleteWebColorId, 'id') ;

            if (!empty($deleteWebColorId)) {
                $destroyStatus = $this->propertyWebColorMappingRepository->destroy($deleteWebColorId);
                if ($destroyStatus['status'] != 'success') {
                    throw new Exception('api-unknown_error');
                }
            }

            $myWebColors = !empty($params['colors']) ? $params['colors'] : [];

            $createPropertyWebColorMappingData = [];
            foreach ($myWebColors as $key => $myWebColor){
                $createPropertyWebColorMappingData[$key]['property_id'] = fillOnUndefined($params,'property_id');
                $createPropertyWebColorMappingData[$key]['property_web_id'] = fillOnUndefined($params,'property_web_id');
                $createPropertyWebColorMappingData[$key]['color_code'] = $myWebColor['color_code'];
                $createPropertyWebColorMappingData[$key]['order_number'] = $myWebColor['color_number'];
                $createPropertyWebColorMappingData[$key]['status'] = $status;
                $createPropertyWebColorMappingData[$key]['created_by'] = fillOnUndefined($params, 'user_id');
                $createPropertyWebColorMappingData[$key]['updated_by'] = fillOnUndefined($params, 'user_id');
                $createPropertyWebColorMappingData[$key]['created_at'] = time();
                $createPropertyWebColorMappingData[$key]['updated_at'] = time();
            }

            $propertyWebColorMappingResult = $this->createPropertyWebColorMapping($createPropertyWebColorMappingData);
            if ($propertyWebColorMappingResult['status'] != 'success'){
                throw new ApiErrorException($propertyWebColorMappingResult['message']);
            }

            $response = ['statusCode' => 200, 'status' => true, 'message' => '', 'data' => ['property_web_menu_mapping' => $return ] ];

        } 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);
    }

    public function getPropertyWebColorMappingWithStatus($params, $select = ['*']){

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

            $return = [];
            $searchCriteria = [
                'criteria' => [
                    ['field' => 'property_id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'property_web_id', 'condition' => '=', 'value' => $params['property_web_id']],
                    ['field' => 'status', 'condition' => '=', 'value' => $params['status']]
                ]
            ];

            $searchResult = $this->select($searchCriteria, $select);

            if($searchResult['status'] != 'success'){
                throw new ApiErrorException($searchResult['message']);
            }

            $return = $searchResult['data'];

            $response = ['statusCode' => 200, 'status' => true, 'message' => '', 'data' => ['property_web_color_mapping' => $return ] ];
        } 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);

    }

}
