<?php

namespace App\Http\Controllers\BookingEngine\V1;

use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

Abstract class BookingEngineBaseController
{

    protected $cachePrefix = 'enwAPISearch:';
    protected $cacheExpireTime = 30;

    public function __construct()
    {

    }

    public function setCacheDataWithSearchKey($searchKey, $dataArray)
    {
        $searchKeyPrefix = $this->cachePrefix . $searchKey;
        Cache::put($searchKeyPrefix, $dataArray, $this->cacheExpireTime * 60);//5 min, 300 ttl, 600 10 dk

        if (Cache::has($searchKeyPrefix)) {
            return true;
        } else {
            return false;
        }

    }

    public function getCacheDataWithSearchKey($searchKey)
    {
        $cacheData = [];
        $searchKeyPrefix = $this->cachePrefix . $searchKey;
        if (Cache::has($searchKeyPrefix)) {
            $cacheData = Cache::get($searchKeyPrefix);

        }

        return $cacheData;

    }

    public function rateKeyCodeDecode($rateKeyCode)
    {
        $rateKeyCodeExplode = explode('-', $rateKeyCode);
        $data = [
            'propertyId' => $rateKeyCodeExplode[0],
            'channelId' => $rateKeyCodeExplode[1],
            'availabilityTypeId' => $rateKeyCodeExplode[2],
            'roomId' => $rateKeyCodeExplode[3],
            'rateMappingId' => $rateKeyCodeExplode[4],
            'roomOccupancyCode' => $rateKeyCodeExplode[5],
            'checkInDate' => Carbon::parse($rateKeyCodeExplode[6])->format('Y-m-d'),
            'checkOutDate' => Carbon::parse($rateKeyCodeExplode[7])->format('Y-m-d'),
            'cancellationPolicyId' => $rateKeyCodeExplode[8],
            'paymentTypeId' => $rateKeyCodeExplode[9],
        ];

        return $data;
    }

    public function getDateByDay($dates = [])
    {

        $dateByDay = [];

        $diffInDays = Carbon::parse($dates['checkIn'])->floatDiffInDays(Carbon::parse($dates['checkOut']));

        for ($i = 0; $i < $diffInDays; $i++) {
            $dateByDay[] = Carbon::parse($dates['checkIn'])->addDay($i)->format('Y-m-d');
        }

        return $dateByDay;

    }


    public function getRoomsByOccupancies($rooms = [])
    {
        $roomsByOccupancies = [];

        foreach ($rooms as $roomOrder => $room) {

            $roomKey = [];
            $roomKey[] = str_repeat('A', $room['adults']);
            if ($room['children'] > 0) {
                foreach ($room['age'] as $childAge) {
                    $roomKey[] = 'C' . $childAge;
                }
            }

            $roomKey = implode($roomKey, '');

            $roomsByOccupancies[$roomKey]['rooms'][] = $roomOrder;
            $roomsByOccupancies[$roomKey]['roomCount'] = count($roomsByOccupancies[$roomKey]['rooms']);
            $roomsByOccupancies[$roomKey]['occupancy'] = intval($room['adults'] + $room['children']);
            $roomsByOccupancies[$roomKey]['adultCount'] = $room['adults'];
            $roomsByOccupancies[$roomKey]['childCount'] = $room['children'];
            $roomsByOccupancies[$roomKey]['childAges'] = fillOnUndefined($room, 'age', []);
            $roomsByOccupancies[$roomKey]['occupancyCode'] = str_repeat('A', $room['adults']) . str_repeat('C', $room['children']);
        }

        return $roomsByOccupancies;

    }
}
