<?php

namespace App\Http\Controllers\V1;

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

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

class PropertyWebContentController extends Controller
{


    private $request;
    private $propertyWebContentService;


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

    public function getPropertyWebContent(Request $request, $id = null)
    {

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

            $columns = ['id', 'property_id', 'content_category_id', 'title', 'slug', 'image', 'language_code', 'status', 'created_at'];
            $propertyWebContentCriteria = [
                'criteria' => [
                    ['field' => 'property_id', 'condition' => '=', 'value' => $params['property_id']]
                ],
                'with' => ['ContentCategory'],
                'orderBy' => [
                    ['field' => 'id', 'value' => 'DESC']
                ]
            ];


            $availableFilterArray = ['title', 'content_category_id', 'language_code'];
            if (!empty($params['filter'])) {
                foreach ($params['filter'] as $filterKey => $filterValue) {
                    if (in_array($filterKey, $availableFilterArray)) {
                        if (in_array($filterKey, ['content_category_id', 'language_code', 'status'])) {
                            $propertyWebContentCriteria['criteria'][] = ['field' => $filterKey, 'condition' => '=', 'value' => $filterValue];
                        }

                        if (in_array($filterKey, ['title'])) {
                            $propertyWebContentCriteria['criteria'][] = ['field' => $filterKey, 'condition' => 'LIKE', 'value' => '%' . $filterValue . '%'];
                        }

                    }
                }
            }

            if (!empty($id)) {
                $propertyWebContentCriteria['criteria'][] = ['field' => 'id', 'condition' => '=', 'value' => $id];
                $propertyWebContentCriteria['firstRow'] = true;
                $columns[] = 'content';
            }


            $propertyWebContent = $this->propertyWebContentService->select($propertyWebContentCriteria, $columns);

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

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

        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

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

            $columns = ['id', 'name', 'language_key'];
            $propertyWebContentCategoryCriteria = [
                'criteria' => [
                    ['field' => 'status', 'condition' => '=', 'value' => 1]
                ],
                'orderBy' => [
                    ['field' => 'id', 'value' => 'ASC']
                ]
            ];

            $propertyWebContentCategory = $this->propertyWebContentService->selectWebContentCategory($propertyWebContentCategoryCriteria, $columns);
            if ($propertyWebContentCategory['status'] != 'success') {
                throw new Exception($propertyWebContentCategory['message']);
            }

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyWebContentCategory['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 createPropertyWebContent(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 = [
                'property_id' => fillOnUndefined($params, 'property_id'),
                'content_category_id' => fillOnUndefined($params, 'content_category_id'),
                'title' => fillOnUndefined($params, 'title'),
                'language_code' => fillOnUndefined($params, 'language_code'),
                'image' => fillOnUndefined($params, 'image'),
                'content' => fillOnUndefined($params, 'content'),
                'user_id' => $this->request->credentials->user_id
            ];

            $propertyContent = $this->propertyWebContentService->create($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']);


    }

    public function updatePropertyWebContent(Request $request, $id)
    {

        $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 = [
                'property_id' => fillOnUndefined($params, 'property_id'),
                'content_category_id' => fillOnUndefined($params, 'content_category_id'),
                'title' => fillOnUndefined($params, 'title'),
                'language_code' => fillOnUndefined($params, 'language_code'),
                'image' => fillOnUndefined($params, 'image'),
                'content' => fillOnUndefined($params, 'content'),
                'updated_by' => $this->request->credentials->user_id
            ];

            $propertyContent = $this->propertyWebContentService->update($id, $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']);


    }

    public function deletePropertyWebContent(Request $request, $id)
    {

        $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 = [
                'id' => $id,
                'property_id' => fillOnUndefined($params, 'property_id')
            ];

            $propertyContent = $this->propertyWebContentService->delete($id, $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']);


    }

    public function uploadPropertyWebContentImage(Request $request)
    {
        $response = ['status' => false, 'statusCode' => 500, 'message' => '', 'data' => null];
        try {
            if (!$request->hasFile('image')) {
                throw new ApiErrorException(lang('Photos not found'));
            }

            $param = [
                "property_id" => $request->input('property_id'),
                "photo" => $request->file('image'),
            ];

            $response = $this->propertyWebContentService->uploadPhoto($param);

        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();

        } catch (Exception $e) {
            $message = $e->getFile() . ' ' . $e->getLine() . ' ' . $e->getMessage();
            Log::error($message);
        }

        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }


}
