<?php

namespace App\Livewire\Forms;

use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Auth;

use Livewire\Component;
use WireUi\Traits\WireUiActions;

use Illuminate\Validation\Rules\Enum;
use App\Models\TextAd;
use App\Enums\TextAdColors;

class TextAdsForm extends Component
{
    use WireUiActions;
    
    protected $listeners = ['openModal','closeModal', 'deleteTextAd'];

    public bool $canCreate;
    public bool $canEdit;
    public bool $canDelect;

    public bool $showModal = false;
    public ?int $textAdId = null;
    
    public array $colorOptions =[];
    public array $fields = [
        'content' =>'',
        'color' => 'black',
        'duration' => 1,
        'is_active' => 1,
    ];
    public function rules()
    {
        return [
            'fields.content' => 'required|string|max:255',
            'fields.color' => ['required',new Enum(TextAdColors::class)],
            'fields.duration' => 'required|integer|min:1|max:60', 
            'fields.is_active' => 'required|boolean',
        ];
    }
    

    public function mount()
    {
        $this->colorOptions = collect(TextAdColors::cases())->map(fn ($color) => [
            'name' => $color->labels(),
            'value' => $color->value,
        ])->toArray();
        $this->canCreate = Auth::user()?->can('text-ad-edit') ?? false;
        $this->canEdit = Auth::user()?->can('text-ad-edit') ?? false;
        $this->canDelect = Auth::user()?->can('text-ad-delete') ?? false;
    }

    public function openModal($id = null)
    {
        $this->resetFields();

        if ($id) {
            $textAd = TextAd::findOrFail($id);
            $this->textAdId = $textAd->id;
            $this->fields = $textAd->only(array_keys($this->fields));
        }
    
        $this->showModal = true;
    }

    public function closeModal()
    {
        $this->resetFields();
        $this->showModal = false;
    }
    
    public function save()
    {
        $validated = $this->validate($this->rules());
        if ($this->textAdId) {
            if ($this->canEdit) {
                $textAd = TextAd::findOrFail($this->textAdId);
                $textAd->update($this->fields);
                $this->notification()->send([
                    'icon' => 'success',
                    'title' => '成功',
                    'description' => '文字廣吿已更新',
                ]);
            }
        } else {
            if ($this->canCreate) {
                $textAd = TextAd::create($this->fields);
                $this->notification()->send([
                    'icon' => 'success',
                    'title' => '成功',
                    'description' => '文字廣吿已新增',
                ]);
            }
        }
        $this->resetFields();
        $this->showModal = false;
        $this->dispatch('pg:eventRefresh-text-ads-table');
    }
    
    public function deleteTextAd($id)
    {
        if ($this->canDelect) {
            TextAd::findOrFail($id)->delete();
            $this->notification()->send([
                'icon' => 'success',
                'title' => '成功',
                'description' => '文字廣吿已刪除',
            ]);
            
            $this->dispatch('pg:eventRefresh-text-ads-table');
        }
    }

    public function resetFields()
    {
        foreach ($this->fields as $key => $value) {
            if ($key == 'color') {
                $this->fields[$key] = 'black';
            } else if ($key == 'content'){
                $this->fields[$key] = '';
            } else {
                $this->fields[$key] = 1;
            }
        }
        $this->textAdId = null;
    }

    public function render()
    {
        return view('livewire.forms.text-ads-form');
    }
}
