<?php

namespace App\Core\Service;

use App\Core\Repository\PropertyWebLanguageMapping\PropertyWebLanguageMappingRepository;

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

class PropertyWebLanguageMappingService
{

    private $propertyWebLanguageMappingRepository;
    private $languageService;


    public function __construct(
        PropertyWebLanguageMappingRepository $propertyWebLanguageMappingRepository,
        LanguageService $languageService
    )
    {
        $this->propertyWebLanguageMappingRepository = $propertyWebLanguageMappingRepository;
        $this->languageService = $languageService;
    }

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

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

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

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

            $propertyWebMenuMappingResult = $this->propertyWebLanguageMappingRepository->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 getPropertyWebLanguageMapping($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_language_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 updatePropertyWebLanguageMapping($params = []){

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

            $return  = [];

            $getDeletePropertyWebLanguageRequest =  [
                '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"){

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

            $deleteWebLanguageId = $this->propertyWebLanguageMappingRepository->findByCriteria($getDeletePropertyWebLanguageRequest, ['id']) ;
            $deleteWebLanguageId = array_column($deleteWebLanguageId, 'id') ;

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


            $myWebLanguages = !empty($params['languages']) ? $params['languages'] : [];

            $createPropertyWebLanguageMappingData = [];
            foreach ($myWebLanguages as $key => $myWebLanguage){
                $createPropertyWebLanguageMappingData[$key]['property_id'] = fillOnUndefined($params,'property_id');
                $createPropertyWebLanguageMappingData[$key]['property_web_id'] = fillOnUndefined($params,'property_web_id');
                $createPropertyWebLanguageMappingData[$key]['language_code'] = $myWebLanguage;
                $createPropertyWebLanguageMappingData[$key]['status'] = $status;
                $createPropertyWebLanguageMappingData[$key]['created_by'] = fillOnUndefined($params, 'user_id');
                $createPropertyWebLanguageMappingData[$key]['updated_by'] = fillOnUndefined($params, 'user_id');
                $createPropertyWebLanguageMappingData[$key]['created_at'] = time();
                $createPropertyWebLanguageMappingData[$key]['updated_at'] = time();
            }


            $propertyWebLanguageMappingResult = $this->createPropertyWebLanguageMapping($createPropertyWebLanguageMappingData);
            if ($propertyWebLanguageMappingResult['status'] != 'success'){
                throw new ApiErrorException($propertyWebLanguageMappingResult['message']);
            }

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

}
