<?php

namespace App\Filament\Pages;

use App\Services\PrinterService;
use App\Settings\GeneralSettings;
use BackedEnum;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Filament\Schemas\Schema;
use Illuminate\Support\Arr;
use UnitEnum;

class GlobalSettings extends Page implements HasForms
{
    use InteractsWithForms;

    protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-cog';

    protected string $view = 'filament.pages.global-settings';

    protected static string|UnitEnum|null $navigationGroup = 'Admin';

    public ?array $data = [];

    public function mount(GeneralSettings $settings): void
    {
        $this->form->fill($settings->toArray());
    }

    public function form(Schema $schema): Schema
    {
        $printerService = new PrinterService;
        $printers = $printerService->getPrinters();
        $printerOptions = array_merge($printers, ['__custom__' => __('filament.resource.setting.form.custom_printer')]);

        return $schema
            ->schema([
                TextInput::make('gallery_heading')
                    ->label(__('filament.resource.setting.form.gallery_heading'))
                    ->required(),
                TextInput::make('new_image_timespan_minutes')
                    ->label(__('filament.resource.setting.form.new_image_timespan_minutes'))
                    ->numeric()
                    ->required(),
                TextInput::make('image_refresh_interval')
                    ->label(__('filament.resource.setting.form.image_refresh_interval'))
                    ->numeric()
                    ->required(),
                TextInput::make('max_number_of_copies')
                    ->label(__('filament.resource.setting.form.max_number_of_copies'))
                    ->numeric()
                    ->required(),
                Toggle::make('show_print_button')
                    ->label(__('filament.resource.setting.form.show_print_button')),
                Select::make('selected_printer')
                    ->label(__('filament.resource.setting.form.printer'))
                    ->options($printerOptions)
                    ->reactive(),
                TextInput::make('custom_printer_address')
                    ->label(__('filament.resource.setting.form.custom_printer_address'))
                    ->visible(fn ($get) => $get('selected_printer') === '__custom__'),
            ])
            ->statePath('data');
    }

    public function save(GeneralSettings $settings): void
    {
        $data = $this->form->getState();

        if (Arr::get($data, 'selected_printer') !== '__custom__') {
            $data['custom_printer_address'] = null;
        }

        $data['new_image_timespan_minutes'] = (int) Arr::get($data, 'new_image_timespan_minutes', 0);
        $data['image_refresh_interval'] = (int) Arr::get($data, 'image_refresh_interval', 0);
        $data['max_number_of_copies'] = (int) Arr::get($data, 'max_number_of_copies', 0);
        $data['show_print_button'] = (bool) Arr::get($data, 'show_print_button', false);
        $data['custom_printer_address'] = $data['custom_printer_address'] ?: null;

        $settings->fill($data)->save();

        Notification::make()
            ->title(__('settings.saved_successfully'))
            ->success()
            ->send();
    }

    protected function getFormActions(): array
    {
        return [
            \Filament\Actions\Action::make('save')
                ->label(__('settings.save_button'))
                ->submit('save'),
        ];
    }
}
