<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use App\Models\GenericBackoffice\WebsiteObject;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use App\Models\Tariff as TariffModel;
use App\Models\Country as CountryModel;
use App\Models\PackType as PackTypeModel;
use App\Models\Faq as FaqModel;
use App\Models\Misc as Misc;
use App\Models\FaqCategory as FaqCategoryModel;
use App\Models\InternationalRate as InternationalRateModel;
use App\Facades\RequestContext;
use App\Models\Pack as PackModel;

class Block extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function __construct($resource)
    {
        $this->misc = RequestContext::get('miscData') ?? Misc::first();
        RequestContext::set('miscData', $this->misc);
        $this->resource = $resource;
    }

    public function toArray($request)
    {
        // Default data that we need to return on every block
        $out = [
            'id'          => (int)$this->id,
            'title'       => $this->block_title ? $this->block_title : $this->title,
            'type'        => Str::kebab($this->type),
        ];

        // Specific block data per type
        $out = array_merge($out, $this->_getBlockData());

        return $out;
    }

    // Header Parser
    private function _getBlockData()
    {
        $out = [];
        switch($this->type) {
            case 'richText':
                $out = $this->_getRitchText();
                break;
            case 'hero':
            case 'leftImage':
            case 'leftCard':
            case 'rightImage':
            case 'topImage':
            case 'mobileAppBlock':
            case 'centerBlock':
            case 'landingHeader':
            case 'contactForm':
                $out = $this->_getImageBlocksFields();
                break;
            case 'headerWithImage':
                $out = $this->_getHeaderWithImage();
                break;
            case 'tariffList':
                $out = $this->_getTariffList();
                break;
            case 'blockSlider':
                $out = $this->_getSlider();
                break;
            case 'moreLink':
                $out = $this->_getMoreLink();
                break;
            case 'roamingPage':
            case 'newRoamingPage':
                $out = $this->_getRoamingPage();
                break;
            case 'packagesPage':
                $out = $this->_getPackagesPage();
                break;
            case 'supportHeader':
                $out = $this->_getSupportHeader();
                break;
            case 'popularFaqs':
                $out = $this->_getPopularFaqs();
                break;
            case 'standardHeader':
                $out = $this->_getStandardHeader();
                break;
            case 'doubleImage':
                $out = $this->_getDoubleImageBlocksFields();
                break;
        }
        return $out;
    }

    // Block RitchText
    private function _getRitchText()
    {
        $out = [
            'title' => $this->block_title,
            'content' => $this->wysiwyg
        ];

        return $out;
    }

    // Block Hero
    private function _getImageBlocksFields()
    {
        $out = [
            'title' => $this->block_title,
            'tagline' => $this->tagline,
            'preTitle' => $this->pre_title,
            'content' => $this->wysiwyg,
            'cta1' => getLinkPath($this->cta_1),
            'cta2' => getLinkPath($this->cta_2),
            'image' => !empty($this->image[0]) ? getImage($this->image[0], 'w700') : null,
            'imageMedium' => !empty($this->image[0]) ? getImage($this->image[0], 'w1000') : null,
            'imageBig' => !empty($this->image[0]) ? getImage($this->image[0], 'w1400') : null,
            'priceType' => null,
            'priceTag' => $this->price_tag,
            'price' => $this->price,
        ];

        if (!empty($this->misc) && $this->misc->price_type) {
            $out['priceType'] = $this->misc->price_type;
            $out['priceTag'] = $this->misc->price_tag;
            $out['price'] = $this->misc->price;
            $out['discountPrice'] = $this->misc->discount_price;
            $out['discount'] = $this->misc->discount;
            $out['discountText'] = $this->misc->discount_text;
        }

        return $out;
    }

    private function _getDoubleImageBlocksFields()
    {
        $out = [
            'cta1' => getLinkPath($this->cta_1),
            'cta2' => getLinkPath($this->cta_2),
            'slides'  => Slide::collection($this->slider)
        ];

        return $out;
    }

    private function _getHeaderWithImage()
    {
        $out = [
            'title' => $this->block_title,
            'image' => getImage($this->image[0], 'w320')
        ];

        return $out;
    }

    private function _getStandardHeader()
    {
        $checklist = !empty($this->checklist) ? explode("\n", $this->checklist) : null;
        $out = [
            'title' => $this->block_title,
            'preTitle' => $this->pre_title,
            'checklist' => $checklist,
        ];

        return $out;
    }

    private function _getSlider()
    {
        $out = [
            'slides'  => Slide::collection($this->slider)
        ];

        return $out;
    }

    private function _getTariffList()
    {
        $out = [
            'title' => $this->block_title,
            'content' => $this->wysiwyg,
            'tariffs' => Tariff::collection($this->tariffs)
        ];

        return $out;
    }

    private function _getMoreLink()
    {
        $out = [
            'cta1' => getLinkPath($this->cta_1)
        ];

        return $out;
    }

    private function _getRoamingPage()
    {
        $roamingTariffs = TariffModel::full()
            ->where('type', 'roaming')
            ->where('status', 'published')
            ->get();

        $countries = CountryModel::full()
            ->where('status', 'published')
            ->whereNotNull('region')
            ->orderBy('title', 'asc')
            ->get();

        $internationalRates = InternationalRateModel::full()
            ->with(['countries' => function ($query) {
                $query->full()
                    ->where('status', 'published');
            }])
            ->get();

        $out = [
            'title' => $this->block_title,
            'preTitle' => $this->pre_title,
            'listTariffs' => Tariff::collection($this->tariffs),
            'roamingTariffs' => Tariff::collection($roamingTariffs),
            'countries' => Country::collection($countries),
            'internationalRates' => InternationalRate::collection($internationalRates)
        ];

        return $out;
    }

    private function _getPackagesPage()
    {
        $packTypes = PackTypeModel::full()
            ->with(['packs' => function ($query) {
                $query->full()
                    ->with(['simpleContents' => function ($query) {
                        $query->full()
                            ->where('status', 'published')
                            ->orderBy('pack_simple_content.position', 'asc');
                    }])
                    ->where('status', 'published')
                    ->orderBy('pack_pack_type.position', 'asc');
            }])
            ->where('status', 'published')
            ->orderBy('position', 'asc')
            ->get();

        $out = [
            'title' => $this->block_title,
            'preTitle' => $this->pre_title,
            'cta1' => getLinkPath($this->cta_1),
            'packTypes' => PackType::collection($packTypes)
        ];

        return $out;
    }

    private function _getSupportHeader()
    {
        $categories = FaqCategoryModel::full()
            ->where('status', 'published')
            ->orderBy('position', 'asc')
            ->get();

        $out = [
            'title' => $this->block_title,
            'preTitle' => $this->pre_title,
            'faqCategories' => FaqCategory::collection($categories)
        ];

        return $out;
    }

    private function _getPopularFaqs()
    {
        $faqs = FaqModel::full()
            ->where('status', 'published')
            ->orderBy('views', 'desc')
            ->limit(6)
            ->get();

        $out = [
            'title' => $this->block_title,
            'preTitle' => $this->pre_title,
            'faqs' => Faq::collection($faqs)
        ];

        return $out;
    }
}
