<?php

namespace App\Http\Controllers\V1;

use App\Http\Controllers\Controller;
use App\Core\Service\PropertyConfigService;
use Illuminate\Http\Request;

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

class PropertyConfigController extends Controller
{


    private $request;
    private $propertyConfigService;


    public function __construct(
        Request $request,
        PropertyConfigService $propertyConfigService
    )
    {
        $this->request = $request;
        $this->propertyConfigService = $propertyConfigService;

    }

    public function getPropertyConfig()
    {

        $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'),
                'config_keys' => fillOnUndefined($params, 'config_keys'),


            ];
            $propertyConfig = $this->propertyConfigService->getPropertyConfig($requestParams);

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

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyConfig['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 updatePropertyConfig(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'),
                'config_keys' => fillOnUndefined($params, 'config_keys'),
                'property_id' => fillOnUndefined($params, 'property_id'),
                'user_id' => $request->credentials->user_id,
            ];



            $propertyConfig = $this->propertyConfigService->propertyConfigUpdate($requestParams);

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

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


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



            $propertyConfig = $this->propertyConfigService->propertyConfigDelete($requestParams);

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

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

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


    }

}
