<?php

namespace App\Http\Controllers\V1;

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

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

class PropertyWebPopupController extends Controller
{


    private $request;
    private $propertyWebContentService;


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

    public function getPropertyWebPopup(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', 'title', 'language_code', 'start_date', 'end_date', 'url', 'image', 'status', 'created_at'];
            $criteria = [
                'criteria' => [
                    ['field' => 'property_id', 'condition' => '=', 'value' => $params['property_id']],
                ],
                'orderBy' => [
                    ['field' => 'id', 'value' => 'DESC']
                ]
            ];

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

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

                    }
                }
            }

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


            $propertyWebPopup = $this->propertyWebPopupService->select($criteria, $columns);

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

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyWebPopup['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 createPropertyWebPopup(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'),
                'title' => fillOnUndefined($params, 'title'),
                'language_code' => fillOnUndefined($params, 'language_code'),
                'image' => fillOnUndefined($params, 'image'),
                'start_date' => fillOnUndefined($params, 'start_date'),
                'end_date' => fillOnUndefined($params, 'end_date'),
                'url' => fillOnUndefined($params, 'url'),
                'user_id' => $this->request->credentials->user_id
            ];


            $propertyWebPopup = $this->propertyWebPopupService->create($requestParams);

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

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyWebPopup['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 updatePropertyWebPopup(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'),
                'title' => fillOnUndefined($params, 'title'),
                'language_code' => fillOnUndefined($params, 'language_code'),
                'image' => fillOnUndefined($params, 'image'),
                'start_date' => fillOnUndefined($params, 'start_date'),
                'end_date' => fillOnUndefined($params, 'end_date'),
                'url' => fillOnUndefined($params, 'url'),
                'updated_by' => $this->request->credentials->user_id
            ];

            $propertyWebPopup = $this->propertyWebPopupService->update($id, $requestParams);

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

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

            $propertyWebPopup = $this->propertyWebPopupService->delete($id, $requestParams);

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

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


}
