<?php

namespace App\Http\Controllers\V1;


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

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


class PropertyNonrefundableController extends Controller
{

    private $request;
    private $propertyNonrefundableService;

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

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


            $getPropertyNonrefundable = $this->propertyNonrefundableService->createForm($params);
            if($getPropertyNonrefundable['status'] != "success"){
                throw new ApiErrorException($getPropertyNonrefundable['message']);
            }

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

        try {

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

            $updateResponse = $this->propertyNonrefundableService->storePropertyNonrefundable($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 getPropertyChannelNonrefundable(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;


            $getPropertyNonrefundable = $this->propertyNonrefundableService->getNonrefundableData($params);
            if($getPropertyNonrefundable['status'] != "success"){
                throw new ApiErrorException($getPropertyNonrefundable['message']);
            }

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => ['channel_list' => $getPropertyNonrefundable['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']);


    }


}
