<?php

namespace App\Livewire\Admin\Banner;

use Livewire\Component;
use App\Models\Admin\Page;
use App\Models\Admin\Banner;
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 $button_title;
    public $button_link;
    public $page_id;

    public Banner $banner;

    public function mount(Banner $banner)
    {
        $this->banner = $banner;

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

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

        $this->title = $this->banner->getTranslation('title', $this->selectedLanguage, false);
        $this->description = $this->banner->getTranslation('description', $this->selectedLanguage, false);
        $this->button_title = $this->banner->getTranslation('button_title', $this->selectedLanguage, false);
        $this->button_link = $this->banner->getTranslation('button_link', $this->selectedLanguage, false);

        $this->image = basename($this->banner->image);
        $this->image_path = $this->banner->image;
        $this->image_mobile = basename($this->banner->image_mobile);
        $this->image_mobile_path = $this->banner->image_mobile;
        $this->page_id = $this->banner->page_id;
    }

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

        $this->title = $this->translations[$language]['title'];
        $this->description = $this->translations[$language]['description'];
        $this->button_title = $this->translations[$language]['button_title'];
        $this->button_link = $this->translations[$language]['button_link'];
    }

    #[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 updatedButtonTitle($value)
    {
        $this->translations[$this->selectedLanguage]['button_title'] = $value;
    }

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

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

        $this->banner->image = str_replace(config('app.url'),'',$this->image_path);
        $this->banner->image_mobile = str_replace(config('app.url'),'',$this->image_mobile_path);

        $this->banner->page_id = $this->page_id ? $this->page_id : null;

        $this->banner->save();

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

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

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