<?php

namespace App\Core\Service;

use App\Core\Repository\PropertyWebPlaceMapping\PropertyWebPlaceMappingRepository;

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

class PropertyWebPlaceMappingService
{

    private $propertyWebPlaceMappingRepository;


    public function __construct(
        PropertyWebPlaceMappingRepository $propertyWebPlaceMappingRepository
    )
    {
        $this->propertyWebPlaceMappingRepository = $propertyWebPlaceMappingRepository;
    }

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

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

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

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

            $propertyWebPlaceMappingResult = $this->propertyWebPlaceMappingRepository->insert($param);

            if ($propertyWebPlaceMappingResult['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 getPropertyWebRoomMapping($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_room_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 updatePropertyWebPlaceMapping($params = []){

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

            $return  = [];

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

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


            $deleteWebPlaceId = $this->propertyWebPlaceMappingRepository->findByCriteria($getDeletePropertyWebPlaceRequest, ['id']) ;
            $deleteWebPlaceId = array_column($deleteWebPlaceId, 'id') ;


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

            $myWebPlaces = !empty($params['places']) ? $params['places'] : [];

            $createPropertyWebPlaceMappingData = [];
            foreach ($myWebPlaces as $key => $myWebPlace){
                $createPropertyWebPlaceMappingData[$key]['property_id'] = fillOnUndefined($params,'property_id');
                $createPropertyWebPlaceMappingData[$key]['property_web_id'] = fillOnUndefined($params,'property_web_id');
                $createPropertyWebPlaceMappingData[$key]['property_place_id'] = $myWebPlace;
                $createPropertyWebPlaceMappingData[$key]['status'] = $status;
                $createPropertyWebPlaceMappingData[$key]['created_by'] = fillOnUndefined($params, 'user_id');
                $createPropertyWebPlaceMappingData[$key]['updated_by'] = fillOnUndefined($params, 'user_id');
                $createPropertyWebPlaceMappingData[$key]['created_at'] = time();
                $createPropertyWebPlaceMappingData[$key]['updated_at'] = time();
            }

            $propertyWebPlaceMappingResult = $this->createPropertyWebPlaceMapping($createPropertyWebPlaceMappingData);
            if ($propertyWebPlaceMappingResult['status'] != 'success'){
                throw new ApiErrorException($propertyWebPlaceMappingResult['message']);
            }

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

}
