<?php

namespace App\Http\Controllers\V1;


use App\Http\Controllers\Controller;
use App\Core\Service\PropertyBrandService;
use App\Core\Service\PropertyConfigService ;


use Illuminate\Http\Request;

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


class PropertyBrandController extends Controller
{


    private $request;
    private $propertyBrandService;
    private $propertyConfigService;


    public function __construct(
        Request $request,
        PropertyBrandService $propertyBrandService,
        PropertyConfigService $propertyConfigService


    )
    {
        $this->request = $request;
        $this->propertyBrandService = $propertyBrandService;
        $this->propertyConfigService = $propertyConfigService ;

    }

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

        try {



            $params =
                [
                    "property_id" => $request->input('property_id'),
                    "title" => $request->input("title"),
                    "color_codes" => $request->input("colors"),
                    "logo_path" => $request->input( "logo_path"),
                    "logo_name" => $request->input( "logo_name"),
                    "photo" => $request->file('photo'),
                    "user_id" => $this->request->auth->id
                ];


            $updateResponse = $this->propertyBrandService->updatePropertyBrand($params);

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

            $rateParams = [
                'property_id' => fillOnUndefined($params, 'property_id'),
                'user_id' => fillOnUndefined($params, 'user_id'),
                'property_rate_for' => 'Property.Brand.Update',
            ];
            $this->propertyConfigService->rateProperty(array_merge($params, $rateParams));

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => ['get_property_brand' => $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 getPropertyBrand(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;
            $getPropertyBrand = $this->propertyBrandService->getPropertyBrand($params);
            if($getPropertyBrand['status'] != "success"){
                throw new ApiErrorException($getPropertyBrand['message']);
            }

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => ['get_property_brand' => $getPropertyBrand['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 clearBrandLogo(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->credentials->user_id;

            $getPropertyBrand = $this->propertyBrandService->clearBrandLogo($params);

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

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


}
