<?php

namespace App\Core\Service;

use App\Core\Repository\PropertyWebSetup\PropertyWebSetupRepository;

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

class PropertyWebSetupService
{

    private $propertyWebSetupRepository;



    public function __construct(
        PropertyWebSetupRepository $propertyWebSetupRepository
    )
    {

        $this->propertyWebSetupRepository = $propertyWebSetupRepository;

    }

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

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

        try {
            $data = $this->propertyWebSetupRepository->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 getPropertyWebSetup($params = []){

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

            $return = [];
            $searchCriteria = [
                'criteria' => [
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                    ['field' => 'property_id', 'condition' => '=', 'value' => fillOnUndefined($params, 'property_id')],
                    ['field' => 'property_web_id', 'condition' => '=', 'value' => fillOnUndefined($params, 'property_web_id')],
                ],
                'firstRow' => true
            ];

            $searchResult = $this->select($searchCriteria, ['*']);

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

            $return = $searchResult['data'];

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

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

            DB::beginTransaction();

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

            $deleteWebSetupId = $this->propertyWebSetupRepository->findByCriteria($getDeletePropertyWebSetupRequest, ['id']) ;
            $deleteWebSetupId = array_column($deleteWebSetupId, 'id') ;

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

            $webSetupInsertData = [
                "property_id" => fillOnUndefined($params, 'property_id'),
                "property_web_id" => fillOnUndefined($params, 'property_web_id'),
                "languages" => empty($params['languages']) ? NULL : json_encode($params['languages']),
                "color" => empty($params['colors']) ? NULL : json_encode($params['colors']),
                "status" => $status,
                "created_by" => fillOnUndefined($params, 'user_id'),
                "updated_by" => fillOnUndefined($params, 'user_id'),
                "created_at" => time(),
                "updated_at" => time()
            ];

            $propertyWebSetup = $this->propertyWebSetupRepository->insert($webSetupInsertData) ;
            if ($propertyWebSetup['status'] != 'success') {
                throw new Exception('api-unknown_error');
            }

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

        return output($response);

    }




}
