<?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 Create 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 function mount()
    {
        $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();
    }

    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()
    {
        $banner = new Banner();

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

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

        $banner->save();

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

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

        $this->dispatch('showToast', ['message' => 'Banner created successfully!']);
    }

    public function saveAndRedirect()
    {
        $this->save();
        return redirect()->route('admin.banner.index');
    }

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