<?php

namespace App\Http\Controllers\V1;

use App\Http\Controllers\Controller;
use App\Core\Service\PropertyExecutiveService;
use Illuminate\Http\Request;
use App\Core\Service\PropertyService;

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

class PropertyExecutiveController extends Controller
{


    private $request;
    private $propertyExecutiveService;
    private $propertyService;


    public function __construct(
        Request $request,
        PropertyService $propertyService,
        PropertyExecutiveService $propertyExecutiveService
    )
    {
        $this->request = $request;
        $this->propertyExecutiveService = $propertyExecutiveService;
        $this->propertyService = $propertyService;

    }

    public function getPropertyExecutive()
    {

        $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;


            $requestParams = [

                'locale' => fillOnUndefined($params, 'locale'),
                'property_id' => fillOnUndefined($params, 'property_id'),

            ];
            $propertyExecutive = $this->propertyExecutiveService->getPropertyExecutive($requestParams);

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

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyExecutive['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 listPropertyExecutive()
    {

        $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;


            $requestParams = [

                'locale' => fillOnUndefined($params, 'locale'),
                'property_id' => fillOnUndefined($params, 'property_id'),

            ];
            $propertyExecutive = $this->propertyExecutiveService->listPropertyExecutive($requestParams);
            if($propertyExecutive['status'] != 'success'){
                throw new Exception($propertyExecutive['message']);
            }

            $requestParams = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => fillOnUndefined($params, 'property_id')],
                ],
                'firstRow' => 1
            ];
            $property = $this->propertyService->select($requestParams, ['country']);
            if($property['status'] != 'success'){
                throw new Exception($property['message']);
            }
            $propertyExecutive['data']['country_code'] = strtolower($property['data']['country']) ;

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

            $requestParams = [
                'locale' => fillOnUndefined($params, 'locale'),
                'executive_type' => fillOnUndefined($params, 'executive_type'),
                'property_id' => fillOnUndefined($params, 'property_id'),
                'user_id' => $request->credentials->user_id,
            ];

            $propertyExecutive = $this->propertyExecutiveService->propertyExecutiveAdd($requestParams);
            if($propertyExecutive['status'] != 'success'){
                throw new ApiErrorException($propertyExecutive['message']);
            }



            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyExecutive['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 updatePropertyExecutive(Request $request)
    {

        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        $params = $this->request->params;
        $requestParams = [
            'locale' => fillOnUndefined($params, 'locale'),
            'executive_type' => fillOnUndefined($params, 'executive_type'),
            'property_id' => fillOnUndefined($params, 'property_id'),
            'user_id' => $request->credentials->user_id,
        ];
        $oldPropertyExecutive = $this->propertyExecutiveService->listPropertyExecutive($requestParams);

        try {

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

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

            $requestParams = [
                'locale' => fillOnUndefined($params, 'locale'),
                'executive_type' => fillOnUndefined($params, 'executive_type'),
                'property_id' => fillOnUndefined($params, 'property_id'),
                'user_id' => $request->credentials->user_id,
            ];

            $propertyExecutive = $this->propertyExecutiveService->propertyExecutiveUpdate($requestParams);

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



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

        } catch (ApiErrorException $e) {
            $response['data'] = $oldPropertyExecutive['data'] ;
            $response['message'] = implode(', ', $e->getMessageArr());
            $response['statusCode'] = 400;
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['data'] = $oldPropertyExecutive['data'] ;
            $response['message'] = $e->getMessage();
            $response['statusCode'] = 500;
        }
        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);


    }

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

            $requestParams = [
                'locale' => fillOnUndefined($params, 'locale'),
                'property_executive_id' => fillOnUndefined($params, 'property_executive_id'),
                'property_id' => fillOnUndefined($params, 'property_id'),
                'user_id' => $request->credentials->user_id,
            ];



            $propertyExecutive = $this->propertyExecutiveService->propertyExecutivePassive($requestParams);

            if($propertyExecutive['status'] != 'success'){

                throw new ApiErrorException($propertyExecutive['message']);
            }

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


    }

}
