<?php

namespace App\Http\Controllers\V1;

use App\Core\Service\FindCountryCodeService;
use App\Core\Service\MyWebContentService;
use App\Core\Service\PdfContentService;
use App\Core\Service\LanguageService;
use App\Core\Service\PropertyWebLogService;
use App\Core\Service\PropertyWebPhotoMappingService;
use App\Core\Service\PropertyRoomPhotoMappingService;
use App\Core\Service\PropertyPlaceService;
use App\Core\Service\PropertyWebMetaService;

use App\Http\Controllers\Controller;
use Barryvdh\DomPDF\PDF;
use Illuminate\Http\Request;
use App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Config;
use Exception;
use App\Exceptions\ApiErrorException;
use Illuminate\Support\Str;
use ZipArchive;

class MyWebContentController
{


    private $request;
    private $myWebContentService;

    private $languageCode;

    public function __construct(
        Request                         $request,
        PDF                             $pdf,
        MyWebContentService             $myWebContentService,
        PdfContentService               $pdfContentService,
        LanguageService                 $languageService,
        PropertyWebPhotoMappingService  $propertyWebPhotoMappingService,
        PropertyRoomPhotoMappingService $propertyRoomPhotoMappingService,
        PropertyPlaceService            $propertyPlaceService,
        PropertyWebLogService           $propertyWebLogService,
        FindCountryCodeService          $findCountryCodeService,
        PropertyWebMetaService          $propertyWebMetaService
    )
    {
        $this->request = $request;
        $this->pdf = $pdf;
        $this->myWebContentService = $myWebContentService;
        $this->pdfContentService = $pdfContentService;
        $this->languageService = $languageService;
        $this->propertyWebPhotoMappingService = $propertyWebPhotoMappingService;
        $this->propertyRoomPhotoMappingService = $propertyRoomPhotoMappingService;
        $this->propertyPlaceService = $propertyPlaceService;
        $this->propertyWebLogService = $propertyWebLogService;
        $this->findCountryCodeService = $findCountryCodeService;
        $this->propertyWebMetaService = $propertyWebMetaService;

        $this->languageCode = $request->headers->get('language');

    }


    public function webMetaTag($propertyId, $propertyWebMetaId, $code = null)
    {

        $webMetaTag = null;
        $webMetaTagCriteria = [
            'criteria' => [
                ['field' => 'property_id', 'condition' => '=', 'value' => $propertyId],
                ['field' => 'property_web_meta_id', 'condition' => '=', 'value' => $propertyWebMetaId]
            ],
        ];

        if ($code) {
            $webMetaTagCriteria['criteria'][] = ['field' => 'code', 'condition' => '=', 'value' => $code];
        }

        $webMetaTagRequest = $this->propertyWebMetaService->selectMapping($webMetaTagCriteria);
        $webMetaTagRequest = $webMetaTagRequest['status'] == 'success' && !empty($webMetaTagRequest['status']) ? $webMetaTagRequest['data'] : [];

        foreach ($webMetaTagRequest as $webMetaTagData) {

            $webMetaTag[$webMetaTagData['property_web_meta_tag']['name']] = [
                'property_web_meta' => $webMetaTagData['property_web_meta']['name'],
                'property_web_meta_tag' => $webMetaTagData['property_web_meta_tag']['name'],
                'content' => collect($webMetaTagData['textArray'])->where('language_code', $this->languageCode)->first()['content'] ?? null
            ];

        }

        return $webMetaTag;

    }

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

            if (is_null($request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $request->body['params'];
            $params['mode'] = fillOnUndefined($params, 'mode', null);

            $responseData = $this->myWebContentService->home($params);
            if ($responseData['status'] != 'success') {
                throw new ApiErrorException($responseData['message']);
            }

            $agentIp = isset($params['agentInfo']['ip']) ? $params['agentInfo']['ip'] : null;
            if ($agentIp !== null) {
                // Find Country Code with IP
                $ipResponse = $this->findCountryCodeService->findCountryWithIpAddress($agentIp);
                if ($ipResponse['status'] !== 'success') {
                    //throw new ApiErrorException('IP Not Found');
                }

                // Create or Update Web Log
                if (!empty($params['agentInfo']) && !empty($ipResponse['data']) && !empty($ipResponse['data']['code']) && $ipResponse['status']) {

                    $agentParams = [
                        'web_id' => $params['property_web_id'],
                        'ip' => $agentIp,
                        'country_code' => $ipResponse['data'] ? $ipResponse['data']['code'] : null,
                        'isRobot' => $params['agentInfo']['isRobot'] === true ? $params['agentInfo']['isRobot'] : false,
                        'isDesktop' => $params['agentInfo']['isDesktop'] === true ? $params['agentInfo']['isDesktop'] : false,
                        'isPhone' => $params['agentInfo']['isPhone'] === true ? $params['agentInfo']['isPhone'] : false
                    ];

                    if ($agentParams['isRobot'] === false) {
                        $webLogResponse = $this->propertyWebLogService->createPropertyWebLog($agentParams);
                    }

                }
            }

            $responseData['data']['metaTag'] = $this->webMetaTag($params['property_id'], 1);

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

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

            if (is_null($request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $request->body['params'];
            $params['mode'] = fillOnUndefined($params, 'mode', null);

            $responseData = $this->myWebContentService->aboutUs($params);
            if ($responseData['status'] != 'success') {
                throw new ApiErrorException($responseData['message']);
            }

            $responseData['data']['metaTag'] = $this->webMetaTag($params['property_id'], 2);

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

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

            if (is_null($request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $request->body['params'];
            $params['mode'] = fillOnUndefined($params, 'mode', null);

            $responseData = $this->myWebContentService->gallery($params);
            if ($responseData['status'] != 'success') {
                throw new ApiErrorException($responseData['message']);
            }

            $responseData['data']['metaTag'] = $this->webMetaTag($params['property_id'], 9);

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

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

            if (is_null($request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $request->body['params'];
            $params['mode'] = fillOnUndefined($params, 'mode', null);

            $responseData = $this->myWebContentService->rooms($params);
            if ($responseData['status'] != 'success') {
                throw new ApiErrorException($responseData['message']);
            }

            $responseData['data']['metaTag'] = $this->webMetaTag($params['property_id'], 17);

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

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

            if (is_null($request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $request->body['params'];
            $params['mode'] = fillOnUndefined($params, 'mode', null);

            if (!isset($params['room_id'])) {
                throw new ApiErrorException('room_id required');
            }

            $responseData = $this->myWebContentService->roomDetail($params);
            if ($responseData['status'] != 'success') {
                throw new ApiErrorException($responseData['message']);
            }

            $responseData['data']['metaTag'] = $this->webMetaTag($params['property_id'], 18, $params['room_id']);

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

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

            if (is_null($request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $request->body['params'];
            $params['mode'] = fillOnUndefined($params, 'mode', null);

            $responseData = $this->myWebContentService->contact($params);
            if ($responseData['status'] != 'success') {
                throw new ApiErrorException($responseData['message']);
            }

            $responseData['data']['metaTag'] = $this->webMetaTag($params['property_id'], 5);

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

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

            if (is_null($request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $request->body['params'];
            $params['mode'] = fillOnUndefined($params, 'mode', null);

            $responseData = $this->myWebContentService->executiveDetail($params);
            if ($responseData['status'] != 'success') {
                throw new ApiErrorException($responseData['message']);
            }

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

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

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


            if ($request->headers->get('language')) {
                $language = $request->headers->get('language');
                app('translator')->setLocale($language);
            }

            $params = $request->body['params'];
            $params['mode'] = fillOnUndefined($params, 'mode', null);

            $responseData = $this->myWebContentService->contactForm($params);
            if ($responseData['status'] != 'success') {
                throw new ApiErrorException($responseData['message']);
            }

            $response = ['status' => 1, 'statusCode' => 200, 'message' => $responseData['message'], 'data' => $responseData['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 placeCategoryPlaces(Request $request)
    {

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

            if (is_null($request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $request->body['params'];
            $params['mode'] = fillOnUndefined($params, 'mode', null);

            if (!isset($params['place_category_id'])) {
                throw new ApiErrorException('place_category_id required');
            }

            $responseData = $this->myWebContentService->placeCategoryPlaces($params);
            if ($responseData['status'] != 'success') {
                throw new ApiErrorException($responseData['message']);
            }

            $responseData['data']['metaTag'] = $this->webMetaTag($params['property_id'], 12);

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

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

            if (is_null($request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $request->body['params'];
            $params['mode'] = fillOnUndefined($params, 'mode', null);

            if (!isset($params['place_id'])) {
                throw new ApiErrorException('place_id required');
            }

            $responseData = $this->myWebContentService->getPlace($params);
            if ($responseData['status'] != 'success') {
                throw new ApiErrorException($responseData['message']);
            }

            $responseData['data']['metaTag'] = $this->webMetaTag($params['property_id'], 13, $params['place_id']);

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

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

            if (is_null($request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $request->body['params'];
            $params['mode'] = fillOnUndefined($params, 'mode', null);

            $responseData = $this->myWebContentService->kvkk($params);
            if ($responseData['status'] != 'success') {
                throw new ApiErrorException($responseData['message']);
            }

            $responseData['data']['metaTag'] = $this->webMetaTag($params['property_id'], 10);

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

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

            if (is_null($request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $request->body['params'];
            $params['mode'] = fillOnUndefined($params, 'mode', null);

            $responseData = $this->myWebContentService->multimedia($params);
            if ($responseData['status'] != 'success') {
                throw new ApiErrorException($responseData['message']);
            }


            $contentCodeConfirmToken = fillOnUndefined($params, 'contentCodeConfirmToken');
            $contentCodeConfirmTokenCache = Cache::get($contentCodeConfirmToken);


            $multimedia = [];
            if ($responseData['data']['id'] == $contentCodeConfirmTokenCache['propertyId']) {

                $availableLanguageRequest = [
                    'criteria' => [
                        ['field' => 'status', 'condition' => '=', 'value' => 1],
                        ['field' => 'is_application', 'condition' => '=', 'value' => 1],
                        ['field' => 'is_published', 'condition' => '=', 'value' => 1]
                    ],
                ];

                $availableLanguages = $this->languageService->select($availableLanguageRequest, ['code', 'name', 'language_key']);


                $factSheetParam = [
                    'contentCodeConfirmToken' => fillOnUndefined($params, 'contentCodeConfirmToken'),
                    'language' => []
                ];

                foreach ($availableLanguages['data'] as $availableLanguage) {
                    $factSheetParam['language'][$availableLanguage['code']] = $availableLanguage;
                    $factSheetParam['language'][$availableLanguage['code']]['url'] = Config::get('app.url') . '/multimedia/fact/' . $availableLanguage['code'] . '/' . $contentCodeConfirmToken;
                }


                $photoParam = [];

                //Gallery
                $propertyWebPhotoMappingCriteria = [
                    'criteria' => [
                        ['field' => 'status', 'condition' => '=', 'value' => 1],
                        ['field' => 'property_id', 'condition' => '=', 'value' => $responseData['data']['id']]
                    ],
                    'with' => ['propertyPhoto'],
                    'orderBy' => [
                        ['field' => 'id', 'value' => 'ASC']
                    ]
                ];


                $propertyWebPhotoMapping = $this->propertyWebPhotoMappingService->select($propertyWebPhotoMappingCriteria);

                if ($propertyWebPhotoMapping['status'] == 'success' && !empty($propertyWebPhotoMapping['data'])) {

                    $propertyWebPhotoMapping = $propertyWebPhotoMapping['data'];
                    $firstPhoto = reset($propertyWebPhotoMapping);

                    $photoParam['gallery']['all'] = [
                        'id' => 'all',
                        'title' => 'Galeri',
                        'language_key' => 'myweb-menu-gallery',
                        'url' => Config::get('app.url') . '/multimedia/photo/gallery/all/' . $contentCodeConfirmToken,
                        'photoUrl' => $firstPhoto['property_photo']['photoUrl']
                    ];

                }


                //Room
                $propertyRoomPhotoMappingCriteria = [
                    'criteria' => [
                        ['field' => 'property_id', 'condition' => '=', 'value' => $responseData['data']['id']]
                    ],
                    'with' => ['propertyRoomPhoto', 'propertyRoom'],
                    'orderBy' => [
                        ['field' => 'id', 'value' => 'ASC']
                    ]
                ];

                $propertyRoomPhotoMapping = $this->propertyRoomPhotoMappingService->select($propertyRoomPhotoMappingCriteria);

                if ($propertyRoomPhotoMapping['status'] == 'success' && !empty($propertyRoomPhotoMapping['data'])) {

                    $propertyRoomPhotoMapping = $propertyRoomPhotoMapping['data'];
                    $firstPhoto = reset($propertyWebPhotoMapping);

                    $photoParam['room']['all'] = [
                        'id' => 'all',
                        'title' => 'Tüm Odalar',
                        'language_key' => 'api-multimedia-all_room_photos',
                        'url' => Config::get('app.url') . '/multimedia/photo/room/all/' . $contentCodeConfirmToken,
                        'photoUrl' => $firstPhoto['property_photo']['photoUrl']
                    ];


                    $propertyRoomPhotos = [];
                    foreach ($propertyRoomPhotoMapping as $propertyRoomPhoto) {
                        if ($propertyRoomPhoto['property_room']['status'] != 1) {
                            continue;
                        }
                        $propertyRoomPhotos[$propertyRoomPhoto['room_id']]['title'] = $propertyRoomPhoto['property_room']['name'];
                        $propertyRoomPhotos[$propertyRoomPhoto['room_id']]['photo'][] = $propertyRoomPhoto['property_room_photo']['photoUrl'];
                    }

                    foreach ($propertyRoomPhotos as $propertyRoomId => $propertyRoomPhoto) {
                        $photoParam['room'][$propertyRoomId] = [
                            'id' => $propertyRoomId,
                            'title' => $propertyRoomPhoto['title'],
                            'language_key' => $propertyRoomPhoto['title'],
                            'url' => Config::get('app.url') . '/multimedia/photo/room/' . $propertyRoomId . '/' . $contentCodeConfirmToken,
                            'photoUrl' => reset($propertyRoomPhoto['photo'])
                        ];
                    }
                }

                //Place
                $propertyPlacePhotoMappingCriteria = [
                    'criteria' => [
                        ['field' => 'property_id', 'condition' => '=', 'value' => $responseData['data']['id']]
                    ],
                    'with' => ['propertyPlacePhoto', 'propertyPlace'],
                    'orderBy' => [
                        ['field' => 'id', 'value' => 'ASC']
                    ]
                ];

                $propertyPlacePhotoMapping = $this->propertyPlaceService->selectPhotoMapping($propertyPlacePhotoMappingCriteria);


                if ($propertyPlacePhotoMapping['status'] == 'success' && !empty($propertyPlacePhotoMapping['data'])) {


                    $propertyPlacePhotoMapping = $propertyPlacePhotoMapping['data'];
                    $firstPhoto = reset($propertyPlacePhotoMapping);

                    $photoParam['place']['all'] = [
                        'id' => 'all',
                        'title' => 'Tüm Mekanlar',
                        'language_key' => 'api-multimedia-all_place_photos',
                        'url' => Config::get('app.url') . '/multimedia/photo/place/all/' . $contentCodeConfirmToken,
                        'photoUrl' => $firstPhoto['property_place_photo']['photoUrl']
                    ];


                    $propertyPlacePhotos = [];
                    foreach ($propertyPlacePhotoMapping as $propertyPlacePhoto) {
                        $propertyPlacePhotos[$propertyPlacePhoto['place_id']]['title'] = $propertyPlacePhoto['property_place']['name'];
                        $propertyPlacePhotos[$propertyPlacePhoto['place_id']]['photo'][] = $propertyPlacePhoto['property_place_photo']['photoUrl'];
                    }

                    foreach ($propertyPlacePhotos as $propertyPlaceId => $propertyPlacePhoto) {
                        $photoParam['place'][$propertyPlaceId] = [
                            'id' => $propertyRoomId,
                            'title' => $propertyPlacePhoto['title'],
                            'language_key' => $propertyPlacePhoto['title'],
                            'url' => Config::get('app.url') . '/multimedia/photo/place/' . $propertyPlaceId . '/' . $contentCodeConfirmToken,
                            'photoUrl' => reset($propertyPlacePhoto['photo'])
                        ];
                    }
                }


                $multimedia = [
                    "factSheet" => $factSheetParam,
                    "photo" => $photoParam
                ];

            }

            $responseData['data']['multimedia'] = $multimedia;

            $responseData['data']['metaTag'] = $this->webMetaTag($params['property_id'], 11);

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

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

        try {

            $contentCodeConfirmToken = null;

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

            $params = $request->body['params'];

            $responseData = $this->myWebContentService->multimedia($params);
            if ($responseData['status'] != 'success') {
                throw new ApiErrorException($responseData['message']);
            }

            if (empty($responseData['data']['content_code'])) {
                throw new ApiErrorException('No security code has yet been created to download multimedia.');
            }

            if ($params['privateToken'] != md5($responseData['data']['content_code'])) {
                throw new ApiErrorException('Security code could not be verified');
            }

            $contentCodeConfirmToken = md5($params['privateToken'] . '|' . $params['contentCodeToken']);

            $dataArray = [
                'propertyId' => $responseData['data']['id'],
                'contentCodeConfirmToken' => $contentCodeConfirmToken
            ];

            Cache::put($contentCodeConfirmToken, $dataArray, 10 * 60); //10 Min, 600 TTL

            unset($dataArray['propertyId']);

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $dataArray];

        } 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 multimediaFactDownload(Request $request, $language = 'en', $contentCodeConfirmToken)
    {

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

        try {

            $contentCodeConfirmTokenCache = Cache::get($contentCodeConfirmToken);

            if (is_null($contentCodeConfirmTokenCache)) {
                throw new ApiErrorException('Security code could not be verified');
            }

            $responseData = $this->myWebContentService->multimedia(['property_id' => $contentCodeConfirmTokenCache['propertyId']]);
            if ($responseData['status'] != 'success') {
                throw new ApiErrorException($responseData['message']);
            }

            $pdfDataRequest = [
                'locale' => $language,
                'property_id' => $contentCodeConfirmTokenCache['propertyId'],
            ];

            $pdfDataResponse = $this->pdfContentService->pdfData($pdfDataRequest);
            if ($pdfDataResponse['status'] != 'success') {
                throw new Exception($pdfDataResponse['message']);
            }

            $pdfData = $pdfDataResponse['data'];

            $logoBase64 = null;
            if ($pdfData['property_brand']) {
                if ($pdfData['property_brand']['logo_name']) {
                    $logoFile = Config::get('app.imageUrl') . '/property-photos/' . $pdfData['property_brand']['property_id'] . '/logo/' . $pdfData['property_brand']['logo_name'] . '_250x250.' . $pdfData['property_brand']['logo_file_ext'];
                    $logoBase64 = 'data:image/' . $pdfData['property_brand']['logo_file_ext'] . ';base64,' . base64_encode(file_get_contents($logoFile));
                }
            }


            $availableLanguageRequest = [
                'criteria' => [
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                    ['field' => 'is_application', 'condition' => '=', 'value' => 1],
                    ['field' => 'is_published', 'condition' => '=', 'value' => 1]
                ],
            ];

            $availableLanguages = $this->languageService->select($availableLanguageRequest, ['code', 'name', 'language_key']);
            $availableLanguages = $availableLanguages['status'] == 'success' ? $availableLanguages['data'] : [];
            $availableLanguages = pickItemFromArray('code', $availableLanguages);

            if (!in_array($language, $availableLanguages)) {
                $language = 'en';
            }


            app('translator')->setLocale($language);

            $this->pdf->loadView('pdf.hotelContent', compact('pdfData', 'logoBase64'), [], 'UTF8');
            $this->pdf->setOptions(['dpi' => 100, 'isHtml5ParserEnabled' => true, 'isRemoteEnabled' => true]);

            return $this->pdf->stream()->header('Content-Type', 'application/pdf');

        } 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 multimediaPhotoDownload(Request $request, $type, $id, $contentCodeConfirmToken)
    {

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

        try {

            $contentCodeConfirmTokenCache = Cache::get($contentCodeConfirmToken);

            if (is_null($contentCodeConfirmTokenCache)) {
                throw new ApiErrorException('Security code could not be verified');
            }

            $downloadPhotoIds = [];

            switch ($type) {
                case 'gallery':

                    $photoMappingCriteria = [
                        'criteria' => [
                            ['field' => 'status', 'condition' => '=', 'value' => 1],
                            ['field' => 'property_id', 'condition' => '=', 'value' => $contentCodeConfirmTokenCache['propertyId']]
                        ],
                        'with' => ['propertyPhoto'],
                        'orderBy' => [
                            ['field' => 'id', 'value' => 'ASC']
                        ]
                    ];


                    $photoMapping = $this->propertyWebPhotoMappingService->select($photoMappingCriteria);

                    if ($photoMapping['status'] == 'success' && !empty($photoMapping['data'])) {
                        $photoMapping = !empty($photoMapping['data']) ? $photoMapping['data'] : [];
                        $downloadPhotoIds = collect($photoMapping)->pluck('property_photo.photoUrl.fixed')->toArray();
                    }


                    break;
                case 'room':


                    $photoMappingCriteria = [
                        'criteria' => [
                            ['field' => 'property_id', 'condition' => '=', 'value' => $contentCodeConfirmTokenCache['propertyId']]
                        ],
                        'with' => ['propertyRoomPhoto', 'propertyRoom'],
                        'orderBy' => [
                            ['field' => 'id', 'value' => 'ASC']
                        ]
                    ];

                    if ($id != 'all' && is_numeric($id)) {
                        $photoMappingCriteria['criteria'][] = ['field' => 'room_id', 'condition' => '=', 'value' => $id];
                    }

                    $photoMapping = $this->propertyRoomPhotoMappingService->select($photoMappingCriteria);

                    if ($photoMapping['status'] == 'success' && !empty($photoMapping['data'])) {
                        $photoMapping = !empty($photoMapping['data']) ? $photoMapping['data'] : [];
                        $downloadPhotoIds = collect($photoMapping)->pluck('property_room_photo.photoUrl.fixed')->toArray();
                    }

                    break;
                case 'place':

                    $photoMappingCriteria = [
                        'criteria' => [
                            ['field' => 'property_id', 'condition' => '=', 'value' => $contentCodeConfirmTokenCache['propertyId']]
                        ],
                        'with' => ['propertyPlacePhoto', 'propertyPlace'],
                        'orderBy' => [
                            ['field' => 'id', 'value' => 'ASC']
                        ]

                    ];

                    if ($id != 'all' && is_numeric($id)) {
                        $photoMappingCriteria['criteria'][] = ['field' => 'place_id', 'condition' => '=', 'value' => $id];
                    }

                    $photoMapping = $this->propertyPlaceService->selectPhotoMapping($photoMappingCriteria);

                    if ($photoMapping['status'] == 'success' && !empty($photoMapping['data'])) {
                        $photoMapping = !empty($photoMapping['data']) ? $photoMapping['data'] : [];
                        $downloadPhotoIds = collect($photoMapping)->pluck('property_place_photo.photoUrl.fixed')->toArray();
                    }

                    break;
                case 'default':
                    break;

            }

            if (empty($downloadPhotoIds)) {
                throw new ApiErrorException('Photo list is empty');
            }


            $publicDir = public_path();
            $zipFileStoragePath = $publicDir . '/property-zip-files';

            if (!File::exists($zipFileStoragePath)) {
                File::makeDirectory($zipFileStoragePath, 0777, true);
            }

            $zipFileName = md5($contentCodeConfirmTokenCache['propertyId'] . '-' . $type . '-' . $id) . '.zip';


            $zipArchive = new ZipArchive;
            if (file_exists($zipFileStoragePath . '/' . $zipFileName)) {
                unlink($zipFileStoragePath . '/' . $zipFileName);
            }

            if ($zipArchive->open($zipFileStoragePath . '/' . $zipFileName, ZipArchive::CREATE) === TRUE) {
                foreach ($downloadPhotoIds as $downloadPhoto) {

                    $downloadPhotoExplode = explode('/', $downloadPhoto);
                    $photoName = last($downloadPhotoExplode);
                    $photoUrlFilePath = Config::get('app.fileSystemDriver') . '/property-photos/' . $contentCodeConfirmTokenCache['propertyId'] . '/' . $photoName;
                    if (file_exists($photoUrlFilePath)) {
                        $zipArchive->addFile($photoUrlFilePath, $photoName);
                    }
                }
                $zipArchive->close();
            }

            $responseData = Config::get('app.zipFilesUrl') . $zipFileName;

            return redirect($responseData);

        } 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 content(Request $request)
    {

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

            if (is_null($request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $request->body['params'];
            $params['mode'] = fillOnUndefined($params, 'mode', null);

            $responseData = $this->myWebContentService->content($params);
            if ($responseData['status'] != 'success') {
                throw new ApiErrorException($responseData['message']);
            }

            $responseData['data']['metaTag'] = $this->webMetaTag($params['property_id'], 6);

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

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

            if (is_null($request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $request->body['params'];
            $params['mode'] = fillOnUndefined($params, 'mode', null);

            $responseData = $this->myWebContentService->contentDetail($params);
            if ($responseData['status'] != 'success') {
                throw new ApiErrorException($responseData['message']);
            }


            $responseData['data']['metaTag'] = isset($responseData['data']['property_content']['id']) ? $this->webMetaTag($params['property_id'], 7, $responseData['data']['property_content']['id']) : null;

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

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

            if (is_null($request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $request->body['params'];
            $params['mode'] = fillOnUndefined($params, 'mode', null);

            $responseData = $this->myWebContentService->policy($params);
            if ($responseData['status'] != 'success') {
                throw new ApiErrorException($responseData['message']);
            }

            $responseData['data']['metaTag'] = $this->webMetaTag($params['property_id'], 14);

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

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

            if (is_null($request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $request->body['params'];
            $params['mode'] = fillOnUndefined($params, 'mode', null);

            if ($request->headers->get('language')) {
                $params['language'] = $request->headers->get('language');
            }

            $responseData = $this->myWebContentService->promotion($params);
            if ($responseData['status'] != 'success') {
                throw new ApiErrorException($responseData['message']);
            }

            $responseData['data']['metaTag'] = $this->webMetaTag($params['property_id'], 16);

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

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

            if (is_null($request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $request->body['params'];

            if ($request->headers->get('language')) {
                $params['language'] = $request->headers->get('language');
            }


            $responseData = $this->myWebContentService->affiliateRequestMail($params);
            if ($responseData['status'] != 'success') {
                throw new ApiErrorException($responseData['message']);
            }

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

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

            if (is_null($request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $request->body['params'];
            $params['mode'] = fillOnUndefined($params, 'mode', null);

            $responseData['data'] = null;

            $responseData['data']['metaTag'] = $this->webMetaTag($params['property_id'], 4);

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

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

            if (is_null($request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $request->body['params'];
            $params['mode'] = fillOnUndefined($params, 'mode', null);

            $responseData = $this->myWebContentService->aboutUs($params);
            if ($responseData['status'] != 'success') {
                throw new ApiErrorException($responseData['message']);
            }

            $responseData['data']['metaTag'] = $this->webMetaTag($params['property_id'], 8);

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

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

            if (is_null($request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $request->body['params'];
            $params['mode'] = fillOnUndefined($params, 'mode', null);

            $responseData = $this->myWebContentService->aboutUs($params);
            if ($responseData['status'] != 'success') {
                throw new ApiErrorException($responseData['message']);
            }

            $responseData['data']['metaTag'] = $this->webMetaTag($params['property_id'], 15);

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

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

            if (is_null($request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }
            $params = $request->body['params'];
            $params['mode'] = fillOnUndefined($params, 'mode', null);

            $responseData['data'] = null;

            $responseData['data']['metaTag'] = $this->webMetaTag($params['property_id'], 3);

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

}
