<?php


namespace App\Http\Controllers\V1;

use App\Core\Service\PropertyPhotoService\PropertyPhotoService;
use App\Core\Service\PropertyPlaceService;
use App\Exceptions\ApiErrorException;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request ;
use Exception ;

class PropertyPlaceController
{

    private $request ;
    private $propertyPlaceService;
    private $propertyPhotoService;


    public function __construct(
        Request $request,
        PropertyPhotoService $propertyPhotoService,
        PropertyPlaceService $propertyPlaceService
    )
    {
        $this->request = $request ;
        $this->propertyPlaceService = $propertyPlaceService ;
        $this->propertyPhotoService = $propertyPhotoService ;


    }


    public function getPlaceCategories(){
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $this->request->params;
            $responseData = null ;
            $placeCategories = $this->propertyPlaceService->getPropertyPlaceCategories($params);

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

            $responseData['place_categories'] = $placeCategories['data'] ;

            $placeWorkingHours = $this->propertyPlaceService->getPropertyWorkingHours($params);
            if($placeWorkingHours['status'] != 'success'){
                throw new Exception($placeWorkingHours['message']);
            }
            $responseData['place_working_hours'] = $placeWorkingHours['data'] ;

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $responseData];

        } 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 apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

    public function getPlaceCategoriesSingle(){
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $this->request->params;
            $responseData = null ;
            $placeCategories = $this->propertyPlaceService->getPropertyPlaceCategoriesSingle($params);

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

            $responseData['place_categories'] = $placeCategories['data'] ;

            $placeWorkingHours = $this->propertyPlaceService->getPropertyWorkingHours($params);
            if($placeWorkingHours['status'] != 'success'){
                throw new Exception($placeWorkingHours['message']);
            }
            $responseData['place_working_hours'] = $placeWorkingHours['data'] ;


            $placeIncludes = $this->propertyPlaceService->getPlaceIncludes($params);
            if($placeIncludes['status'] != 'success'){
                throw new Exception($placeIncludes['message']);
            }
            $responseData['place_includes'] = $placeIncludes['data'] ;

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $responseData];

        } 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 apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

    public function getPlacePlaceWorkingHours(){
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $this->request->params;
            $responseData = null ;
            $placeWorkingHours = $this->propertyPlaceService->getPropertyWorkingHours($params);

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

            $responseData['place_working_hours'] = $placeWorkingHours['data'] ;

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $responseData];

        } 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 apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

    public function createPlace(){
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $this->request->params;
            $params['user_id'] = $this->request->auth->id;

            $responseData = null ;
            $placeWorkingHours = $this->propertyPlaceService->create($params);

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

            $responseData['property_place'] = $placeWorkingHours['data'] ;

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $responseData];

        } 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 apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

    public function listPlace(){
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $this->request->params;
            $responseData = null ;
            $listPlaces = $this->propertyPlaceService->listPlaces($params);

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

            $responseData['places'] = $listPlaces['data'] ;

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $responseData];

        } 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 apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

    public function getPropertyPlacePhoto(Request $request)
    {

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

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }

            $params = $this->request->params;

            $requestFact = [
                'locale' => fillOnUndefined($params, 'locale'),
                'property_id' => fillOnUndefined($params, 'property_id'),
                'place_id' => fillOnUndefined($params, 'place_id'),
            ];

            $getPropertyPhoto = $this->propertyPhotoService->getPropertyPlacePhoto($requestFact);
            if ($getPropertyPhoto['status'] != 'success') {
                throw new ApiErrorException($getPropertyPhoto['message']);
            }

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $getPropertyPhoto['data'] ];

        } 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 apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);


    }

    public function updatePropertyPlacePhotoMapping(Request $request)
    {
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];

        try
        {
            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = json_decode($this->request->getContent(),1);
            $params = current($params);
            $params['user_id'] =  $this->request->auth->id;

            $response =  $this->propertyPlaceService->updatePropertyPlacePhotoMapping($params);

        }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 apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

    public function updatePlace(){
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $this->request->params;
            $params['user_id'] = $this->request->auth->id;

            $responseData = null ;
            $placeWorkingHours = $this->propertyPlaceService->update($params);

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

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $placeWorkingHours['data']];

        } 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 apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

    public function updatePlaceStatus(){
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $this->request->params;
            $params['user_id'] = $this->request->auth->id;

            $responseData = null ;
            $placeWorkingHours = $this->propertyPlaceService->updateStatus($params);

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

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $placeWorkingHours['data']];

        } 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 apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

    public function deletePlace(){
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $this->request->params;
            $params['user_id'] = $this->request->auth->id;

            $responseData = null ;
            $placeWorkingHours = $this->propertyPlaceService->deletePlace($params);

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

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $placeWorkingHours['data']];

        } 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 apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

    public function editPlace(){
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $this->request->params;
            $responseData = null ;
            $listPlaces = $this->propertyPlaceService->editPlaces($params);

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

            $responseData = $listPlaces['data'] ;

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $responseData];

        } 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 apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

    public function listPlaceFacilities(){
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $this->request->params;
            $responseData = null ;
            $listPlaces = $this->propertyPlaceService->listPlaceFacilities($params);

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

            $responseData['place_facilities'] = $listPlaces['data'] ;

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $responseData];

        } 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 apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

    public function updatePlaceFacilities(){
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $this->request->params;
            $params['user_id'] = $this->request->auth->id;

            $responseData = null ;
            $placeWorkingHours = $this->propertyPlaceService->updatePlaceFacilities($params);

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

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $placeWorkingHours['data']];

        } 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 apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

    public function getPlaceCategoryFields(){
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $this->request->params;
            $responseData = null ;
            $placeCategoryFields = $this->propertyPlaceService->getPlaceCategoryFields($params);
            if($placeCategoryFields['status'] != 'success'){
                throw new Exception($placeCategoryFields['message']);
            }
            $responseData['place_category_fields'] = $placeCategoryFields['data'] ;
            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $responseData];
        } 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 apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

    public function insertNewFieldMapping(){
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $this->request->params;
            $params['user_id'] = $this->request->auth->id;

            $responseData = null ;
            $placeCategoryFields = $this->propertyPlaceService->insertNewFieldMapping($params);
            if($placeCategoryFields['status'] != 'success'){
                throw new Exception($placeCategoryFields['message']);
            }
            $responseData['place_category_fields'] = $placeCategoryFields['data'] ;
            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $responseData];
        } 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 apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

}
