import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../../providers/player_engine_override_provider.dart';
import '../../../providers/player_settings_provider.dart';
import '../../../providers/version_priority_provider.dart';
import '../../player/gestures/gesture_action.dart' as g;
import '../widgets/player_engine_row.dart';
import '../widgets/settings_card.dart';
import '../widgets/settings_rows.dart';
import '../widgets/version_priority_section.dart';

class PlaybackSettingsTab extends ConsumerWidget {
  const PlaybackSettingsTab({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final playerSettings = ref.watch(playerSettingsProvider);
    final versionPriority = ref.watch(versionPriorityProvider);
    final engineOverride = ref.watch(playerEngineOverrideProvider);
    final theme = Theme.of(context);

    return SettingsTabScroll(
      children: [
        engineOverride.when(
          data: (current) => SettingsCard(
            children: [
              PlayerEngineRow(
                value: current,
                onChanged: (v) => ref
                    .read(playerEngineOverrideProvider.notifier)
                    .setOverride(v),
              ),
            ],
          ),
          loading: () => const SizedBox.shrink(),
          error: (e, _) => settingsLoadError(e),
        ),
        playerSettings.when(
          data: (data) => Column(
            children: [
              SettingsCard(
                children: [
                  SeekDurationRow(
                    title: '快退时间',
                    seconds: data.seekBackwardSeconds,
                    onChanged: (v) => ref
                        .read(playerSettingsProvider.notifier)
                        .setSeekBackwardSeconds(v),
                  ),
                  SeekDurationRow(
                    title: '快进时间',
                    seconds: data.seekForwardSeconds,
                    onChanged: (v) => ref
                        .read(playerSettingsProvider.notifier)
                        .setSeekForwardSeconds(v),
                  ),
                ],
              ),
              if (Platform.isAndroid)
                SettingsCard(
                  children: [
                    Padding(
                      padding: const EdgeInsets.fromLTRB(16, 10, 16, 4),
                      child: Text(
                        '触控',
                        style: theme.textTheme.labelLarge?.copyWith(
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                    ),
                    GestureSpeedRow(
                      title: '长按左侧倍速',
                      value: data.keyboardHoldLeftSpeed,
                      onChanged: (v) => ref
                          .read(playerSettingsProvider.notifier)
                          .setKeyboardHoldLeftSpeed(v),
                    ),
                    GestureSpeedRow(
                      title: '长按右侧倍速',
                      value: data.keyboardHoldRightSpeed,
                      onChanged: (v) => ref
                          .read(playerSettingsProvider.notifier)
                          .setKeyboardHoldRightSpeed(v),
                    ),
                  ],
                )
              else ...[
                SettingsCard(
                  children: [
                    Padding(
                      padding: const EdgeInsets.fromLTRB(16, 10, 16, 4),
                      child: Text(
                        '键盘',
                        style: theme.textTheme.labelLarge?.copyWith(
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                    ),
                    GestureSpeedRow(
                      title: '长按左方向键倍速',
                      value: data.keyboardHoldLeftSpeed,
                      onChanged: (v) => ref
                          .read(playerSettingsProvider.notifier)
                          .setKeyboardHoldLeftSpeed(v),
                    ),
                    GestureSpeedRow(
                      title: '长按右方向键倍速',
                      value: data.keyboardHoldRightSpeed,
                      onChanged: (v) => ref
                          .read(playerSettingsProvider.notifier)
                          .setKeyboardHoldRightSpeed(v),
                    ),
                  ],
                ),
                SettingsCard(
                  children: [
                    Padding(
                      padding: const EdgeInsets.fromLTRB(16, 10, 16, 4),
                      child: Text(
                        '鼠标',
                        style: theme.textTheme.labelLarge?.copyWith(
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                    ),
                    GestureActionRow(
                      title: '左键单击',
                      kind: g.GestureKind.leftClick,
                      value: data.mouseLeftClickAction,
                      onChanged: (v) => ref
                          .read(playerSettingsProvider.notifier)
                          .setMouseLeftClickAction(v),
                    ),
                    GestureActionRow(
                      title: '左键双击',
                      kind: g.GestureKind.leftDoubleClick,
                      value: data.mouseLeftDoubleClickAction,
                      onChanged: (v) => ref
                          .read(playerSettingsProvider.notifier)
                          .setMouseLeftDoubleClickAction(v),
                    ),
                    GestureActionRow(
                      title: '右键单击',
                      kind: g.GestureKind.rightClick,
                      value: data.mouseRightClickAction,
                      onChanged: (v) => ref
                          .read(playerSettingsProvider.notifier)
                          .setMouseRightClickAction(v),
                    ),
                    GestureActionRow(
                      title: '右键双击',
                      kind: g.GestureKind.rightDoubleClick,
                      value: data.mouseRightDoubleClickAction,
                      onChanged: (v) => ref
                          .read(playerSettingsProvider.notifier)
                          .setMouseRightDoubleClickAction(v),
                    ),
                  ],
                ),
              ],
            ],
          ),
          loading: () => const SizedBox.shrink(),
          error: (e, _) => settingsLoadError(e),
        ),
        versionPriority.when(
          data: (data) => SettingsCard(
            children: [
              Padding(
                padding: const EdgeInsets.symmetric(
                  horizontal: 16,
                  vertical: 12,
                ),
                child: VersionPrioritySection(
                  activePriorities: data.activePriorities,
                  onToggle: (p) =>
                      ref.read(versionPriorityProvider.notifier).toggle(p),
                  onReorder: (list) => ref
                      .read(versionPriorityProvider.notifier)
                      .setPriorities(list),
                ),
              ),
            ],
          ),
          loading: () => const SizedBox.shrink(),
          error: (e, _) => settingsLoadError(e),
        ),
      ],
    );
  }
}
