import 'dart:io';

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

import '../../../providers/audio_settings_provider.dart';
import '../../../shared/widgets/setting_select.dart';
import '../utils/settings_labels.dart';
import '../widgets/settings_card.dart';

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

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final audioSettings = ref.watch(audioSettingsProvider);

    return SettingsTabScroll(
      children: [
        audioSettings.when(
          data: (data) => Column(
            children: [
              SettingsCard(
                children: [
                  SettingRow(
                    title: '记住所选音轨',
                    trailing: FSwitch(
                      value: data.rememberAudioTrack,
                      onChange: (v) => ref
                          .read(audioSettingsProvider.notifier)
                          .setRememberAudioTrack(v),
                    ),
                  ),
                  SettingRow(
                    title: '音频首选语言',
                    trailing: SettingSelect<String>(
                      value: data.preferredAudioLanguage,
                      options: const ['', 'chi', 'eng', 'jpn'],
                      labelOf: audioLangLabel,
                      onChanged: (v) {
                        if (v != null) {
                          ref
                              .read(audioSettingsProvider.notifier)
                              .setPreferredAudioLanguage(v);
                        }
                      },
                    ),
                  ),
                ],
              ),
              if (!Platform.isAndroid)
                SettingsCard(
                  children: [
                    SettingRow(
                      title: '音频增强',
                      trailing: FSwitch(
                        value: data.volumeBoost,
                        onChange: (v) => ref
                            .read(audioSettingsProvider.notifier)
                            .setVolumeBoost(v),
                      ),
                    ),
                  ],
                ),
            ],
          ),
          loading: () => const SizedBox.shrink(),
          error: (e, _) => settingsLoadError(e),
        ),
      ],
    );
  }
}
