<?php

namespace App\Core\Service;

use App\Core\Repository\PropertyWebAboutUs\PropertyWebAboutUsRepository;

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

class PropertyWebAboutUsService
{

    private $propertyWebAboutUsRepository;
    private $languageService;



    public function __construct(
        PropertyWebAboutUsRepository $propertyWebAboutUsRepository,
        LanguageService $languageService
    )
    {

        $this->propertyWebAboutUsRepository = $propertyWebAboutUsRepository;
        $this->languageService = $languageService;

    }

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

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

        try {
            $data = $this->propertyWebAboutUsRepository->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 create($params = [])
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];

        try {

            $propertyCreateResult = $this->propertyWebAboutUsRepository->insert($params);

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

            $response = [
                'status' => true,
                'data' => $propertyCreateResult["data"]
            ];
        } 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 getPropertyWebAboutUs($param = [], $column = ['*']){

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

            $return = [];

            $getApplicationLanguages = $this->languageService->getApplicationLanguages();
            if($getApplicationLanguages['status'] != 'success'){
                throw new ApiErrorException($getApplicationLanguages['message']);
            }
            $applicationLanguages = $getApplicationLanguages['data'] ;

            $requestPropertyWebAboutUs = [
                'criteria' => [
                    ['field' => 'property_id', 'condition' => '=', 'value' => fillOnUndefined($param, 'property_id')],
                    ['field' => 'property_web_id', 'condition' => '=', 'value' => fillOnUndefined($param, 'property_web_id')],
                    ['field' => 'status', 'condition' => '=', 'value' => fillOnUndefined($param, 'mode')]
                ]
            ];

            $propertyWebAboutUs = $this->select($requestPropertyWebAboutUs, $column);

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

            $propertyWebAboutUs = !empty($propertyWebAboutUs['data']) ? collect($propertyWebAboutUs['data'])->keyBy('language_code')->toArray() : [];
            foreach ($applicationLanguages as $applicationLanguage) {
                $langKey = $applicationLanguage['code'] ;
                $responseAboutUs[] = [
                    'language_code' => $langKey,
                    'value' => isset($propertyWebAboutUs[$langKey]) ? $propertyWebAboutUs[$langKey]['value'] : null
                ];
            }

            $return = $responseAboutUs;

            $response = ['statusCode' => 200, 'status' => true, 'message' => '', 'data' => $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 updatePropertyWebAboutUs($params){

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

        try {

            $return  = [];


            $getDeletePropertyWebAboutRequest =  [
                '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"){
                $getDeletePropertyWebAboutRequest['criteria'][] = ['field' => 'status', 'condition' => '=', 'value' => 2];
                $status = 2;
            }

            $deleteWebAboutUsId = $this->propertyWebAboutUsRepository->findByCriteria($getDeletePropertyWebAboutRequest, ['id']) ;
            $deleteWebAboutUsId = array_column($deleteWebAboutUsId, 'id') ;

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

            $createPropertyWebAboutUsData = [];
            $aboutUsDatas = isset($params['about_us']) && !empty($params['about_us']) ? $params['about_us'] : [];
            foreach ($aboutUsDatas as $key => $aboutUsData){
                if($aboutUsData['value']){
                    $createPropertyWebAboutUsData[$key]['property_id'] = fillOnUndefined($params,'property_id');
                    $createPropertyWebAboutUsData[$key]['property_web_id'] = fillOnUndefined($params,'property_web_id');
                    $createPropertyWebAboutUsData[$key]['language_code'] = $aboutUsData['language_code'];
                    $createPropertyWebAboutUsData[$key]['value'] = $aboutUsData['value'];
                    $createPropertyWebAboutUsData[$key]['status'] = $status;
                    $createPropertyWebAboutUsData[$key]['created_by'] = fillOnUndefined($params, 'user_id');
                    $createPropertyWebAboutUsData[$key]['updated_by'] = fillOnUndefined($params, 'user_id');
                    $createPropertyWebAboutUsData[$key]['created_at'] = time();
                    $createPropertyWebAboutUsData[$key]['updated_at'] = time();
                }
            }

            $propertyWebAboutUsResult = $this->create($createPropertyWebAboutUsData);
            if ($propertyWebAboutUsResult['status'] != 'success'){
                throw new ApiErrorException($propertyWebAboutUsResult['message']);
            }

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

    }


}
