<?php

namespace App\Livewire\Admin\Navigation\Link;

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

class Edit extends Component
{
    public $selectedLanguage;
    public $defaultLanguage;
    public $translations = [];

    public $type;
    public NavigationMenu $navigationMenu;
    public NavigationLink $navigationLink;

    public $parent_id;
    public $page_id;
    public $url;
    public $title;
    public $description;
    public $target;
    public $class;

    public function mount(NavigationMenu $navigationMenu, NavigationLink $navigationLink)
    {
        $this->navigationMenu = $navigationMenu;
        $this->navigationLink = $navigationLink;

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

        $this->initializeTranslations();

        $this->parent_id = $navigationLink->parent_id;
        $this->page_id = $navigationLink->page_id;
        $this->type = $navigationLink->type;
        $this->target = $navigationLink->target;
        $this->class = $navigationLink->class;
    }

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

    private function initializeTranslations()
    {
        $languages = Language::where('status', 1)->orderByDesc('default')->pluck('short');

        foreach ($languages as $short) {
            $this->translations[$short] = [
                'title' => $this->navigationLink->getTranslation('title', $short, false) ?? '',
                'description' => $this->navigationLink->getTranslation('description', $short, false) ?? '',
                'url' => $this->navigationLink->getTranslation('url', $short, false) ?? ''
            ];
        }

        $this->setFieldsForSelectedLanguage();
    }

    private function setFieldsForSelectedLanguage()
    {
        $this->title = $this->translations[$this->selectedLanguage]['title'] ?? '';
        $this->url = $this->translations[$this->selectedLanguage]['url'] ?? '';
        $this->description = $this->translations[$this->selectedLanguage]['description'] ?? '';
    }

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

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

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

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

    public function save()
    {
        foreach ($this->translations as $language => $translation) {
            $this->navigationLink->setTranslation('title', $language, $translation['title']);
            $this->navigationLink->setTranslation('description', $language, $translation['description']);
            if($this->type == 1) {
                $this->navigationLink->setTranslation('url', $language, $translation['url']);
            }
        }

        $this->navigationLink->page_id = $this->page_id;

        $this->navigationLink->parent_id = $this->parent_id;
        $this->navigationLink->type = $this->type;
        $this->navigationLink->target = $this->target;
        $this->navigationLink->class = $this->class;

        $this->navigationLink->save();

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

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

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