<?php

namespace App\Filament\Resources;

use App\Filament\Resources\BannerResource\Pages;
use App\Models\Banner;
use App\Service\DeepLService;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Group;
use Filament\Forms\Components\Radio;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use SolutionForest\FilamentTranslateField\Forms\Component\Translate;

class BannerResource extends Resource
{
    protected static ?string $model = Banner::class;

    protected static ?int $navigationSort = 1;

    protected static ?string $navigationIcon = 'heroicon-o-photo';

    protected static ?string $navigationGroup = '其他設定';

    // protected static ?string $navigationGroup = '上稿內容管理';
    protected static ?string $navigationLabel = '首頁 Banner 管理';

    protected static ?string $modelLabel = '首頁 Banner 管理';

    // 預設排序
    protected static function booted()
    {
        static::addGlobalScope('ordered', function ($query) {
            $query->orderBy('order')->orderBy('id');
        });
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                //
                Section::make('')->schema([
                    Group::make()->schema([
                        Radio::make('type')->label('')->default(1)->options([1 => '圖片', 2 => '影片'])->reactive(),
                        Translate::make()->schema(fn (string $locale) => [
                            Textarea::make('title')
                                ->required(fn ($get) => $locale == 'zh_TW' && $get('../../type') == 1)
                                ->label('大字標題'),
                            Textarea::make('content')
                                ->required(fn ($get) => $locale == 'zh_TW' && $get('../../type') == 1)
                                ->label('小字標題'),
                        ])
                            ->locales(['zh_TW', 'en'])
                            ->actions([
                                app(DeepLService::class)->createTranslationAction('Main', ['title', 'content']),
                            ])->columnSpanFull()
                            ->visible(fn ($get) => $get('type') == 1),
                        Group::make()->schema([
                            FileUpload::make('img_url')->label('圖片')->directory('banners')->columnSpan(1)
                                ->image()
                                ->optimize('webp')
                                ->maxImageWidth(1920)
                                ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])
                                ->required(fn ($get) => $get('type') == 1)
                                ->imageEditor()
                                ->visible(fn ($get) => $get('type') == 1),
                            FileUpload::make('mobile_img')->label('手機板圖片')->directory('banners')->columnSpan(1)
                                ->image()
                                ->optimize('webp')
                                ->maxImageWidth(1920)
                                ->acceptedFileTypes(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])
                                ->required(fn ($get) => $get('type') == 1)
                                ->imageEditor()
                                ->visible(fn ($get) => $get('type') == 1),
                            Translate::make()->schema(fn (string $locale) => [
                                TextInput::make('img_alt')
                                    ->required(fn ($get) => $locale == 'zh_TW' && $get('../../type') == 1)
                                    ->label('圖片註釋'),
                            ])
                                ->locales(['zh_TW', 'en'])
                                ->actions([
                                    app(DeepLService::class)->createTranslationAction('Main', ['img_alt']),
                                ])->columnSpan(1)
                                ->visible(fn ($get) => $get('../type') == 1),
                        ])->columnSpanFull(),
                        TextInput::make('video_url')->label('影片網址')->url()->columnSpanFull()
                            ->required(fn ($get) => $get('type') == 2)
                            ->visible(fn ($get) => $get('type') == 2),
                        Radio::make('visible')->label('顯示/不顯示')->options([1 => '顯示', 0 => '不顯示'])->inline()->default(1)->columnSpan(2),
                    ])->columns(4)->columnSpanFull(),
                ]),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                IconColumn::make('type')->label('類別')
                    ->icon(fn (string $state): string => match ($state) {
                        '1' => 'heroicon-o-photo',
                        '2' => 'heroicon-o-film',
                    }),
                ImageColumn::make('img_url')->label('圖片')
                    ->getStateUsing(fn ($record) => $record->type == 1 ? $record->img_url : null),
                TextColumn::make('title')->label('標題')
                    ->getStateUsing(fn ($record) => $record->type == 1 ? $record->title : null),
                TextColumn::make('video_url')->label('影片網址')
                    ->formatStateUsing(fn ($record) => $record->type == 2 ? $record->video_url : null),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ])
            ->reorderable('order')
            ->defaultSort('order');
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListBanners::route('/'),
            'create' => Pages\CreateBanner::route('/create'),
            'edit' => Pages\EditBanner::route('/{record}/edit'),
        ];
    }
}
