<?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\BroadcastTemplate;
use App\Enums\TextAdColors;


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

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

    public bool $showModal = false;
    public ?int $textId = null;
    
    public array $colorOptions =[];

    public array $fields = [
        'content' =>'',
        'color' => 'white',
        'is_active' => true,
    ];
    public function rules()
    {
        return [
            'fields.content' => 'required|string|max:255',
            'fields.color' => ['required',new Enum(TextAdColors::class)],
            '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('broadcast-edit') ?? false;
        $this->canEdit = Auth::user()?->can('broadcast-edit') ?? false;
        $this->canDelect = Auth::user()?->can('broadcast-delete') ?? false;
    }

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

        if ($id) {
            $text = BroadcastTemplate::findOrFail($id);
            $this->textId = $text->id;
            $this->fields = $text->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->textId) {
            if ($this->canEdit) {
                $text = BroadcastTemplate::findOrFail($this->textId);
                $text->update($this->fields);
                $this->notification()->send([
                    'icon' => 'success',
                    'title' => '成功',
                    'description' => '文字公告已更新',
                ]);
            }
        } else {
            if ($this->canCreate) {
                $text = BroadcastTemplate::create($this->fields);
                $this->notification()->send([
                    'icon' => 'success',
                    'title' => '成功',
                    'description' => '文字公告已新增',
                ]);
            }
        }
        $this->resetFields();
        $this->showModal = false;
        $this->dispatch('pg:eventRefresh-broadcast-table');
    }
    
    public function deleteBroadcast($id)
    {
        if ($this->canDelect) {
            BroadcastTemplate::findOrFail($id)->delete();
            $this->notification()->send([
                'icon' => 'success',
                'title' => '成功',
                'description' => '文字公告已刪除',
            ]);
            
            $this->dispatch('pg:eventRefresh-broadcast-table');
        }
    }

    public function resetFields()
    {
        foreach ($this->fields as $key => $value) {
            if ($key == 'color') {
                $this->fields[$key] = 'white';
            } else if($key == 'is_active'){
                $this->fields[$key] = true;
            } else {
                $this->fields[$key] = '';
            }
        }
        $this->textId = null;
    }

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