<?php

namespace App\Http\Controllers\V1;

use App\Http\Controllers\Controller;
use App\Core\Service\PropertyCancellationPolicyService;


use Illuminate\Http\Request;
use App;
use Illuminate\Support\Facades\Log;
use Exception;
use App\Exceptions\ApiErrorException;


class PropertyCancellationPolicyController extends Controller
{

    private $request;
    private $propertyCancellationPolicyService;

    public function __construct(
        Request $request,
        PropertyCancellationPolicyService $propertyCancellationPolicyService
    )
    {
        $this->request = $request;
        $this->propertyCancellationPolicyService = $propertyCancellationPolicyService;
    }

    public function getPropertyCancellationPolicyList(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;

            $getPropertyCancellationList = $this->propertyCancellationPolicyService->getPropertyCancellationPolicyList($params);

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

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => ['cancellation_policies' => $getPropertyCancellationList['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 createPropertyCancellationPolicy(Request $request)
    {
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];

        try {

            $params = $this->request->params;
            $params['user_id'] = $request->credentials->user_id;

            if (!isset($params['is_date_range']) || (isset($params['is_date_range']) && $params['is_date_range'] == 0)) {
                $params['is_date_range'] = 0;
                unset($params['start_date']);
                unset($params['finish_date']);
            }

            $createResponse = $this->propertyCancellationPolicyService->createPropertyCancellationPolicy($params);

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

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $createResponse['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 updatePropertyCancellationPolicy(Request $request)
    {
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];

        try {

            $params = $this->request->params;
            $params['user_id'] = $request->credentials->user_id;

            if (!isset($params['is_date_range']) || (isset($params['is_date_range']) && $params['is_date_range'] == 0)) {
                $params['is_date_range'] = 0;
                unset($params['start_date']);
                unset($params['finish_date']);
            }

            $updateResponse = $this->propertyCancellationPolicyService->updatePropertyCancellationPolicy($params);

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

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $updateResponse['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 getRoomRateChannelCancellationPolicy(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;

            $getPropertyCancellationList = $this->propertyCancellationPolicyService->getRoomRateChannelCancellationPolicy($params);

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

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => ['rooms' => $getPropertyCancellationList['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 updateRoomRateChannelCancellationPolicy(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;
            $params['user_id'] = $this->request->auth->id;


            $getPropertyCancellationList = $this->propertyCancellationPolicyService->updateRoomRateChannelCancellationPolicy($params);

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

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

        } 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']);


    }

}
