<?php

namespace App\View\Components;

use App\Models\Territory;
use App\Models\Utp as UtpModel;
use Cache;
use Cookie;
use Illuminate\View\Component;

//use Cache;

class Utp extends Component
{
    /**
     * @var int
     */
    private $block;
    /**
     * @var int|mixed
     */
    private $page;
    /**
     * @var bool
     */
    private $insert;
    /**
     * @var int
     */
    private $crop;
    /**
     * @var array|string|null
     */
    private $countryKey;

    /**
     * Create a new component instance.
     *
     * @return void
     */
    public function __construct(?int $block = 1, ?int $count = 15)
    {
        $this->crop = 2;
        $this->insert = ($block < $count - 1) && ($block > 1) && !($block % 9 !== 1 && $block % 9 !== 5);
        $this->block = $block;
        $this->page = (int) request()->page ?: 1;
        $this->countryKey = strtolower(Cookie::get('country_code'));
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\Contracts\View\View|string
     */
    public function render()
    {
        if (!$this->insert) {
            return '';
        }

        if (!session('country_link')) {
            return '';
        }

        $countrySlug = session('country_link', '');

        $getCountry = $this->countryKey;
        $key = 'utps_'.$countrySlug;
        $utps = Cache::get($key);
        if (!$utps) {
            $country = Territory::where('country_en', 'like', $countrySlug)->first('country_id')->country_id;
            if (!$country) {
                return '';
            }

            $utps = Cache::remember($key, 600, function () use ($country) {
                return UtpModel::where(function ($query) use ($country) {
                $query->where('country_id', $country);
                $query->orWhereNull('country_id');
            })
                ->wherePublish(1)
                ->with(['translation', 'addresses.translation'])
                ->oldest('priority')
                ->get();
            });
        }

        $count = $utps->count();

        if (!$count) {
            return '';
        }

        $crop = (int) !($this->block % $this->crop);
        $bannerIndex = $this->crop * ($this->page - 1) + $crop;

        if ($bannerIndex >= $count) {
            $bannerIndex %= $count;
        }

        $utp = $utps[$bannerIndex];

        if ($utp->addresses->count()) {
            $utp->addresses = $utp->addresses->sortBy('priority');
            $utp->addresses = collect([
                    $utp->addresses->filter(function ($item) use ($getCountry) {
                        return $item->country === $getCountry;
                    }), $utp->addresses->filter(function ($item) use ($getCountry) {
                        return $item->country !== $getCountry;
                    }),
                ]
            )->collapse();
        }

        $new = $utp->view;

        return view('layouts.UTP.'.$new, compact('utp'));
    }
}
