<?php

namespace App\Livewire\Admin\Navigation\Link;

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

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

    public $type;
    public NavigationMenu $navigationMenu;

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

    public function mount($type, NavigationMenu $navigationMenu)
    {
        if ($type != 'page' && $type != 'url')
            return $this->redirectRoute('admin.navigation.menu.index');

        $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' => '', 'url' => '']];
            })
            ->toArray();

        $this->type = $type == 'url' ? 1 : 2;
        $this->target = '_self';
        $this->navigationMenu = $navigationMenu;
    }

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

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

        $this->title = $this->translations[$language]['title'] ?? '';
        $this->url = $this->translations[$language]['url'] ?? '';
        $this->description = $this->translations[$language]['description'] ?? '';
    }

    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()
    {
        $navigationLink = new NavigationLink();

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

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

        $navigationLink->parent_id = $this->parent_id;
        $navigationLink->navigation_menu_id = $this->navigationMenu->id;
        $navigationLink->type = $this->type;
        $navigationLink->target = $this->target;
        $navigationLink->class = $this->class;

        $navigationLink->save();

        activity()->performedOn($navigationLink)->causedBy(auth()->id())->log('Link oluşturuldu');

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

        $this->resetExcept('type', 'navigationMenu');
    }

    public function saveAndRedirect()
    {
        $this->save();

        return redirect()->route('admin.navigation.link.index', $this->navigationMenu->id);
    }

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