<?php

namespace App\Http\Controllers\V1;

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

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

class PropertyContentController extends Controller
{


    private $request;
    private $propertyContentService;


    public function __construct(
        Request $request,
        PropertyContentService $propertyContentService
    )
    {
        $this->request = $request;
        $this->propertyContentService = $propertyContentService;

    }

    public function getPropertyContent()
    {

        $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 = [

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

            ];
            $propertyContent = $this->propertyContentService->propertyContent($requestParams);

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

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyContent['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 updatePropertyContent(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 = [
                'active_user' => $this->request->credentials->user_id,
                'locale' => fillOnUndefined($params, 'locale'),
                'contents' => fillOnUndefined($params, 'contents'),
                'property_id' => fillOnUndefined($params, 'property_id'),
                'user_id' => $request->credentials->user_id,
            ];



            $propertyContent = $this->propertyContentService->propertyContentUpdate($requestParams);

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

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



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


    }

}
