<?php

namespace App\Livewire\Admin\Popup;

use Livewire\Component;
use App\Models\Admin\Page;
use App\Models\Admin\Popup;
use App\Models\Admin\Language;
use Livewire\Attributes\Computed;

class Edit extends Component
{
    public $selectedLanguage;
    public $defaultLanguage;

    public $image;
    public $image_path;

    public $image_mobile;
    public $image_mobile_path;

    public $translations = [];

    public $title;
    public $description;
    public $popup_link;
    public $page_id;
    public $display_type;
    public $popup_display_scope;
    public $popup_display_freq;

    public Popup $popup;

    public function mount(Popup $popup)
    {
        $this->popup = $popup;

        $this->defaultLanguage = Language::where('default', 1)->first()->short;
        $this->selectedLanguage = $this->defaultLanguage;

        $this->translations = Language::where('status', 1)
            ->orderByDesc('default')
            ->pluck('short')
            ->mapWithKeys(function ($short) {
                return [$short => ['title' => '', 'description' => '', 'popup_link' => '']];
            })
            ->toArray();

        foreach ($this->translations as $locale => &$title) {
            $this->translations[$locale] = [
                'title' => $this->popup->getTranslation('title', $locale, false),
                'description' => $this->popup->getTranslation('description', $locale, false),
                'popup_link' => $this->popup->getTranslation('popup_link', $locale, false),
            ];
        }

        $this->title = $this->popup->getTranslation('title', $this->selectedLanguage, false);
        $this->description = $this->popup->getTranslation('description', $this->selectedLanguage, false);
        $this->popup_link = $this->popup->getTranslation('popup_link', $this->selectedLanguage, false);

        $this->image = basename($this->popup->image);
        $this->image_path = $this->popup->image;
        $this->image_mobile = basename($this->popup->image_mobile);
        $this->image_mobile_path = $this->popup->image_mobile;
        $this->page_id = $this->popup->page_id;
        $this->display_type = $this->popup->display_type;
        $this->popup_display_scope = $this->popup->popup_display_scope;
        $this->popup_display_freq = $this->popup->popup_display_freq;

        $this->dispatch('languageChanged', content: $this->description);
    }

    public function changeLanguage($language)
    {
        $this->selectedLanguage = $language;

        $this->title = $this->translations[$language]['title'];
        $this->description = $this->translations[$language]['description'];
        $this->popup_link = $this->translations[$language]['popup_link'];

        $this->dispatch('languageChanged', content: $this->description);
    }

    #[Computed()]
    public function pages()
    {
        return Page::whereNull('parent_id')->with('children')->get();
    }

    public function updatedTitle($value)
    {
        $this->translations[$this->selectedLanguage]['title'] = $value;
    }

    public function updatedDescription($value)
    {
        $this->translations[$this->selectedLanguage]['description'] = $value;
    }

    public function updatedPopupLink($value)
    {
        $this->translations[$this->selectedLanguage]['popup_link'] = $value;
    }

    public function save()
    {
        foreach ($this->translations as $language => $translation) {
            $this->popup->setTranslation('title', $language, $translation['title']);
            $this->popup->setTranslation('description', $language, $translation['description']);
            $this->popup->setTranslation('popup_link', $language, $translation['popup_link']);
        }

        $this->popup->image = $this->image_path;
        $this->popup->image_mobile = $this->image_mobile_path;
        $this->popup->page_id = $this->page_id ? $this->page_id : null;
        $this->popup->display_type = $this->display_type;
        $this->popup->popup_display_scope = $this->popup->popup_display_scope;
        $this->popup->popup_display_freq = $this->popup->popup_display_freq;

        $this->popup->save();

        activity()->performedOn($this->popup)->causedBy(auth()->id())->log('Popup güncellendi');

        $this->dispatch('showAlert', ['title' => 'Başarılı!', 'text' => 'Popup başarıyla güncellendi.', 'icon' => 'success']);
    }

    public function render()
    {
        return view('livewire.admin.popup.edit');
    }
}
