Source code

We have made the main source code publicly available. We hope it will be helpful as a reference for app development.

● Japanese ● ご注意 ● このページで公開しているのはDartコードのみです。アプリの動作には画像・音声・多言語テキストなどの追加データが必要なため、ここに掲載している情報だけではアプリを完全に再現することはできません。 ● アプリは継続的に機能拡張しているため、ストアで公開している最新版とコード内容が異なる場合があります。 ● コードはコピーして自由にご利用いただけますが、著作権は放棄していません。そのため、コード全体の再掲載はご遠慮ください。一部の引用や改変しての再掲載は問題ありません。個人利用・商用利用を問わず、アプリ開発の参考として自由にお使いください。 ● このコードは「お手本」として公開しているものではありません。ミニアプリとして作成しているため、変数名など細部に配慮していない箇所や誤りが含まれる可能性があります。参考程度にご覧ください。 ● 他の開発者の公開コードを参考にした部分も含まれています。アプリ開発の熟練者が書いたコードではない点もご了承ください。 ● ここはエンジニア向けの技術情報共有サービスではないため、詳細な説明は省略しています。 ● 本コードは無保証であり、動作や品質を保証するものではありません。
Copyright© エーオーシステム株式会社

● English ● Notice ● Only the Dart source code is provided on this page. Additional assets such as images, audio files, and multilingual text are required for the application to function. Therefore, the information here alone is not sufficient to fully reproduce the app. ● The app is continuously being improved, so the code published here may differ from the latest version available on the app stores. ● You are free to copy and use the code, but the copyright is not waived. For this reason, please refrain from redistributing (reposting) the entire code. Partial quotation or redistribution of modified portions is allowed. You may use the code freely for reference in your own app development, whether for personal or commercial use. ● This code is not intended to serve as a coding example or best practice. As this is a small sample app, some variable names and details may lack refinement, and there may be mistakes. Please use it only as a reference. ● Some parts of the code are based on publicly available examples from other developers. Please note that this code was not written by an expert in Android app development. ● This is not a technical knowledge-sharing service for engineers, so detailed explanations are omitted. ● The code is provided "as is" without any warranty of any kind.
Copyright© ao-system, Inc.

Last build date: 2026-05-28

name: fortuneslip
description: "fortuneslip"

publish_to: 'none'

version: 2.3.1+26

environment:
  sdk: ^3.11.5

dependencies:         # flutter pub upgrade --major-versions
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  shared_preferences: ^2.3.2
  flutter_tts: ^4.0.2
  google_mobile_ads: ^8.0.0
  just_audio: ^0.10.5
  cupertino_icons: ^1.0.8
  wakelock_plus: ^1.4.0
  in_app_review: ^2.0.11
  app_settings: ^7.0.0

dev_dependencies:
  flutter_lints: ^6.0.0
  flutter_launcher_icons: ^0.14.4    #flutter pub run flutter_launcher_icons
  flutter_native_splash: ^2.4.0     #flutter pub run flutter_native_splash:create

flutter_icons:
  android: "launcher_icon"
  ios: true
  image_path: "assets/icon/icon.png"
  adaptive_icon_background: "assets/icon/icon_back.png"
  adaptive_icon_foreground: "assets/icon/icon_fore.png"

flutter_native_splash:
  color: '#9da9f5'
  image: 'assets/image/splash.png'
  color_dark: '#9da9f5'
  image_dark: 'assets/image/splash.png'
  fullscreen: true
  android_12:
    icon_background_color: '#9da9f5'
    image: 'assets/image/splash.png'
    icon_background_color_dark: '#9da9f5'
    image_dark: 'assets/image/splash.png'

flutter:
  generate: true
  uses-material-design: true
  config:
    enable-swift-package-manager: true

  assets:
    - assets/image/
    - assets/image/fortune/
    - assets/sound/

/// Copyright© ao-system, Inc.

import 'package:flutter/cupertino.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';

import 'package:fortuneslip/ad_manager.dart';

class AdBannerWidget extends StatefulWidget {
  final AdManager adManager;
  const AdBannerWidget({super.key, required this.adManager});
  @override
  State<AdBannerWidget> createState() => _AdBannerWidgetState();
}

class _AdBannerWidgetState extends State<AdBannerWidget> {
  int _lastBannerWidthDp = 0;
  bool _isAdLoaded = false;
  bool _isLoading = false;
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: LayoutBuilder(
        builder: (context, constraints) {
          final int width = constraints.maxWidth.isFinite ? constraints.maxWidth.truncate() : MediaQuery.of(context).size.width.truncate();
          final bannerAd = widget.adManager.bannerAd;
          if (width > 0) {
            WidgetsBinding.instance.addPostFrameCallback((_) {
              if (mounted) {
                final bannerAd = widget.adManager.bannerAd;
                final bool widthChanged = _lastBannerWidthDp != width;
                final bool sizeMismatch = bannerAd == null || bannerAd.size.width != width;
                if ((widthChanged || !_isAdLoaded || sizeMismatch) && !_isLoading) {
                  _lastBannerWidthDp = width;
                  setState(() { _isAdLoaded = false; _isLoading = true; });
                  widget.adManager.loadAdaptiveBannerAd(width, () {
                    if (mounted) {
                      setState(() { _isAdLoaded = true; _isLoading = false; });
                    }
                  });
                }
              }
            });
          }
          if (_isAdLoaded && bannerAd != null) {
            return Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                SizedBox(height: 10),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    SizedBox(
                      width: bannerAd.size.width.toDouble(),
                      height: bannerAd.size.height.toDouble(),
                      child: AdWidget(ad: bannerAd),
                    ),
                  ],
                )
              ]
            );
          } else {
            return const SizedBox.shrink();
          }
        },
      ),
    );
  }
}
/// Copyright© ao-system, Inc.

import 'dart:async';
import 'dart:io' show Platform;
import 'dart:ui';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:flutter/widgets.dart';

import 'package:fortuneslip/_secrets.dart';

class AdManager {
  static String get _adUnitId => Platform.isIOS ? Secrets.adUnitIdIos : Secrets.adUnitIdAndroid;
  BannerAd? _bannerAd;
  int _lastWidthPx = 0;
  VoidCallback? _onLoadedCb;
  Timer? _retryTimer;
  int _retryAttempt = 0;

  BannerAd? get bannerAd => _bannerAd;

  /// アプリ起動時の設定
  /// UMP(同意管理)を導入したため、手動のNPA設定は不要になった。
  static Future<void> initForNPA() async {
    if (kIsWeb) {
      return;
    }
    // UMP SDK が保存した同意情報を MobileAds SDK が自動で読み取るため、
    // ここで RequestConfiguration を使って NPA を強制する必要はない。
    await MobileAds.instance.updateRequestConfiguration(
      RequestConfiguration(
        tagForChildDirectedTreatment: TagForChildDirectedTreatment.unspecified,
        testDeviceIds: Secrets.umpConsentTestDeviceIds, //テストデバイスID:広告の誤クリック防止
      ),
    );
  }

  Future<void> loadAdaptiveBannerAd(int widthPx, VoidCallback onAdLoaded) async {
    if (kIsWeb) {
      return;
    }
    _onLoadedCb = onAdLoaded;
    _lastWidthPx = widthPx;
    _retryAttempt = 0;
    _retryTimer?.cancel();
    _retryTimer = null;
    _startLoad(widthPx);
  }

  static AdRequest getAdRequest() {
    // ユーザーの同意状態(TCF信号)は、SDKによって自動的に付与される。
    // 手動で npa: 1 を送ると、UMPでのユーザーの選択と競合する可能性があるため、空で返す。
    // AdRequest(nonPersonalizedAds: true);にはしない
    return const AdRequest();
  }

  Future<void> _startLoad(int widthPx) async {
    if (kIsWeb) {
      return;
    }
    _bannerAd?.dispose();
    AnchoredAdaptiveBannerAdSize? adaptiveSize;
    try {
      adaptiveSize = await AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(widthPx);
    } catch (_) {
      adaptiveSize = null;
    }
    final AdSize size = adaptiveSize ?? AdSize.fullBanner;
    _bannerAd = BannerAd(
      adUnitId: _adUnitId,
      request: getAdRequest(),
      size: size,
      listener: BannerAdListener(
        onAdLoaded: (ad) {
          _retryTimer?.cancel();
          _retryTimer = null;
          _retryAttempt = 0;
          final cb = _onLoadedCb;
          if (cb != null) {
            cb();
          }
        },
        onAdFailedToLoad: (ad, err) {
          ad.dispose();
          _scheduleRetry();
        },
      ),
    )..load();
  }

  void _scheduleRetry() {
    if (kIsWeb) return;
    _retryTimer?.cancel();
    _retryTimer = null;
    _retryAttempt = (_retryAttempt + 1).clamp(1, 5);
    final seconds = _retryAttempt >= 4 ? 30 : (3 << (_retryAttempt - 1));
    _retryTimer = Timer(Duration(seconds: seconds), () {
      _startLoad(_lastWidthPx > 0 ? _lastWidthPx : 320);
    });
  }

  void dispose() {
    _bannerAd?.dispose();
    _retryTimer?.cancel();
    _retryTimer = null;
  }
}
/// Copyright© ao-system, Inc.

import 'dart:async';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:flutter/widgets.dart';

import 'package:fortuneslip/l10n/app_localizations.dart';
import 'package:fortuneslip/_secrets.dart';

/// UMP状態格納用
class AdUmpState {
  final PrivacyOptionsRequirementStatus privacyStatus;
  final ConsentStatus consentStatus;
  final bool privacyOptionsRequired;
  final bool isChecking;
  const AdUmpState({
    required this.privacyStatus,
    required this.consentStatus,
    required this.privacyOptionsRequired,
    required this.isChecking,
  });

  AdUmpState copyWith({
    PrivacyOptionsRequirementStatus? privacyStatus,
    ConsentStatus? consentStatus,
    bool? privacyOptionsRequired,
    bool? isChecking,
  }) {
    return AdUmpState(
      privacyStatus: privacyStatus ?? this.privacyStatus,
      consentStatus: consentStatus ?? this.consentStatus,
      privacyOptionsRequired:
      privacyOptionsRequired ?? this.privacyOptionsRequired,
      isChecking: isChecking ?? this.isChecking,
    );
  }

  static const initial = AdUmpState(
    privacyStatus: PrivacyOptionsRequirementStatus.unknown,
    consentStatus: ConsentStatus.unknown,
    privacyOptionsRequired: false,
    isChecking: false,
  );
}

//UMPコントローラ
class AdUmpConsentController {
  //デバッグ用:同意フォームの表示テスト:EEA地域を強制する(本番ではfalseにすること)
  final bool forceEeaForDebug = false;
  //デバッグ用:同意フォームの表示テスト:EEA地域を強制するテストデバイスID
  static final List<String> _testDeviceIds = Secrets.umpConsentTestDeviceIds;

  ConsentRequestParameters _buildParams() {
    if (forceEeaForDebug && _testDeviceIds.isNotEmpty) {
      return ConsentRequestParameters(
        consentDebugSettings: ConsentDebugSettings(
          debugGeography: DebugGeography.debugGeographyEea,
          testIdentifiers: _testDeviceIds,
        ),
      );
    }
    return ConsentRequestParameters();
  }

  //同意情報を更新して状態を返す
  Future<AdUmpState> updateConsentInfo({AdUmpState current = AdUmpState.initial}) async {
    if (kIsWeb) {
      return current;
    }
    var state = current.copyWith(isChecking: true);

    try {
      final params = _buildParams();
      final completer = Completer<AdUmpState>();

      ConsentInformation.instance.requestConsentInfoUpdate(
        params,
        () async {
          //同意フォームが必要なら表示する
          ConsentForm.loadAndShowConsentFormIfRequired((formError) async {
            final s = await ConsentInformation.instance.getPrivacyOptionsRequirementStatus();
            final c = await ConsentInformation.instance.getConsentStatus();
            completer.complete(
              state.copyWith(
                privacyStatus: s,
                consentStatus: c,
                privacyOptionsRequired: s == PrivacyOptionsRequirementStatus.required,
                isChecking: false,
              ),
            );
          });
        },
        (FormError e) {
          completer.complete(state.copyWith(isChecking: false));
        },
      );
      return await completer.future;
    } catch (_) {
      return state.copyWith(isChecking: false);
    }
  }

  //プライバシーオプションフォームを表示
  Future<FormError?> showPrivacyOptions() async {
    if (kIsWeb) return null;
    final completer = Completer<FormError?>();
    ConsentForm.showPrivacyOptionsForm((FormError? e) {
      completer.complete(e);
    });
    return completer.future;
  }
}

class AdUmpService {
  final AdUmpConsentController _adUmpConsentController = AdUmpConsentController();

  Future<AdUmpState> updateConsentInfo(AdUmpState current) async {
    return await _adUmpConsentController.updateConsentInfo(current: current);
  }

  Future<void> requestConsentInfoUpdate(ConsentRequestParameters params) async {
    final completer = Completer<void>();
    ConsentInformation.instance.requestConsentInfoUpdate(
      params,
          () => completer.complete(),
          (FormError error) => completer.completeError(error),
    );
    return completer.future;
  }

  Future<FormError?> showPrivacyOptions() async {
    return await _adUmpConsentController.showPrivacyOptions();
  }
}

extension ConsentStatusL10n on ConsentStatus {
  String localized(BuildContext context) {
    final l = AppLocalizations.of(context)!;
    switch (this) {
      case ConsentStatus.obtained:
        return l.cmpConsentStatusObtained;
      case ConsentStatus.required:
        return l.cmpConsentStatusRequired;
      case ConsentStatus.notRequired:
        return l.cmpConsentStatusNotRequired;
      case ConsentStatus.unknown:
        return l.cmpConsentStatusUnknown;
    }
  }
}
/// Copyright© ao-system, Inc.

import 'package:flutter/services.dart';

///App Tracking Transparency サービス
///iOS 14以降で広告トラッキングの許可をリクエストする
class AttService {
  //チャンネル名をiOS側と一致させる
  static const _channel = MethodChannel('aosystem.att');

  static final AttService _instance = AttService._internal();
  factory AttService() => _instance;
  AttService._internal();

  ///トラッキング許可をリクエスト
  ///戻り値: AttStatus (enum)
  Future<AttStatus> requestTracking() async {
    try {
      //iOS側からInt値を受け取る
      final result = await _channel.invokeMethod<int>('requestTracking');
      //enumに変換して返却
      return parseAttStatus(result);
    } on MissingPluginException catch (_) {
      //ハンドラ未登録(iOS以外/旧バイナリ等)はアプリ全体を落とさない
      return AttStatus.unknown;
    } on PlatformException catch (_) {
      return AttStatus.unknown;
    }
  }

  ///現在のトラッキング許可状態を取得
  ///戻り値:AttStatus(enum)
  Future<AttStatus> getTrackingStatus() async {
    try {
      //iOS側からInt値を受け取る
      final result = await _channel.invokeMethod<int>('getTrackingStatus');
      //enumに変換して返却
      return parseAttStatus(result);
    } on MissingPluginException catch (_) {
      return AttStatus.unknown;
    } on PlatformException catch (_) {
      return AttStatus.unknown;
    }
  }

  ///トラッキングが許可されているか
  Future<bool> isTrackingAuthorized() async {
    final status = await getTrackingStatus();
    return status == AttStatus.authorized;
  }
}

//一緒に利用する enum とヘルパー関数

enum AttStatus {
  notDetermined, // 0
  restricted,    // 1
  denied,        // 2
  authorized,    // 3
  unknown,       // 4
}

AttStatus parseAttStatus(int? value) {
  switch (value) {
    case 0:
      return AttStatus.notDetermined;
    case 1:
      return AttStatus.restricted;
    case 2:
      return AttStatus.denied;
    case 3:
      return AttStatus.authorized;
    default:
      return AttStatus.unknown;
  }
}
/// Copyright© ao-system, Inc.


import 'package:just_audio/just_audio.dart';

import 'package:fortuneslip/model.dart';

class AudioPlay {
  late AudioPlayer _audioPlayer;

  void dispose() {
    _audioPlayer.stop();
    _audioPlayer.dispose();
  }

  void play() {
    if (Model.soundVolume == 0) {
      return;
    }
    _audioPlayer = AudioPlayer();
    _audioPlayer.setAsset('assets/sound/switch.wav');
    _audioPlayer.setVolume(Model.soundVolume);
    _audioPlayer.seek(Duration.zero);
    _audioPlayer.play();
  }

}
/// Copyright© ao-system, Inc.

import 'dart:math';

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

class FortuneItem {
  const FortuneItem({required this.rawName, required this.ratio});

  final String rawName;
  final int ratio;

  String get display {
    final index = rawName.indexOf(':');
    if (index == -1) {
      return rawName.trim();
    }
    return rawName.substring(0, index).trim();
  }

  String get reading {
    final index = rawName.indexOf(':');
    if (index == -1) {
      return rawName.trim();
    }
    return rawName.substring(index + 1).trim();
  }

  FortuneItem copyWith({String? rawName, int? ratio}) {
    return FortuneItem(
      rawName: rawName ?? this.rawName,
      ratio: ratio ?? this.ratio,
    );
  }
}

class FortuneSettings {
  FortuneSettings({
    required List<FortuneItem> items,
    required this.speakResult,
    required this.animationSpeed,
    required this.countdownTime,
    required this.themeMode,
    required this.locale,
    this.speechVoice = '',
    this.speechLocale = '',
  }) : items = List<FortuneItem>.from(items);

  final List<FortuneItem> items;
  final bool speakResult;
  final int animationSpeed;
  final int countdownTime;
  final ThemeMode themeMode;
  final Locale? locale;
  final String speechVoice;
  final String speechLocale;

  FortuneSettings copyWith({
    List<FortuneItem>? items,
    bool? speakResult,
    int? animationSpeed,
    int? countdownTime,
    ThemeMode? themeMode,
    Locale? locale,
    String? speechVoice,
    String? speechLocale,
  }) {
    return FortuneSettings(
      items: items ?? this.items,
      speakResult: speakResult ?? this.speakResult,
      animationSpeed: animationSpeed ?? this.animationSpeed,
      countdownTime: countdownTime ?? this.countdownTime,
      themeMode: themeMode ?? this.themeMode,
      locale: locale ?? this.locale,
      speechVoice: speechVoice ?? this.speechVoice,
      speechLocale: speechLocale ?? this.speechLocale,
    );
  }
}

class FortuneDefaults {
  static List<FortuneItem> forLocale(Locale? locale) {
    final code = locale?.languageCode;
    if (code == 'ja') {
      return _jaDefaults;
    }
    return _enDefaults;
  }

  static List<FortuneItem> get _enDefaults => const [
        FortuneItem(rawName: 'Great blessing', ratio: 1),
        FortuneItem(rawName: 'Blessing', ratio: 3),
        FortuneItem(rawName: 'Middle blessing', ratio: 5),
        FortuneItem(rawName: 'Small blessing', ratio: 5),
        FortuneItem(rawName: 'Uncertain luck', ratio: 5),
        FortuneItem(rawName: 'Curse', ratio: 1),
        FortuneItem(rawName: 'Great curse', ratio: 1),
      ];

  static List<FortuneItem> get _jaDefaults => const [
        FortuneItem(rawName: '大吉:だいきち', ratio: 1),
        FortuneItem(rawName: '吉:きち', ratio: 3),
        FortuneItem(rawName: '中吉:ちゅうきち', ratio: 5),
        FortuneItem(rawName: '小吉:しょうきち', ratio: 5),
        FortuneItem(rawName: '末吉:すえきち', ratio: 5),
        FortuneItem(rawName: '凶:きょう', ratio: 1),
        FortuneItem(rawName: '大凶:だいきょう', ratio: 1),
      ];
}

class FortuneStorage {
  static const _speechNumberKey = 'speechNumber';
  static const _speechVoiceKey = 'speechVoice';
  static const _speechLocaleKey = 'speechLocale';
  static const _animationSpeedKey = 'animationSpeed';
  static const _countdownTimeKey = 'countdownTime';
  static const _themeNumberKey = 'themeNumber';
  static const _localeLanguageKey = 'localeLanguage';

  Future<FortuneSettings> load() async {
    final prefs = await SharedPreferences.getInstance();
    final localeCode = prefs.getString(_localeLanguageKey) ?? '';
    final locale = localeCode.isEmpty ? null : Locale(localeCode);
    final items = <FortuneItem>[];
    for (var i = 1; i <= 7; i++) {
      final name = prefs.getString('itemName$i') ?? '';
      final ratio = prefs.getInt('itemRatio$i') ?? 0;
      items.add(FortuneItem(rawName: name, ratio: max(0, ratio)));
    }
    final allNamesEmpty = items.every((item) => item.rawName.isEmpty);
    final effectiveItems = allNamesEmpty
        ? FortuneDefaults.forLocale(locale ?? WidgetsBinding.instance.platformDispatcher.locale)
        : items;

    final speakResult = (prefs.getInt(_speechNumberKey) ?? 1) != 0;
    final speechVoice = prefs.getString(_speechVoiceKey) ?? '';
    final speechLocale = prefs.getString(_speechLocaleKey) ?? '';
    final animationSpeed = 4000;
    final countdownTime = (prefs.getInt(_countdownTimeKey) ?? 3).clamp(0, 9);
    final themeValue = prefs.getInt(_themeNumberKey);
    final themeMode = switch (themeValue) {
      0 => ThemeMode.light,
      1 => ThemeMode.dark,
      2 => ThemeMode.system,
      null => ThemeMode.system,
      _ => ThemeMode.system,
    };

    return FortuneSettings(
      items: effectiveItems,
      speakResult: speakResult,
      animationSpeed: animationSpeed,
      countdownTime: countdownTime,
      themeMode: themeMode,
      locale: locale,
      speechVoice: speechVoice,
      speechLocale: speechLocale,
    );
  }

  Future<void> save(FortuneSettings settings) async {
    final prefs = await SharedPreferences.getInstance();
    for (var i = 0; i < settings.items.length; i++) {
      final item = settings.items[i];
      final index = i + 1;
      await prefs.setString('itemName$index', item.rawName);
      await prefs.setInt('itemRatio$index', item.ratio);
    }
    await prefs.setInt(_speechNumberKey, settings.speakResult ? 1 : 0);
    if (settings.speechVoice.isEmpty && settings.speechLocale.isEmpty) {
      await prefs.remove(_speechVoiceKey);
      await prefs.remove(_speechLocaleKey);
    } else {
      await prefs.setString(_speechVoiceKey, settings.speechVoice);
      await prefs.setString(_speechLocaleKey, settings.speechLocale);
    }
    await prefs.setInt(_animationSpeedKey, 4000);
    await prefs.setInt(_countdownTimeKey, settings.countdownTime.clamp(0, 9));
    final themeValue = switch (settings.themeMode) {
      ThemeMode.light => 0,
      ThemeMode.dark => 1,
      ThemeMode.system => 2,
    };
    await prefs.setInt(_themeNumberKey, themeValue);
    if (settings.locale == null) {
      await prefs.remove(_localeLanguageKey);
    } else {
      await prefs.setString(_localeLanguageKey, settings.locale!.languageCode);
    }
  }
}
/// Copyright© ao-system, Inc.

class FortuneItem {
  const FortuneItem({required this.rawName, required this.ratio});

  final String rawName;
  final int ratio;

  String get display {
    final index = rawName.indexOf(':');
    if (index == -1) {
      return rawName.trim();
    }
    return rawName.substring(0, index).trim();
  }

  String get reading {
    final index = rawName.indexOf(':');
    if (index == -1) {
      return rawName.trim();
    }
    return rawName.substring(index + 1).trim();
  }

  FortuneItem copyWith({String? rawName, int? ratio}) {
    return FortuneItem(
      rawName: rawName ?? this.rawName,
      ratio: ratio ?? this.ratio,
    );
  }
}
/// Copyright© ao-system, Inc.

import 'dart:async';
import 'dart:math';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:wakelock_plus/wakelock_plus.dart';

import 'package:fortuneslip/l10n/app_localizations.dart';
import 'package:fortuneslip/setting_page.dart';
import 'package:fortuneslip/ad_banner_widget.dart';
import 'package:fortuneslip/audio_play.dart';
import 'package:fortuneslip/text_to_speech.dart';
import 'package:fortuneslip/theme_color.dart';
import 'package:fortuneslip/fortune_item.dart';
import 'package:fortuneslip/model.dart';
import 'package:fortuneslip/loading_screen.dart';
import 'package:fortuneslip/main.dart';

class MainHomePage extends StatefulWidget {
  const MainHomePage({super.key});
  @override
  State<MainHomePage> createState() => _MainHomePageState();
}

class _MainHomePageState extends State<MainHomePage> with TickerProviderStateMixin, WidgetsBindingObserver {
  late AudioPlay _audioPlay;
  late Random _random;
  late ThemeColor _themeColor;
  bool _isReady = false;
  //
  late List<FortuneItem> _fortuneItems;
  static const int _frameCount = 120;
  static const List<_TextSpec> _textSpecs = [
    _TextSpec(frame: 84, x: 547, y: 440),
    _TextSpec(frame: 85, x: 549, y: 444),
    _TextSpec(frame: 86, x: 550, y: 450),
    _TextSpec(frame: 87, x: 550, y: 454),
    _TextSpec(frame: 88, x: 552, y: 459),
    _TextSpec(frame: 89, x: 554, y: 464),
    _TextSpec(frame: 90, x: 555, y: 469),
    _TextSpec(frame: 91, x: 556, y: 474),
    _TextSpec(frame: 92, x: 558, y: 479),
    _TextSpec(frame: 93, x: 560, y: 484),
    _TextSpec(frame: 94, x: 562, y: 489),
    _TextSpec(frame: 95, x: 563, y: 495),
    _TextSpec(frame: 96, x: 565, y: 500),
    _TextSpec(frame: 97, x: 567, y: 505),
    _TextSpec(frame: 98, x: 569, y: 511),
    _TextSpec(frame: 99, x: 571, y: 516),
    _TextSpec(frame: 100, x: 573, y: 522),
    _TextSpec(frame: 101, x: 575, y: 530),
    _TextSpec(frame: 102, x: 578, y: 539),
    _TextSpec(frame: 103, x: 580, y: 550),
    _TextSpec(frame: 104, x: 583, y: 563),
    _TextSpec(frame: 105, x: 587, y: 576),
    _TextSpec(frame: 106, x: 590, y: 592),
    _TextSpec(frame: 107, x: 594, y: 608),
    _TextSpec(frame: 108, x: 599, y: 626),
    _TextSpec(frame: 109, x: 604, y: 645),
    _TextSpec(frame: 110, x: 608, y: 665),
    _TextSpec(frame: 111, x: 614, y: 687),
    _TextSpec(frame: 112, x: 619, y: 708),
    _TextSpec(frame: 113, x: 625, y: 730),
    _TextSpec(frame: 114, x: 630, y: 751),
    _TextSpec(frame: 115, x: 636, y: 772),
    _TextSpec(frame: 116, x: 641, y: 792),
    _TextSpec(frame: 117, x: 646, y: 808),
    _TextSpec(frame: 118, x: 651, y: 822),
    _TextSpec(frame: 119, x: 654, y: 832),
  ];
  static const int _countdownFramesPerDigit = 30;
  static const Duration _countdownFrameInterval = Duration(milliseconds: 30);
  static const Duration _ticketFrameDuration = Duration(milliseconds: 40);

  Timer? _animationTimer;
  Timer? _countdownTimer;
  int _currentFrame = 0;
  int? _countdown;
  String? _countdownAsset;
  double _countdownScale = 1.1;
  double _countdownOpacity = 0;
  int _countdownFrames = 0;
  int _countdownValue = 0;
  List<ui.Image> _decodedFrames = [];
  Future<void>? _frameLoadFuture;
  bool _framesReady = false;
  Future<void>? _countdownPrecacheFuture;
  FortuneItem? _activeFortune;
  bool _isAnimating = false;

  bool get _isBusy => _isAnimating || _countdown != null;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    _initState();
  }

  void _initState() async {
    _themeColor = ThemeColor(themeNumber: Model.themeNumber, context: context);
    _wakelock();
    _audioPlay = AudioPlay();
    _random = Random();
    await TextToSpeech.applyPreferences(Model.ttsVoiceId,Model.ttsVolume);
    _fortuneItems = [
      FortuneItem(rawName: Model.fortune1, ratio: Model.ratio1),
      FortuneItem(rawName: Model.fortune2, ratio: Model.ratio2),
      FortuneItem(rawName: Model.fortune3, ratio: Model.ratio3),
      FortuneItem(rawName: Model.fortune4, ratio: Model.ratio4),
      FortuneItem(rawName: Model.fortune5, ratio: Model.ratio5),
      FortuneItem(rawName: Model.fortune6, ratio: Model.ratio6),
    ];
    _frameLoadFuture = _loadAnimationFrames();
    if (mounted) {
      setState(() {
        _isReady = true;
      });
    }
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    WakelockPlus.disable();
    _animationTimer?.cancel();
    _countdownTimer?.cancel();
    unawaited(TextToSpeech.stop());
    for (final image in _decodedFrames) {
      image.dispose();
    }
    _decodedFrames = [];
    super.dispose();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _countdownPrecacheFuture ??= _precacheCountdownAssets(context);
  }

  void _wakelock() {
    if (Model.wakelockEnabled) {
      WakelockPlus.enable();
    } else {
      WakelockPlus.disable();
    }
  }

  void _handleTap() {
    if (_isBusy) {
      return;
    }
    final selected = _selectFortune();
    if (selected == null) {
      final messenger = ScaffoldMessenger.of(context);
      messenger.hideCurrentSnackBar();
      messenger.showSnackBar(
        SnackBar(content: Text(AppLocalizations.of(context)!.empty)),
      );
      return;
    }
    setState(() {
      _activeFortune = selected;
      _currentFrame = 0;
      _isAnimating = false;
    });

    final int countdownTarget = Model.countdownTime;
    if (countdownTarget <= 0) {
      setState(() {
        _countdown = null;
        _countdownAsset = null;
        _countdownOpacity = 0;
      });
      unawaited(_startAnimation());
      return;
    }

    setState(() {
      _audioPlay.play();
      _countdownValue = countdownTarget;
      _countdown = _countdownValue;
      _countdownFrames = _countdownFramesPerDigit;
      _countdownAsset = _countdownAssetFor(_countdownValue);
      _countdownScale = 1.1;
      _countdownOpacity = 0;
    });
    _countdownTimer?.cancel();
    _countdownTimer = Timer.periodic(_countdownFrameInterval, (timer) {
      if (!mounted) {
        timer.cancel();
        return;
      }
      setState(() {
        _countdownFrames -= 1;
        if (_countdownFrames <= 0) {
          _countdownValue -= 1;
          if (_countdownValue <= 0) {
            _countdown = null;
            _countdownAsset = null;
            _countdownOpacity = 0;
            timer.cancel();
            unawaited(_startAnimation());
            return;
          }
          _countdown = _countdownValue;
          _countdownFrames = _countdownFramesPerDigit;
          _countdownAsset = _countdownAssetFor(_countdownValue);
        }

        final frame = _countdownFrames.toDouble();
        _countdownScale = 1 + 0.1 * (frame / _countdownFramesPerDigit);
        if (frame >= 20) {
          _countdownOpacity = (_countdownFramesPerDigit - frame) / 10;
        } else if (frame <= 5) {
          _countdownOpacity = frame / 5;
        } else {
          _countdownOpacity = 1;
        }
        if (_countdownOpacity < 0) {
          _countdownOpacity = 0;
        } else if (_countdownOpacity > 1) {
          _countdownOpacity = 1;
        }
      });
    });
  }

  Future<void> _precacheCountdownAssets(BuildContext context) async {
    final ctx = context;
    final futures = <Future<void>>[];
    for (var i = 1; i <= 9; i++) {
      // ignore: use_build_context_synchronously
      futures.add(precacheImage(AssetImage(_countdownAssetFor(i)), ctx));
    }
    // ignore: use_build_context_synchronously
    futures.add(
      precacheImage(const AssetImage('assets/image/number_null.webp'), ctx),
    );
    try {
      await Future.wait(futures);
    } catch (_) {
      // Ignore errors; countdown images will fall back to on-demand decoding.
    }
  }

  Future<void> _loadAnimationFrames() async {
    final frames = <ui.Image>[];
    try {
      for (var i = 0; i < _frameCount; i++) {
        final data = await rootBundle.load(_frameAsset(i));
        final codec = await ui.instantiateImageCodec(data.buffer.asUint8List());
        try {
          final frame = await codec.getNextFrame();
          frames.add(frame.image);
        } finally {
          codec.dispose();
        }
      }
      if (!mounted) {
        for (final image in frames) {
          image.dispose();
        }
        return;
      }
      setState(() {
        _decodedFrames = frames;
        _framesReady = true;
      });
    } catch (_) {
      for (final image in frames) {
        image.dispose();
      }
      if (mounted) {
        setState(() {
          _decodedFrames = [];
          _framesReady = true;
        });
      } else {
        _framesReady = true;
      }
    }
  }

  Future<void> _ensureFramesReady() async {
    if (_framesReady) {
      return;
    }
    final future = _frameLoadFuture;
    if (future != null) {
      try {
        await future;
      } catch (_) {
        // Ignore errors and fall back to asset-based rendering.
      }
    }
  }

  Future<void> _startAnimation() async {
    _animationTimer?.cancel();
    await _ensureFramesReady();
    if (!mounted) {
      return;
    }
    setState(() {
      _isAnimating = true;
      _currentFrame = 0;
    });
    _animationTimer = Timer.periodic(_ticketFrameDuration, (timer) {
      if (!mounted) {
        timer.cancel();
        return;
      }
      setState(() {
        if (_currentFrame >= _frameCount - 1) {
          timer.cancel();
          _isAnimating = false;
          _currentFrame = _frameCount - 1;
          _speakResult();
        } else {
          _currentFrame += 1;
        }
      });
    });
  }

  FortuneItem? _selectFortune() {
    final candidates = _fortuneItems
        .where((item) => item.ratio > 0 && item.display.isNotEmpty)
        .toList();
    final total = candidates.fold<int>(0, (value, item) => value + item.ratio);
    if (total == 0) {
      return null;
    }
    var remain = _random.nextInt(total);
    for (final item in candidates) {
      remain -= item.ratio;
      if (remain < 0) {
        return item;
      }
    }
    return candidates.isNotEmpty ? candidates.last : null;
  }

  Future<void> _speakResult() async {
    if (!Model.ttsEnabled || _activeFortune == null) {
      return;
    }
    final text = _activeFortune!.reading;
    if (text.isEmpty) {
      return;
    }
    await TextToSpeech.speak(text);
  }

  Future<void> _openSetting() async {
    if (_isBusy) {
      return;
    }
    final updated = await Navigator.push<bool>(
      context,
      MaterialPageRoute(builder: (_) => const SettingPage()),
    );
    if (mounted && updated == true) {
      MainApp.of(context).rebuildApp();
      _themeColor = ThemeColor(themeNumber: Model.themeNumber, context: context);
      _wakelock();
      await TextToSpeech.applyPreferences(Model.ttsVoiceId,Model.ttsVolume);
      _fortuneItems = [
        FortuneItem(rawName: Model.fortune1, ratio: Model.ratio1),
        FortuneItem(rawName: Model.fortune2, ratio: Model.ratio2),
        FortuneItem(rawName: Model.fortune3, ratio: Model.ratio3),
        FortuneItem(rawName: Model.fortune4, ratio: Model.ratio4),
        FortuneItem(rawName: Model.fortune5, ratio: Model.ratio5),
        FortuneItem(rawName: Model.fortune6, ratio: Model.ratio6),
      ];
    }
    if (mounted) {
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    if (_isReady == false) {
      return const LoadingScreen();
    }
    final l = AppLocalizations.of(context)!;
    return Scaffold(
      backgroundColor: _themeColor.mainBackColor,
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        elevation: 0,
        title: Opacity(
          opacity: _isBusy ? 0.1 : 1,
          child: Text(
            l.tapToDraw,
            style: Theme.of(context).textTheme.bodySmall?.copyWith(
              color: _themeColor.mainForeColor,
              overflow: TextOverflow.visible,
            ),
          ),
        ),
        centerTitle: true,
        actions: <Widget>[
          Opacity(
            opacity: _isBusy ? 0.1 : 1,
            child: IconButton(
              icon: const Icon(Icons.settings),
              color: _themeColor.mainForeColor,
              tooltip: l.setting,
              onPressed: _openSetting,
            ),
          ),
          const SizedBox(width: 10),
        ],
      ),
      body: GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: _handleTap,
        child: Container(
          color: _themeColor.mainBackColor,
          child: SafeArea(
            child: Column(
              children: [
                Expanded(
                  child: Stack(
                    children: [
                      Positioned.fill(child: _buildDrawingArea()),
                      if (_countdownAsset != null) _buildCountdownOverlay(),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
      bottomNavigationBar: AdBannerWidget(adManager: MainApp.of(context).adManager),
    );
  }

  Widget _buildDrawingArea() {
    return LayoutBuilder(
      builder: (context, constraints) {
        final width = constraints.maxWidth;
        final height = constraints.maxHeight;
        final boxSize = min(width, height);
        final marginLeft = (width - boxSize) / 2;
        final marginTop = (height - boxSize) / 2;
        final textLayout = _layoutForFrame(
          _currentFrame,
          boxSize,
          marginLeft,
          marginTop,
        );
        final imageAsset = _frameAsset(_currentFrame);
        final ui.Image? frameImage = _decodedFrames.isEmpty
            ? null
            : _decodedFrames[_currentFrame.clamp(0, _decodedFrames.length - 1)];
        return Stack(
          children: [
            Positioned(
              left: marginLeft,
              top: marginTop,
              width: boxSize,
              height: boxSize,
              child: DecoratedBox(
                decoration: BoxDecoration(
                  color: Colors.black.withValues(alpha: 0.1),
                  borderRadius: BorderRadius.circular(8),
                ),
                child: ClipRRect(
                  borderRadius: BorderRadius.circular(8),
                  child: frameImage != null
                      ? CustomPaint(
                          painter: _FramePainter(frameImage),
                          child: const SizedBox.expand(),
                        )
                      : Image.asset(
                          imageAsset,
                          fit: BoxFit.contain,
                          key: ValueKey<int>(_currentFrame),
                          gaplessPlayback: true,
                        ),
                ),
              ),
            ),
            if (textLayout != null && _activeFortune != null)
              Positioned(
                left: textLayout.offset.dx,
                top: textLayout.offset.dy,
                child: Opacity(
                  opacity: _currentFrame >= 84 ? 1 : 0,
                  child: Text(
                    _activeFortune!.display,
                    style: TextStyle(
                      fontSize: textLayout.fontSize,
                      color: Colors.white,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              ),
          ],
        );
      },
    );
  }

  Widget _buildCountdownOverlay() {
    final asset = _countdownAsset;
    if (asset == null) {
      return const SizedBox.shrink();
    }
    return Positioned.fill(
      child: Container(
        color: Colors.transparent,
        child: Center(
          child: Opacity(
            opacity: _countdownOpacity.clamp(0.0, 1.0),
            child: Transform.scale(
              scale: _countdownScale,
              child: Image.asset(asset),
            ),
          ),
        ),
      ),
    );
  }

  _TextLayout? _layoutForFrame(
    int frame,
    double boxSize,
    double marginLeft,
    double marginTop,
  ) {
    if (frame < 84) {
      return null;
    }
    final spec = _textSpecs.firstWhere(
      (element) => element.frame == frame,
      orElse: () => const _TextSpec(frame: -1, x: 0, y: 0),
    );
    if (spec.frame == -1) {
      return null;
    }
    final boxPixel = boxSize / 900.0;
    final dx = marginLeft + boxPixel * spec.x - boxPixel * ((frame - 84) + 36);
    final dy =
        marginTop + boxPixel * spec.y - boxPixel * ((frame - 84) * 1.3 + 24);
    final fontSize = ((frame - 84) / (119 - 84)) * 14 + 4;
    return _TextLayout(offset: Offset(dx, dy), fontSize: fontSize);
  }

  String _frameAsset(int frame) {
    final clamped = frame.clamp(0, 119) + 1;
    final padded = clamped.toString().padLeft(3, '0');
    return 'assets/image/fortune/omikuji$padded.jpg';
  }

  String _countdownAssetFor(int value) {
    var clamped = value;
    if (clamped <= 0) {
      return 'assets/image/number_null.webp';
    }
    if (clamped > 9) {
      clamped = 9;
    }
    return 'assets/image/number${clamped.toString()}.webp';
  }
}

class _FramePainter extends CustomPainter {
  const _FramePainter(this.image);

  final ui.Image image;

  @override
  void paint(Canvas canvas, Size size) {
    final paint = ui.Paint()..filterQuality = ui.FilterQuality.high;
    final imageWidth = image.width.toDouble();
    final imageHeight = image.height.toDouble();
    if (imageWidth == 0 ||
        imageHeight == 0 ||
        size.width == 0 ||
        size.height == 0) {
      return;
    }
    final imageAspect = imageWidth / imageHeight;
    final canvasAspect = size.width / size.height;
    Rect dst;
    if (imageAspect > canvasAspect) {
      final drawHeight = size.width / imageAspect;
      final dy = (size.height - drawHeight) / 2.0;
      dst = Rect.fromLTWH(0, dy, size.width, drawHeight);
    } else {
      final drawWidth = size.height * imageAspect;
      final dx = (size.width - drawWidth) / 2.0;
      dst = Rect.fromLTWH(dx, 0, drawWidth, size.height);
    }
    final src = Rect.fromLTWH(0, 0, imageWidth, imageHeight);
    canvas.drawImageRect(image, src, dst, paint);
  }

  @override
  bool shouldRepaint(covariant _FramePainter oldDelegate) {
    return oldDelegate.image != image;
  }
}

class _TextSpec {
  const _TextSpec({required this.frame, required this.x, required this.y});

  final int frame;
  final double x;
  final double y;
}

class _TextLayout {
  const _TextLayout({required this.offset, required this.fontSize});

  final Offset offset;
  final double fontSize;
}
/// Copyright© ao-system, Inc.

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

class LoadingScreen extends StatefulWidget {
  const LoadingScreen({super.key});

  @override
  State<LoadingScreen> createState() => _LoadingScreenState();
}

class _LoadingScreenState extends State<LoadingScreen> with SingleTickerProviderStateMixin {
  late AnimationController _animationController;

  @override
  void initState() {
    super.initState();
    final randomStart = Random().nextDouble();
    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 6),
      value: randomStart,
    )..repeat();
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  Color _rainbowColor(double value) {
    final hue = _animationController.value * 360;
    return HSVColor.fromAHSV(1, hue, 1, value).toColor();
  }

  @override
  Widget build(BuildContext context) {
    final barHeight = MediaQuery.of(context).size.height * 0.4;
    return AnimatedBuilder(
      animation: _animationController,
      builder: (context, _) {
        final foreColor = _rainbowColor(1.0);
        final backColor = _rainbowColor(0.08);
        return Scaffold(
          backgroundColor: backColor,
          body: Center(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                SizedBox(
                  height: barHeight,
                  child: RotatedBox(
                    quarterTurns: -1,
                    child: LinearProgressIndicator(
                      minHeight: 1,
                      valueColor: AlwaysStoppedAnimation(foreColor),
                      backgroundColor: Colors.transparent,
                    ),
                  ),
                ),
                const SizedBox(height: 5),
                Text(
                  'LOADING',
                  style: TextStyle(
                    color: foreColor,
                    fontSize: 18,
                    letterSpacing: 16,
                  ),
                ),
                const SizedBox(height: 5),
                SizedBox(
                  height: barHeight,
                  child: RotatedBox(
                    quarterTurns: 1,
                    child: LinearProgressIndicator(
                      minHeight: 1,
                      valueColor: AlwaysStoppedAnimation(foreColor),
                      backgroundColor: Colors.transparent,
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
/// Copyright© ao-system, Inc.

import 'dart:async';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';

import 'package:fortuneslip/l10n/app_localizations.dart';
import 'package:fortuneslip/model.dart';
import 'package:fortuneslip/home_page.dart';
import 'package:fortuneslip/theme_mode_number.dart';
import 'package:fortuneslip/loading_screen.dart';
import 'package:fortuneslip/parse_locale_tag.dart';
import 'package:fortuneslip/ad_ump_status.dart';
import 'package:fortuneslip/att_service.dart';
import 'package:fortuneslip/ad_manager.dart';


void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  //UI設定
  SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
  SystemChrome.setSystemUIOverlayStyle(
    const SystemUiOverlayStyle(
      systemNavigationBarColor: Colors.transparent,
      statusBarColor: Colors.transparent,
      systemNavigationBarContrastEnforced: false,
      systemStatusBarContrastEnforced: false,
    ),
  );
  runApp(const MainApp());
}

class MainApp extends StatefulWidget {
  const MainApp({super.key});

  static MainAppState of(BuildContext context) {
    return context.findAncestorStateOfType<MainAppState>()!;
  }

  @override
  State<MainApp> createState() => MainAppState();
}

class MainAppState extends State<MainApp> {
  late final AdManager adManager;
  ThemeMode _themeMode = ThemeMode.system;
  Locale? _locale;
  bool _hasError = false;
  bool _isReady = false;

  @override
  void initState() {
    super.initState();
    _initState();
  }

  void _initState() async {
    try {
      //ad
      adManager = AdManager();
      //アプリの基本データ
      await Model.ensureReady();
      //ATT
      //iOSは「アプリがactive/resumed状態」でないとrequestTrackingがダイアログを出さず即座にnotDeterminedを返すため、ライフサイクルがresumedになるまで待つ。
      //(iOSは「設定→トラッキング」でトグルを変えるとアプリプロセスをkillして再起動するので、起動時にgetTrackingStatusを読めば常に最新の値が手に入る)
      if (!kIsWeb && Platform.isIOS) {
        if (await _waitForResumed()) {
          final attService = AttService();
          //未決定(初回起動)のときだけダイアログ表示。既に決定済みならスキップ。
          if (await attService.getTrackingStatus() == AttStatus.notDetermined) {
            await attService.requestTracking();
          }
        }
      }
      //UMP(ATTの後)
      final adUmpConsentController = AdUmpConsentController();
      await adUmpConsentController.updateConsentInfo();
      //Mobile Ads SDK(同意確定後)
      await MobileAds.instance.initialize();
      //自前の広告設定
      await AdManager.initForNPA();
      //UI更新
      if (mounted) {
        setState(() {
          _themeMode = ThemeModeNumber.numberToThemeMode(Model.themeNumber);
          _locale = parseLocaleTag(Model.languageCode);
          _isReady = true;
        });
      }
    } catch (e) {
      if (mounted) {
        setState(() {
          _hasError = true;
        });
      }
    }
  }

  @override
  void dispose() {
    adManager.dispose();
    super.dispose();
  }

  //アプリがactive/resumed状態になるまで待つ。すでにresumedならすぐにtrueを返す。タイムアウト時はfalse。
  Future<bool> _waitForResumed({
    Duration timeout = const Duration(seconds: 5),
  }) async {
    final binding = WidgetsBinding.instance;
    if (binding.lifecycleState == AppLifecycleState.resumed) {
      return true;
    }
    final completer = Completer<bool>();
    late final AppLifecycleListener listener;
    listener = AppLifecycleListener(
      onStateChange: (state) {
        if (state == AppLifecycleState.resumed && !completer.isCompleted) {
          completer.complete(true);
        }
      },
    );
    try {
      return await completer.future.timeout(timeout, onTimeout: () => false);
    } finally {
      listener.dispose();
    }
  }

  void rebuildApp() {
    setState(() {
      _themeMode = ThemeModeNumber.numberToThemeMode(Model.themeNumber);
      _locale = parseLocaleTag(Model.languageCode);
    });
  }

  ThemeData _createTheme(Brightness brightness, Color seed) {
    final colorScheme = ColorScheme.fromSeed(seedColor: seed, brightness: brightness);
    return ThemeData(
      useMaterial3: true,
      colorScheme: colorScheme,
      appBarTheme: const AppBarTheme(backgroundColor: Colors.transparent),
      elevatedButtonTheme: ElevatedButtonThemeData(
        style: ElevatedButton.styleFrom(
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
        ),
      ),
      sliderTheme: SliderThemeData(
        showValueIndicator: ShowValueIndicator.onDrag,
        valueIndicatorTextStyle: TextStyle(
          color: brightness == Brightness.light ? Colors.white : Colors.black,
        ),
      ),
      outlinedButtonTheme: OutlinedButtonThemeData(
        style: OutlinedButton.styleFrom(
          side: BorderSide(color: colorScheme.primary),
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    if (_hasError) {
      return _buildErrorMessage();
    }
    Color seed = Colors.red;
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      localizationsDelegates: AppLocalizations.localizationsDelegates,
      supportedLocales: AppLocalizations.supportedLocales,
      locale: _locale,
      themeMode: _themeMode,
      theme: _createTheme(Brightness.light, seed),
      darkTheme: _createTheme(Brightness.dark, seed),
      home: _isReady ? const MainHomePage() : const Scaffold(body: LoadingScreen()),
    );
  }

  Widget _buildErrorMessage() {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: Padding(
            padding: EdgeInsets.all(24.0),
            child: Text(
              'Initialization failed. Please restart the app.',
              textAlign: TextAlign.center,
            ),
          ),
        ),
      ),
    );
  }

}
/// Copyright© ao-system, Inc.

import 'dart:ui' as ui;
import 'package:shared_preferences/shared_preferences.dart';

import 'package:fortuneslip/l10n/app_localizations.dart';

class Model {
  Model._();

  static const String _prefFortune1 = 'fortune1';
  static const String _prefFortune2 = 'fortune2';
  static const String _prefFortune3 = 'fortune3';
  static const String _prefFortune4 = 'fortune4';
  static const String _prefFortune5 = 'fortune5';
  static const String _prefFortune6 = 'fortune6';
  static const String _prefFortune7 = 'fortune7';
  static const String _prefRatio1 = 'ratio1';
  static const String _prefRatio2 = 'ratio2';
  static const String _prefRatio3 = 'ratio3';
  static const String _prefRatio4 = 'ratio4';
  static const String _prefRatio5 = 'ratio5';
  static const String _prefRatio6 = 'ratio6';
  static const String _prefRatio7 = 'ratio7';
  static const String _prefCountdownTime = 'countdownTime';
  static const String _prefSoundVolume = 'soundVolume';
  static const String _prefTtsEnabled = 'ttsEnabled';
  static const String _prefTtsVolume = 'ttsVolume';
  static const String _prefTtsVoiceId = 'ttsVoiceId';
  static const String _prefWakelockEnabled = 'wakelockEnabled';
  static const String _prefThemeNumber = 'themeNumber';
  static const String _prefLanguageCode = 'languageCode';

  static bool _ready = false;
  static String _fortune1 = '';
  static String _fortune2 = '';
  static String _fortune3 = '';
  static String _fortune4 = '';
  static String _fortune5 = '';
  static String _fortune6 = '';
  static String _fortune7 = '';
  static int _ratio1 = 1;
  static int _ratio2 = 1;
  static int _ratio3 = 1;
  static int _ratio4 = 1;
  static int _ratio5 = 1;
  static int _ratio6 = 1;
  static int _ratio7 = 1;
  static int _countdownTime = 0;
  static double _soundVolume = 1.0;
  static bool _ttsEnabled = true;
  static String _ttsVoiceId = '';
  static double _ttsVolume = 1.0;
  static bool _wakelockEnabled = false;
  static int _themeNumber = 0;
  static String _languageCode = '';

  static String get fortune1 => _fortune1;
  static String get fortune2 => _fortune2;
  static String get fortune3 => _fortune3;
  static String get fortune4 => _fortune4;
  static String get fortune5 => _fortune5;
  static String get fortune6 => _fortune6;
  static String get fortune7 => _fortune7;
  static int get ratio1 => _ratio1;
  static int get ratio2 => _ratio2;
  static int get ratio3 => _ratio3;
  static int get ratio4 => _ratio4;
  static int get ratio5 => _ratio5;
  static int get ratio6 => _ratio6;
  static int get ratio7 => _ratio7;
  static int get countdownTime => _countdownTime;
  static double get soundVolume => _soundVolume;
  static bool get ttsEnabled => _ttsEnabled;
  static String get ttsVoiceId => _ttsVoiceId;
  static double get ttsVolume => _ttsVolume;
  static bool get wakelockEnabled => _wakelockEnabled;
  static int get themeNumber => _themeNumber;
  static String get languageCode => _languageCode;

  static Future<void> ensureReady() async {
    if (_ready) {
      return;
    }
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    //
    _fortune1 = prefs.getString(_prefFortune1) ?? '';
    _fortune2 = prefs.getString(_prefFortune2) ?? '';
    _fortune3 = prefs.getString(_prefFortune3) ?? '';
    _fortune4 = prefs.getString(_prefFortune4) ?? '';
    _fortune5 = prefs.getString(_prefFortune5) ?? '';
    _fortune6 = prefs.getString(_prefFortune6) ?? '';
    _fortune7 = prefs.getString(_prefFortune7) ?? '';
    _ratio1 = prefs.getInt(_prefRatio1) ?? 1;
    _ratio2 = prefs.getInt(_prefRatio2) ?? 3;
    _ratio3 = prefs.getInt(_prefRatio3) ?? 5;
    _ratio4 = prefs.getInt(_prefRatio4) ?? 5;
    _ratio5 = prefs.getInt(_prefRatio5) ?? 5;
    _ratio6 = prefs.getInt(_prefRatio6) ?? 1;
    _ratio7 = prefs.getInt(_prefRatio7) ?? 1;
    _countdownTime = prefs.getInt(_prefCountdownTime) ?? 0;
    _soundVolume = prefs.getDouble(_prefSoundVolume) ?? 1.0;
    _ttsEnabled = prefs.getBool(_prefTtsEnabled) ?? true;
    _ttsVoiceId = prefs.getString(_prefTtsVoiceId) ?? '';
    _ttsVolume = (prefs.getDouble(_prefTtsVolume) ?? 1.0).clamp(0.0,1.0);
    _wakelockEnabled = prefs.getBool(_prefWakelockEnabled) ?? false;
    _themeNumber = (prefs.getInt(_prefThemeNumber) ?? 0).clamp(0, 2);
    _languageCode = prefs.getString(_prefLanguageCode) ?? ui.PlatformDispatcher.instance.locale.languageCode;
    _languageCode = _resolveLanguageCode(_languageCode);
    if (_fortune1 == '') {
      if (_languageCode == 'ja') {
        _fortune1 = '大吉:だいきち';
        _fortune2 = '吉:きち';
        _fortune3 = '中吉:ちゅうきち';
        _fortune4 = '小吉:しょうきち';
        _fortune5 = '末吉:すえきち';
        _fortune6 = '凶:きょう';
        _fortune7 = '大凶:だいきょう';
      } else {
        _fortune1 = 'Great blessing';
        _fortune2 = 'Blessing';
        _fortune3 = 'Middle blessing';
        _fortune4 = 'Small blessing';
        _fortune5 = 'Uncertain luck';
        _fortune6 = 'Curse';
        _fortune7 = 'Great curse';
      }
    }
    _ready = true;
  }

  static String _resolveLanguageCode(String code) {
    final supported = AppLocalizations.supportedLocales;
    if (supported.any((l) => l.languageCode == code)) {
      return code;
    } else {
      return '';
    }
  }

  static Future<void> setFortune1(String value) async {
    _fortune1 = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString(_prefFortune1, value);
  }

  static Future<void> setFortune2(String value) async {
    _fortune2 = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString(_prefFortune2, value);
  }

  static Future<void> setFortune3(String value) async {
    _fortune3 = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString(_prefFortune3, value);
  }

  static Future<void> setFortune4(String value) async {
    _fortune4 = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString(_prefFortune4, value);
  }

  static Future<void> setFortune5(String value) async {
    _fortune5 = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString(_prefFortune5, value);
  }

  static Future<void> setFortune6(String value) async {
    _fortune6 = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString(_prefFortune6, value);
  }

  static Future<void> setFortune7(String value) async {
    _fortune7 = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString(_prefFortune7, value);
  }

  static Future<void> setRatio1(int value) async {
    _ratio1 = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setInt(_prefRatio1, value);
  }

  static Future<void> setRatio2(int value) async {
    _ratio2 = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setInt(_prefRatio2, value);
  }

  static Future<void> setRatio3(int value) async {
    _ratio3 = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setInt(_prefRatio3, value);
  }

  static Future<void> setRatio4(int value) async {
    _ratio4 = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setInt(_prefRatio4, value);
  }

  static Future<void> setRatio5(int value) async {
    _ratio5 = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setInt(_prefRatio5, value);
  }

  static Future<void> setRatio6(int value) async {
    _ratio6 = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setInt(_prefRatio6, value);
  }

  static Future<void> setRatio7(int value) async {
    _ratio7 = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setInt(_prefRatio7, value);
  }

  static Future<void> setCountdownTime(int value) async {
    _countdownTime = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setInt(_prefCountdownTime, value);
  }

  static Future<void> setSoundVolume(double value) async {
    _soundVolume = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setDouble(_prefSoundVolume, value);
  }

  static Future<void> setTtsEnabled(bool flag) async {
    _ttsEnabled = flag;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setBool(_prefTtsEnabled, flag);
  }

  static Future<void> setTtsVoiceId(String value) async {
    _ttsVoiceId = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString(_prefTtsVoiceId, value);
  }

  static Future<void> setTtsVolume(double value) async {
    _ttsVolume = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setDouble(_prefTtsVolume, value);
  }

  static Future<void> setWakelockEnabled(bool value) async {
    _wakelockEnabled = value;
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setBool(_prefWakelockEnabled, value);
  }

  static Future<void> setThemeNumber(int value) async {
    _themeNumber = value;
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setInt(_prefThemeNumber, value);
  }

  static Future<void> setLanguageCode(String value) async {
    _languageCode = value;
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setString(_prefLanguageCode, value);
  }

}
/// Copyright© ao-system, Inc.

import 'dart:ui';

Locale? parseLocaleTag(String tag) {
  if (tag.isEmpty) {
    return null;
  }
  final parts = tag.split('-');
  final language = parts[0];
  String? script, country;
  if (parts.length >= 2) {
    parts[1].length == 4 ? script = parts[1] : country = parts[1];
  }
  if (parts.length >= 3) {
    parts[2].length == 4 ? script = parts[2] : country = parts[2];
  }
  return Locale.fromSubtags(
    languageCode: language,
    scriptCode: script,
    countryCode: country,
  );
}
/// Copyright© ao-system, Inc.

import 'package:flutter/material.dart';

import 'package:fortuneslip/theme_color.dart';
import 'package:fortuneslip/model.dart';

/// 設定画面専用のカスタムCardウィジェット
class SettingCard extends StatelessWidget {
  final Widget child;
  final ShapeBorder shape;
  final EdgeInsetsGeometry margin;

  const SettingCard({
    super.key,
    required this.child,
    this.margin = const EdgeInsets.only(left: 0, top: 12, right: 0, bottom: 0),
  }) : shape = const RoundedRectangleBorder(
    borderRadius: BorderRadius.all(Radius.circular(12)),
  );

  const SettingCard.top({
    super.key,
    required this.child,
    this.margin = const EdgeInsets.only(left: 0, top: 12, right: 0, bottom: 0),
  }) : shape = const RoundedRectangleBorder(
    borderRadius: BorderRadius.only(
      topLeft: Radius.circular(12),
      topRight: Radius.circular(12),
      bottomLeft: Radius.circular(0),
      bottomRight: Radius.circular(0),
    ),
  );

  const SettingCard.flat({
    super.key,
    required this.child,
    this.margin = const EdgeInsets.only(left: 0, top: 2, right: 0, bottom: 0),
  }) : shape = const RoundedRectangleBorder(
    borderRadius: BorderRadius.only(
      topLeft: Radius.circular(0),
      topRight: Radius.circular(0),
      bottomLeft: Radius.circular(0),
      bottomRight: Radius.circular(0),
    ),
  );

  const SettingCard.bottom({
    super.key,
    required this.child,
    this.margin = const EdgeInsets.only(left: 0, top: 2, right: 0, bottom: 0),
  }) : shape = const RoundedRectangleBorder(
    borderRadius: BorderRadius.only(
      topLeft: Radius.circular(0),
      topRight: Radius.circular(0),
      bottomLeft: Radius.circular(12),
      bottomRight: Radius.circular(12),
    ),
  );

  @override
  Widget build(BuildContext context) {
    final themeColor = ThemeColor(
      themeNumber: Model.themeNumber,
      context: context,
    );
    return SizedBox(
      width: double.infinity,
      child: Card(
        elevation: 0,
        margin: margin,
        surfaceTintColor: Colors.transparent,
        shadowColor: Colors.transparent,
        color: themeColor.cardColor,
        shape: shape,
        child: child,
      ),
    );
  }
}
/// Copyright© ao-system, Inc.

import "dart:async";
import "dart:io";
import "package:app_settings/app_settings.dart";
import "package:flutter/foundation.dart";
import "package:flutter/material.dart";
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:in_app_review/in_app_review.dart';

import "package:fortuneslip/setting_card.dart";
import 'package:fortuneslip/l10n/app_localizations.dart';
import 'package:fortuneslip/ad_banner_widget.dart';
import 'package:fortuneslip/ad_ump_status.dart';
import 'package:fortuneslip/theme_color.dart';
import 'package:fortuneslip/text_to_speech.dart';
import 'package:fortuneslip/model.dart';
import 'package:fortuneslip/loading_screen.dart';
import 'package:fortuneslip/_secrets.dart';
import "package:fortuneslip/main.dart";
import 'package:fortuneslip/att_service.dart';

class SettingPage extends StatefulWidget {
  const SettingPage({super.key});

  @override
  State<SettingPage> createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {
  AdUmpState _adUmpState = AdUmpState.initial;
  late final AdUmpService _adUmpService;
  late ThemeColor _themeColor;
  final _inAppReview = InAppReview.instance;
  bool _wakelockEnabled = true;
  int _themeNumber = 0;
  String _languageCode = '';
  bool _isReady = false;
  //
  bool _ttsEnabled = true;
  String _ttsVoiceId = '';
  double _ttsVolume = 1.0;
  //
  late List<TextEditingController> _fortuneControllers;
  late List<TextEditingController> _ratioControllers;
  late int _countdownTime;
  late double _soundVolume;

  @override
  void initState() {
    super.initState();
    _initState();
  }

  void _initState() async {
    //ump
    _adUmpService = AdUmpService();
    await _refreshConsentInfo();
    //model
    _fortuneControllers = List.generate(7, (_) => TextEditingController());
    _fortuneControllers[0].text = Model.fortune1;
    _fortuneControllers[1].text = Model.fortune2;
    _fortuneControllers[2].text = Model.fortune3;
    _fortuneControllers[3].text = Model.fortune4;
    _fortuneControllers[4].text = Model.fortune5;
    _fortuneControllers[5].text = Model.fortune6;
    _fortuneControllers[6].text = Model.fortune7;
    _ratioControllers = List.generate(7, (_) => TextEditingController());
    _ratioControllers[0].text = Model.ratio1.toString();
    _ratioControllers[1].text = Model.ratio2.toString();
    _ratioControllers[2].text = Model.ratio3.toString();
    _ratioControllers[3].text = Model.ratio4.toString();
    _ratioControllers[4].text = Model.ratio5.toString();
    _ratioControllers[5].text = Model.ratio6.toString();
    _ratioControllers[6].text = Model.ratio7.toString();
    _countdownTime = Model.countdownTime;
    _soundVolume = Model.soundVolume;
    _ttsEnabled = Model.ttsEnabled;
    _ttsVolume = Model.ttsVolume;
    _ttsVoiceId = Model.ttsVoiceId;
    _wakelockEnabled = Model.wakelockEnabled;
    _themeNumber = Model.themeNumber;
    _languageCode = Model.languageCode;
    //speech
    await TextToSpeech.applyPreferences(Model.ttsVoiceId,Model.ttsVolume);
    setState(() {
      _isReady = true;
    });
  }

  @override
  void dispose() {
    for (final controller in _fortuneControllers) {
      controller.dispose();
    }
    for (final controller in _ratioControllers) {
      controller.dispose();
    }
    unawaited(TextToSpeech.stop());
    super.dispose();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _themeColor = ThemeColor(themeNumber: Model.themeNumber, context: context);
  }

  Future<void> _refreshConsentInfo() async {
    final AdUmpState newState = await _adUmpService.updateConsentInfo(_adUmpState);
    if (mounted) {
      setState(() { _adUmpState = newState; });
    }
  }

  Future<void> _onApply() async {
    await Model.setFortune1(_fortuneControllers[0].text);
    await Model.setFortune2(_fortuneControllers[1].text);
    await Model.setFortune3(_fortuneControllers[2].text);
    await Model.setFortune4(_fortuneControllers[3].text);
    await Model.setFortune5(_fortuneControllers[4].text);
    await Model.setFortune6(_fortuneControllers[5].text);
    await Model.setFortune7(_fortuneControllers[6].text);
    await Model.setRatio1(int.tryParse(_ratioControllers[0].text) ?? 0);
    await Model.setRatio2(int.tryParse(_ratioControllers[1].text) ?? 0);
    await Model.setRatio3(int.tryParse(_ratioControllers[2].text) ?? 0);
    await Model.setRatio4(int.tryParse(_ratioControllers[3].text) ?? 0);
    await Model.setRatio5(int.tryParse(_ratioControllers[4].text) ?? 0);
    await Model.setRatio6(int.tryParse(_ratioControllers[5].text) ?? 0);
    await Model.setRatio7(int.tryParse(_ratioControllers[6].text) ?? 0);
    await Model.setCountdownTime(_countdownTime);
    await Model.setSoundVolume(_soundVolume);
    await Model.setTtsEnabled(_ttsEnabled);
    await Model.setTtsVoiceId(_ttsVoiceId);
    await Model.setTtsVolume(_ttsVolume);
    await Model.setWakelockEnabled(_wakelockEnabled);
    await Model.setThemeNumber(_themeNumber);
    await Model.setLanguageCode(_languageCode);
    if (!mounted) {
      return;
    }
    Navigator.of(context).pop(true);
  }

  @override
  Widget build(BuildContext context) {
    if (!_isReady) {
      return const LoadingScreen();
    }
    final l = AppLocalizations.of(context)!;
    final TextTheme t = Theme.of(context).textTheme;
    return Scaffold(
      backgroundColor: _themeColor.backColor,
      appBar: AppBar(
        centerTitle: true,
        elevation: 0,
        leading: IconButton(
          icon: const Icon(Icons.close),
          onPressed: () => Navigator.of(context).pop(false),
        ),
        title: Text(l.setting),
        foregroundColor: _themeColor.appBarForegroundColor,
        backgroundColor: Colors.transparent,
        actions: [
          Padding(
            padding: const EdgeInsets.only(right: 10),
            child: IconButton(
              onPressed: _onApply,
              tooltip: l.apply,
              icon: const Icon(Icons.check),
            ),
          ),
        ],
      ),
      body: SafeArea(
        child: GestureDetector(
          behavior: HitTestBehavior.translucent,
          onTap: () => FocusScope.of(context).unfocus(),
          child: ListView(
            padding: const EdgeInsets.only(left: 16, right: 16, top: 4, bottom: 100),
            children: [
              _buildFortuneTable(l, t),
              _buildCountdown(l, t),
              _buildSpeech(l, t),
              _buildWakelockEnabled(l, t),
              _buildTheme(l, t),
              _buildLanguage(l, t),
              _buildReview(l, t),
              _buildCmp(l, t),
              _buildAtt(l, t),
            ],
          ),
        ),
      ),
      bottomNavigationBar: AdBannerWidget(adManager: MainApp.of(context).adManager),
    );
  }

  Widget _buildFortuneTable(AppLocalizations l, TextTheme t) {
    return SettingCard(
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Table(
              columnWidths: const {
                0: IntrinsicColumnWidth(),
                1: FlexColumnWidth(),
                2: IntrinsicColumnWidth(),
                3: IntrinsicColumnWidth(),
              },
              defaultVerticalAlignment: TableCellVerticalAlignment.middle,
              children: [
                TableRow(
                  children: [
                    const SizedBox(),
                    Padding(
                      padding: const EdgeInsets.symmetric(vertical: 8),
                      child: Text(l.fortune, style: t.bodyMedium),
                    ),
                    Padding(
                      padding: const EdgeInsets.symmetric(vertical: 8),
                      child: Text(l.ratio, style: t.bodyMedium),
                    ),
                    const SizedBox(),
                  ],
                ),
                for (var i = 0; i < _fortuneControllers.length; i++)
                  TableRow(
                    children: [
                      Text('${i + 1} '),
                      Padding(
                        padding: const EdgeInsets.only(right: 4, bottom: 4),
                        child: TextField(
                          controller: _fortuneControllers[i],
                          decoration: const InputDecoration(
                            isDense: true,
                            border: OutlineInputBorder(),
                          ),
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.only(bottom: 4),
                        child: SizedBox(
                          width: 80,
                          child: TextField(
                            controller: _ratioControllers[i],
                            keyboardType: TextInputType.number,
                            decoration: const InputDecoration(
                              isDense: true,
                              border: OutlineInputBorder(),
                            ),
                          ),
                        ),
                      ),
                      const SizedBox.shrink(),
                    ],
                  ),
              ],
            ),
            const SizedBox(height: 12),
            Text(
              l.fortunesHint,
              style: t.bodySmall,
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildCountdown(AppLocalizations l, TextTheme t) {
    return Column(
      children: [
        SettingCard.top(
          child: ListTile(
            contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
            title: Text(l.countdownTime, style: t.bodyMedium),
            subtitle: Row(
              children: [
                Text(_countdownTime.toStringAsFixed(0)),
                const SizedBox(width: 12),
                Expanded(
                  child: Slider(
                    value: _countdownTime.toDouble(),
                    min: 0,
                    max: 9,
                    divisions: 9,
                    label: _countdownTime.toString(),
                    onChanged: (value) {
                      setState(() {
                        _countdownTime = value.toInt();
                      });
                    },
                  ),
                ),
              ],
            ),
          ),
        ),
        SettingCard.bottom(
          child: ListTile(
            contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
            title: Text(l.soundVolume, style: t.bodyMedium),
            subtitle: Row(
              children: [
                Text(_soundVolume.toStringAsFixed(1)),
                const SizedBox(width: 12),
                Expanded(
                  child: Slider(
                    value: _soundVolume,
                    min: 0.0,
                    max: 1.0,
                    divisions: 10,
                    label: _soundVolume.toStringAsFixed(1),
                    onChanged: (value) {
                      setState(() {
                        _soundVolume = value;
                      });
                    },
                  ),
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }

  Widget _buildSpeech(AppLocalizations l, TextTheme t) {
    if (TextToSpeech.ttsVoices.isEmpty) {
      return const SizedBox.shrink();
    }
    return Column(
      children: [
        SettingCard.top(
          child: ListTile(
            contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
            title: Text(l.ttsEnabled, style: t.bodyMedium),
            trailing: Switch(
              value: _ttsEnabled,
              onChanged: (value) {
                setState(() => _ttsEnabled = value);
              },
            ),
          ),
        ),
        SettingCard.flat(
          child: ListTile(
            contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
            title: Text(l.ttsVolume, style: t.bodyMedium),
            subtitle: Row(
              children: [
                Text(_ttsVolume.toStringAsFixed(1)),
                const SizedBox(width: 12),
                Expanded(
                  child: Slider(
                    value: _ttsVolume,
                    min: 0.0,
                    max: 1.0,
                    divisions: 10,
                    label: _ttsVolume.toStringAsFixed(1),
                    onChanged: _ttsEnabled
                        ? (value) {
                      setState(() {
                        _ttsVolume = double.parse(value.toStringAsFixed(1));
                      });
                    }
                        : null,
                  ),
                ),
              ],
            ),
          ),
        ),
        SettingCard.bottom(
          child: ListTile(
            contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
            title: null,
            subtitle: DropdownButtonFormField<String>(
              initialValue: () {
                if (_ttsVoiceId.isNotEmpty && TextToSpeech.ttsVoices.any((o) => o.id == _ttsVoiceId)) {
                  return _ttsVoiceId;
                }
                return TextToSpeech.ttsVoices.first.id;
              }(),
              items: TextToSpeech.buildGroupedItems(),
              onChanged: (v) {
                if (v == null) {
                  return;
                }
                setState(() => _ttsVoiceId = v);
              },
            ),
          ),
        )
      ],
    );
  }

  Widget _buildWakelockEnabled(AppLocalizations l, TextTheme t) {
    return SettingCard(
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        title: Text(l.wakelockEnabled, style: t.bodyMedium),
        trailing: Switch(
          value: _wakelockEnabled,
          onChanged: (value) => setState(() => _wakelockEnabled = value),
        ),
      ),
    );
  }

  Widget _buildTheme(AppLocalizations l, TextTheme t) {
    return SettingCard(
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        title: Text(l.theme, style: t.bodyMedium),
        trailing: DropdownButton<int>(
          value: _themeNumber,
          items: [
            DropdownMenuItem(value: 0, child: Text(l.systemSetting)),
            DropdownMenuItem(value: 1, child: Text(l.lightTheme)),
            DropdownMenuItem(value: 2, child: Text(l.darkTheme)),
          ],
          onChanged: (value) {
            if (value != null) {
              setState(() {
                _themeNumber = value;
              });
            }
          },
        ),
      ),
    );
  }

  Widget _buildLanguage(AppLocalizations l, TextTheme t) {
    final Map<String,String> languageNames = {
      'af': 'af: Afrikaans',
      'ar': 'ar: العربية',
      'bg': 'bg: Български',
      'bn': 'bn: বাংলা',
      'bs': 'bs: Bosanski',
      'ca': 'ca: Català',
      'cs': 'cs: Čeština',
      'da': 'da: Dansk',
      'de': 'de: Deutsch',
      'el': 'el: Ελληνικά',
      'en': 'en: English',
      'es': 'es: Español',
      'et': 'et: Eesti',
      'fa': 'fa: فارسی',
      'fi': 'fi: Suomi',
      'fil': 'fil: Filipino',
      'fr': 'fr: Français',
      'gu': 'gu: ગુજરાતી',
      'he': 'he: עברית',
      'hi': 'hi: हिन्दी',
      'hr': 'hr: Hrvatski',
      'hu': 'hu: Magyar',
      'id': 'id: Bahasa Indonesia',
      'it': 'it: Italiano',
      'ja': 'ja: 日本語',
      //'jv': 'jv: Basa Jawa',    //flutterのサポート外
      'km': 'km: ខ្មែរ',
      'kn': 'kn: ಕನ್ನಡ',
      'ko': 'ko: 한국어',
      'lt': 'lt: Lietuvių',
      'lv': 'lv: Latviešu',
      'ml': 'ml: മലയാളം',
      'mr': 'mr: मराठी',
      'ms': 'ms: Bahasa Melayu',
      'my': 'my: မြန်မာ',
      'ne': 'ne: नेपाली',
      'nl': 'nl: Nederlands',
      'or': 'or: ଓଡ଼ିଆ',
      'pa': 'pa: ਪੰਜਾਬੀ',
      'pl': 'pl: Polski',
      'pt': 'pt: Português',
      'ro': 'ro: Română',
      'ru': 'ru: Русский',
      'si': 'si: සිංහල',
      'sk': 'sk: Slovenčina',
      'sr': 'sr: Српски',
      'sv': 'sv: Svenska',
      'sw': 'sw: Kiswahili',
      'ta': 'ta: தமிழ்',
      'te': 'te: తెలుగు',
      'th': 'th: ไทย',
      'tl': 'tl: Tagalog',
      'tr': 'tr: Türkçe',
      'uk': 'uk: Українська',
      'ur': 'ur: اردو',
      'uz': 'uz: Oʻzbekcha',
      'vi': 'vi: Tiếng Việt',
      'zh': 'zh: 中文',
      'zu': 'zu: isiZulu',
    };
    return SettingCard(
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        title: Text(l.language, style: t.bodyMedium),
        trailing: DropdownButton<String?>(
          value: _languageCode,
          items: [
            DropdownMenuItem(value: '', child: Text('Default')),
            ...languageNames.entries.map((entry) => DropdownMenuItem<String?>(
              value: entry.key,
              child: Text(entry.value),
            )),
          ],
          onChanged: (String? value) {
            setState(() {
              _languageCode = value ?? '';
            });
          },
        ),
      ),
    );
  }

  Widget _buildReview(AppLocalizations l, TextTheme t) {
    return SettingCard(
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        title: Text(l.reviewApp, style: t.bodyMedium),
        subtitle: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            const SizedBox(height: 8),
            OutlinedButton.icon(
              icon: const Icon(Icons.open_in_new, size: 16),
              label: Text(l.reviewStore, style: t.bodySmall),
              onPressed: () async {
                await _inAppReview.openStoreListing(
                  appStoreId: Secrets.appStoreId,
                );
              },
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildCmp(AppLocalizations l, TextTheme t) {
    final showButton = _adUmpState.privacyStatus == PrivacyOptionsRequirementStatus.required;
    String statusLabel = l.cmpCheckingRegion;
    IconData statusIcon = Icons.help_outline;
    switch (_adUmpState.privacyStatus) {
      case PrivacyOptionsRequirementStatus.required:
        statusLabel = l.cmpRegionRequiresSettings;
        statusIcon = Icons.privacy_tip_outlined;
        break;
      case PrivacyOptionsRequirementStatus.notRequired:
        statusLabel = l.cmpRegionNoSettingsRequired;
        statusIcon = Icons.check_circle_outline;
        break;
      case PrivacyOptionsRequirementStatus.unknown:
        statusLabel = l.cmpRegionCheckFailed;
        statusIcon = Icons.error_outline;
        break;
    }
    return SettingCard(
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        title: Text(l.cmpSettingsTitle, style: t.bodyMedium),
        subtitle: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const SizedBox(height: 8),
            Text(l.cmpConsentDescription, style: t.bodySmall),
            const SizedBox(height: 16),
            Center(
              child: Column(
                children: [
                  Chip(
                    avatar: Icon(statusIcon, size: 18),
                    label: Text(statusLabel),
                  ),
                  const SizedBox(height: 6),
                  Text(
                    '${l.cmpConsentStatusLabel} ${_adUmpState.consentStatus.localized(context)}',
                    style: t.bodySmall,
                  ),
                  if (_adUmpState.consentStatus == ConsentStatus.obtained) ...[
                    const SizedBox(height: 6),
                    Text(l.cmpConsentStatusObtainedNote, style: t.bodySmall),
                  ],
                  if (showButton) ...[
                    const SizedBox(height: 8),
                    ElevatedButton.icon(
                      onPressed: _adUmpState.isChecking
                          ? null
                          : () async {
                        try {
                          await _adUmpService.showPrivacyOptions();
                        } catch (e) {
                          //debugPrint('Privacy options error ignored: $e');
                        }
                        await _refreshConsentInfo();
                      },
                      icon: const Icon(Icons.settings),
                      label: Text(
                        _adUmpState.isChecking
                            ? l.cmpConsentStatusChecking
                            : l.cmpOpenConsentSettings,
                      ),
                    ),
                    const SizedBox(height: 8),
                    OutlinedButton.icon(
                      onPressed: _adUmpState.isChecking ? null : _refreshConsentInfo,
                      icon: const Icon(Icons.refresh),
                      label: Text(l.cmpRefreshStatus),
                    ),
                    const SizedBox(height: 8),
                    OutlinedButton.icon(
                      onPressed: () async {
                        final messenger = ScaffoldMessenger.of(context);
                        final message = l.cmpResetStatusDone;
                        await ConsentInformation.instance.reset();
                        if (!mounted) {
                          return;
                        }
                        setState(() {
                          _adUmpState = _adUmpState.copyWith(
                            consentStatus: ConsentStatus.unknown,
                          );
                        });
                        messenger.showSnackBar(SnackBar(content: Text(message)));
                      },
                      icon: const Icon(Icons.delete_sweep_outlined),
                      label: Text(l.cmpResetStatus),
                    ),
                  ],
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildAtt(AppLocalizations l, TextTheme t) {
    if (kIsWeb || !Platform.isIOS) {
      return const SizedBox.shrink();
    }
    return SettingCard(
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        title: Text(l.attSettingsTitle, style: t.bodyMedium),
        subtitle: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const SizedBox(height: 8),
            Text(l.attDescription, style: t.bodySmall),
            const SizedBox(height: 8),
            FutureBuilder<AttStatus>(
              future: AttService().getTrackingStatus(),
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return Center(
                    child: Column(
                      children: [
                        Chip(
                          avatar: const Icon(Icons.hourglass_empty),
                          label: Text(l.attStatusChecking),
                        ),
                        const SizedBox(height: 8),
                        OutlinedButton.icon(
                          onPressed: null,
                          icon: const Icon(Icons.open_in_new),
                          label: Text(l.attOpenSettings),
                        ),
                      ],
                    ),
                  );
                }
                final status = snapshot.data ?? AttStatus.unknown;
                final label = status.name;
                return Center(
                  child: Column(
                    children: [
                      Chip(
                        avatar: const Icon(Icons.track_changes),
                        label: Text('${l.attStatusLabel} $label'),
                      ),
                      const SizedBox(height: 8),
                      OutlinedButton.icon(
                        onPressed: () => AppSettings.openAppSettings(),
                        icon: const Icon(Icons.open_in_new, size: 16),
                        label: Text(l.attOpenSettings, style: t.bodySmall),
                      ),
                    ],
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
  }

}
/// Copyright© ao-system, Inc.

import "dart:async";
import "dart:io";
import "package:app_settings/app_settings.dart";
import "package:app_tracking_transparency/app_tracking_transparency.dart";
import "package:flutter/foundation.dart";
import "package:flutter/material.dart";
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:in_app_review/in_app_review.dart';

import "package:fortuneslip/setting_card.dart";
import 'package:fortuneslip/l10n/app_localizations.dart';
import 'package:fortuneslip/ad_manager.dart';
import 'package:fortuneslip/ad_banner_widget.dart';
import 'package:fortuneslip/ad_ump_status.dart';
import 'package:fortuneslip/theme_color.dart';
import 'package:fortuneslip/text_to_speech.dart';
import 'package:fortuneslip/model.dart';
import 'package:fortuneslip/loading_screen.dart';
import 'package:fortuneslip/_secrets.dart';
import "package:fortuneslip/main.dart";

class SettingPage extends StatefulWidget {
  const SettingPage({super.key});

  @override
  State<SettingPage> createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {
  AdUmpState _adUmpState = AdUmpState.initial;
  late final AdUmpService _adUmpService;
  late ThemeColor _themeColor;
  final _inAppReview = InAppReview.instance;
  bool _wakelockEnabled = true;
  int _themeNumber = 0;
  String _languageCode = '';
  bool _isReady = false;
  //
  late List<TtsOption> _ttsVoices;
  bool _ttsEnabled = true;
  String _ttsVoiceId = '';
  double _ttsVolume = 1.0;
  //
  late List<TextEditingController> _fortuneControllers;
  late List<TextEditingController> _ratioControllers;
  late int _countdownTime;
  late double _soundVolume;

  @override
  void initState() {
    super.initState();
    _initState();
  }

  void _initState() async {
    //ump
    _adUmpService = AdUmpService();
    await _refreshConsentInfo();
    //model
    _fortuneControllers = List.generate(7, (_) => TextEditingController());
    _fortuneControllers[0].text = Model.fortune1;
    _fortuneControllers[1].text = Model.fortune2;
    _fortuneControllers[2].text = Model.fortune3;
    _fortuneControllers[3].text = Model.fortune4;
    _fortuneControllers[4].text = Model.fortune5;
    _fortuneControllers[5].text = Model.fortune6;
    _fortuneControllers[6].text = Model.fortune7;
    _ratioControllers = List.generate(7, (_) => TextEditingController());
    _ratioControllers[0].text = Model.ratio1.toString();
    _ratioControllers[1].text = Model.ratio2.toString();
    _ratioControllers[2].text = Model.ratio3.toString();
    _ratioControllers[3].text = Model.ratio4.toString();
    _ratioControllers[4].text = Model.ratio5.toString();
    _ratioControllers[5].text = Model.ratio6.toString();
    _ratioControllers[6].text = Model.ratio7.toString();
    _countdownTime = Model.countdownTime;
    _soundVolume = Model.soundVolume;
    _ttsEnabled = Model.ttsEnabled;
    _ttsVolume = Model.ttsVolume;
    _ttsVoiceId = Model.ttsVoiceId;
    _wakelockEnabled = Model.wakelockEnabled;
    _themeNumber = Model.themeNumber;
    _languageCode = Model.languageCode;
    //speech
    await TextToSpeech.getInstance();
    _ttsVoices = TextToSpeech.ttsVoices;
    TextToSpeech.setVolume(_ttsVolume);
    TextToSpeech.setTtsVoiceId(_ttsVoiceId);
    //
    setState(() {
      _isReady = true;
    });
  }

  @override
  void dispose() {
    for (final controller in _fortuneControllers) {
      controller.dispose();
    }
    for (final controller in _ratioControllers) {
      controller.dispose();
    }
    TextToSpeech.stop();
    super.dispose();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _themeColor = ThemeColor(themeNumber: Model.themeNumber, context: context);
  }

  Future<void> _refreshConsentInfo() async {
    final AdUmpState newState = await _adUmpService.updateConsentInfo(_adUmpState);
    if (mounted) {
      setState(() { _adUmpState = newState; });
    }
  }

  Future<void> _onApply() async {
    await Model.setFortune1(_fortuneControllers[0].text);
    await Model.setFortune2(_fortuneControllers[1].text);
    await Model.setFortune3(_fortuneControllers[2].text);
    await Model.setFortune4(_fortuneControllers[3].text);
    await Model.setFortune5(_fortuneControllers[4].text);
    await Model.setFortune6(_fortuneControllers[5].text);
    await Model.setFortune7(_fortuneControllers[6].text);
    await Model.setRatio1(int.tryParse(_ratioControllers[0].text) ?? 0);
    await Model.setRatio2(int.tryParse(_ratioControllers[1].text) ?? 0);
    await Model.setRatio3(int.tryParse(_ratioControllers[2].text) ?? 0);
    await Model.setRatio4(int.tryParse(_ratioControllers[3].text) ?? 0);
    await Model.setRatio5(int.tryParse(_ratioControllers[4].text) ?? 0);
    await Model.setRatio6(int.tryParse(_ratioControllers[5].text) ?? 0);
    await Model.setRatio7(int.tryParse(_ratioControllers[6].text) ?? 0);
    await Model.setCountdownTime(_countdownTime);
    await Model.setSoundVolume(_soundVolume);
    await Model.setTtsEnabled(_ttsEnabled);
    await Model.setTtsVoiceId(_ttsVoiceId);
    await Model.setTtsVolume(_ttsVolume);
    await Model.setWakelockEnabled(_wakelockEnabled);
    await Model.setThemeNumber(_themeNumber);
    await Model.setLanguageCode(_languageCode);
    if (!mounted) {
      return;
    }
    Navigator.of(context).pop(true);
  }

  @override
  Widget build(BuildContext context) {
    if (!_isReady) {
      return const LoadingScreen();
    }
    final l = AppLocalizations.of(context)!;
    final TextTheme t = Theme.of(context).textTheme;
    return Scaffold(
      backgroundColor: _themeColor.backColor,
      appBar: AppBar(
        centerTitle: true,
        elevation: 0,
        leading: IconButton(
          icon: const Icon(Icons.close),
          onPressed: () => Navigator.of(context).pop(false),
        ),
        title: Text(l.setting),
        foregroundColor: _themeColor.appBarForegroundColor,
        backgroundColor: Colors.transparent,
        actions: [
          Padding(
            padding: const EdgeInsets.only(right: 10),
            child: IconButton(
              onPressed: _onApply,
              tooltip: l.apply,
              icon: const Icon(Icons.check),
            ),
          ),
        ],
      ),
      body: SafeArea(
        child: GestureDetector(
          behavior: HitTestBehavior.translucent,
          onTap: () => FocusScope.of(context).unfocus(),
          child: ListView(
            padding: const EdgeInsets.only(left: 16, right: 16, top: 4, bottom: 100),
            children: [
              _buildFortuneTable(l, t),
              _buildCountdown(l, t),
              _buildSpeech(l, t),
              _buildWakelockEnabled(l, t),
              _buildTheme(l, t),
              _buildLanguage(l, t),
              _buildReview(l, t),
              _buildCmp(l, t),
              _buildAtt(l, t),
            ],
          ),
        ),
      ),
      bottomNavigationBar: AdBannerWidget(adManager: MainApp.of(context).adManager),
    );
  }

  Widget _buildFortuneTable(AppLocalizations l, TextTheme t) {
    return SettingCard(
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Table(
              columnWidths: const {
                0: IntrinsicColumnWidth(),
                1: FlexColumnWidth(),
                2: IntrinsicColumnWidth(),
                3: IntrinsicColumnWidth(),
              },
              defaultVerticalAlignment: TableCellVerticalAlignment.middle,
              children: [
                TableRow(
                  children: [
                    const SizedBox(),
                    Padding(
                      padding: const EdgeInsets.symmetric(vertical: 8),
                      child: Text(l.fortune, style: t.bodyMedium),
                    ),
                    Padding(
                      padding: const EdgeInsets.symmetric(vertical: 8),
                      child: Text(l.ratio, style: t.bodyMedium),
                    ),
                    const SizedBox(),
                  ],
                ),
                for (var i = 0; i < _fortuneControllers.length; i++)
                  TableRow(
                    children: [
                      Text('${i + 1} '),
                      Padding(
                        padding: const EdgeInsets.only(right: 4, bottom: 4),
                        child: TextField(
                          controller: _fortuneControllers[i],
                          decoration: const InputDecoration(
                            isDense: true,
                            border: OutlineInputBorder(),
                          ),
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.only(bottom: 4),
                        child: SizedBox(
                          width: 80,
                          child: TextField(
                            controller: _ratioControllers[i],
                            keyboardType: TextInputType.number,
                            decoration: const InputDecoration(
                              isDense: true,
                              border: OutlineInputBorder(),
                            ),
                          ),
                        ),
                      ),
                      const SizedBox.shrink(),
                    ],
                  ),
              ],
            ),
            const SizedBox(height: 12),
            Text(
              l.fortunesHint,
              style: t.bodySmall,
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildCountdown(AppLocalizations l, TextTheme t) {
    return Column(
      children: [
        SettingCard.top(
          child: ListTile(
            contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
            title: Text(l.countdownTime, style: t.bodyMedium),
            subtitle: Row(
              children: [
                Text(_countdownTime.toStringAsFixed(0)),
                const SizedBox(width: 12),
                Expanded(
                  child: Slider(
                    value: _countdownTime.toDouble(),
                    min: 0,
                    max: 9,
                    divisions: 9,
                    label: _countdownTime.toString(),
                    onChanged: (value) {
                      setState(() {
                        _countdownTime = value.toInt();
                      });
                    },
                  ),
                ),
              ],
            ),
          ),
        ),
        SettingCard.bottom(
          child: ListTile(
            contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
            title: Text(l.soundVolume, style: t.bodyMedium),
            subtitle: Row(
              children: [
                Text(_soundVolume.toStringAsFixed(1)),
                const SizedBox(width: 12),
                Expanded(
                  child: Slider(
                    value: _soundVolume,
                    min: 0.0,
                    max: 1.0,
                    divisions: 10,
                    label: _soundVolume.toStringAsFixed(1),
                    onChanged: (value) {
                      setState(() {
                        _soundVolume = value;
                      });
                    },
                  ),
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }

  Widget _buildSpeech(AppLocalizations l, TextTheme t) {
    if (_ttsVoices.isEmpty) {
      return const SizedBox.shrink();
    }
    return Column(
      children: [
        SettingCard.top(
          child: ListTile(
            contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
            title: Text(l.ttsEnabled, style: t.bodyMedium),
            trailing: Switch(
              value: _ttsEnabled,
              onChanged: (value) {
                setState(() => _ttsEnabled = value);
              },
            ),
          ),
        ),
        SettingCard.flat(
          child: ListTile(
            contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
            title: Text(l.ttsVolume, style: t.bodyMedium),
            subtitle: Row(
              children: [
                Text(_ttsVolume.toStringAsFixed(1)),
                const SizedBox(width: 12),
                Expanded(
                  child: Slider(
                    value: _ttsVolume,
                    min: 0.0,
                    max: 1.0,
                    divisions: 10,
                    label: _ttsVolume.toStringAsFixed(1),
                    onChanged: _ttsEnabled
                        ? (value) {
                      setState(() {
                        _ttsVolume = double.parse(value.toStringAsFixed(1));
                      });
                    }
                        : null,
                  ),
                ),
              ],
            ),
          ),
        ),
        SettingCard.bottom(
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
            child: DropdownButtonFormField<String>(
              value: () {
                if (_ttsVoiceId.isNotEmpty &&
                    _ttsVoices.any((o) => o.id == _ttsVoiceId)) {
                  return _ttsVoiceId;
                }
                return _ttsVoices.first.id;
              }(),
              items: _ttsVoices
                  .map((o) => DropdownMenuItem<String>(
                value: o.id,
                child: Text(o.label),
              ))
                  .toList(),
              onChanged: (v) {
                if (v != null) {
                  setState(() => _ttsVoiceId = v);
                }
              },
            ),
          ),
        ),
      ],
    );
  }

  Widget _buildWakelockEnabled(AppLocalizations l, TextTheme t) {
    return SettingCard(
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        title: Text(l.wakelockEnabled, style: t.bodyMedium),
        trailing: Switch(
          value: _wakelockEnabled,
          onChanged: (value) => setState(() => _wakelockEnabled = value),
        ),
      ),
    );
  }

  Widget _buildTheme(AppLocalizations l, TextTheme t) {
    return SettingCard(
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        title: Text(l.theme, style: t.bodyMedium),
        trailing: DropdownButton<int>(
          value: _themeNumber,
          items: [
            DropdownMenuItem(value: 0, child: Text(l.systemSetting)),
            DropdownMenuItem(value: 1, child: Text(l.lightTheme)),
            DropdownMenuItem(value: 2, child: Text(l.darkTheme)),
          ],
          onChanged: (value) {
            if (value != null) {
              setState(() {
                _themeNumber = value;
              });
            }
          },
        ),
      ),
    );
  }

  Widget _buildLanguage(AppLocalizations l, TextTheme t) {
    final Map<String,String> languageNames = {
      'af': 'af: Afrikaans',
      'ar': 'ar: العربية',
      'bg': 'bg: Български',
      'bn': 'bn: বাংলা',
      'bs': 'bs: Bosanski',
      'ca': 'ca: Català',
      'cs': 'cs: Čeština',
      'da': 'da: Dansk',
      'de': 'de: Deutsch',
      'el': 'el: Ελληνικά',
      'en': 'en: English',
      'es': 'es: Español',
      'et': 'et: Eesti',
      'fa': 'fa: فارسی',
      'fi': 'fi: Suomi',
      'fil': 'fil: Filipino',
      'fr': 'fr: Français',
      'gu': 'gu: ગુજરાતી',
      'he': 'he: עברית',
      'hi': 'hi: हिन्दी',
      'hr': 'hr: Hrvatski',
      'hu': 'hu: Magyar',
      'id': 'id: Bahasa Indonesia',
      'it': 'it: Italiano',
      'ja': 'ja: 日本語',
      //'jv': 'jv: Basa Jawa',    //flutterのサポート外
      'km': 'km: ខ្មែរ',
      'kn': 'kn: ಕನ್ನಡ',
      'ko': 'ko: 한국어',
      'lt': 'lt: Lietuvių',
      'lv': 'lv: Latviešu',
      'ml': 'ml: മലയാളം',
      'mr': 'mr: मराठी',
      'ms': 'ms: Bahasa Melayu',
      'my': 'my: မြန်မာ',
      'ne': 'ne: नेपाली',
      'nl': 'nl: Nederlands',
      'or': 'or: ଓଡ଼ିଆ',
      'pa': 'pa: ਪੰਜਾਬੀ',
      'pl': 'pl: Polski',
      'pt': 'pt: Português',
      'ro': 'ro: Română',
      'ru': 'ru: Русский',
      'si': 'si: සිංහල',
      'sk': 'sk: Slovenčina',
      'sr': 'sr: Српски',
      'sv': 'sv: Svenska',
      'sw': 'sw: Kiswahili',
      'ta': 'ta: தமிழ்',
      'te': 'te: తెలుగు',
      'th': 'th: ไทย',
      'tl': 'tl: Tagalog',
      'tr': 'tr: Türkçe',
      'uk': 'uk: Українська',
      'ur': 'ur: اردو',
      'uz': 'uz: Oʻzbekcha',
      'vi': 'vi: Tiếng Việt',
      'zh': 'zh: 中文',
      'zu': 'zu: isiZulu',
    };
    return SettingCard(
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        title: Text(l.language, style: t.bodyMedium),
        trailing: DropdownButton<String?>(
          value: _languageCode,
          items: [
            DropdownMenuItem(value: '', child: Text('Default')),
            ...languageNames.entries.map((entry) => DropdownMenuItem<String?>(
              value: entry.key,
              child: Text(entry.value),
            )),
          ],
          onChanged: (String? value) {
            setState(() {
              _languageCode = value ?? '';
            });
          },
        ),
      ),
    );
  }

  Widget _buildReview(AppLocalizations l, TextTheme t) {
    return SettingCard(
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        title: Text(l.reviewApp, style: t.bodyMedium),
        subtitle: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            const SizedBox(height: 8),
            OutlinedButton.icon(
              icon: const Icon(Icons.open_in_new, size: 16),
              label: Text(l.reviewStore, style: t.bodySmall),
              onPressed: () async {
                await _inAppReview.openStoreListing(
                  appStoreId: Secrets.appStoreId,
                );
              },
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildCmp(AppLocalizations l, TextTheme t) {
    final showButton = _adUmpState.privacyStatus == PrivacyOptionsRequirementStatus.required;
    String statusLabel = l.cmpCheckingRegion;
    IconData statusIcon = Icons.help_outline;
    switch (_adUmpState.privacyStatus) {
      case PrivacyOptionsRequirementStatus.required:
        statusLabel = l.cmpRegionRequiresSettings;
        statusIcon = Icons.privacy_tip_outlined;
        break;
      case PrivacyOptionsRequirementStatus.notRequired:
        statusLabel = l.cmpRegionNoSettingsRequired;
        statusIcon = Icons.check_circle_outline;
        break;
      case PrivacyOptionsRequirementStatus.unknown:
        statusLabel = l.cmpRegionCheckFailed;
        statusIcon = Icons.error_outline;
        break;
    }
    return SettingCard(
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        title: Text(l.cmpSettingsTitle, style: t.bodyMedium),
        subtitle: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const SizedBox(height: 8),
            Text(l.cmpConsentDescription, style: t.bodySmall),
            const SizedBox(height: 16),
            Center(
              child: Column(
                children: [
                  Chip(
                    avatar: Icon(statusIcon, size: 18),
                    label: Text(statusLabel),
                  ),
                  const SizedBox(height: 6),
                  Text(
                    '${l.cmpConsentStatusLabel} ${_adUmpState.consentStatus.localized(context)}',
                    style: t.bodySmall,
                  ),
                  if (_adUmpState.consentStatus == ConsentStatus.obtained) ...[
                    const SizedBox(height: 6),
                    Text(l.cmpConsentStatusObtainedNote, style: t.bodySmall),
                  ],
                  if (showButton) ...[
                    const SizedBox(height: 8),
                    ElevatedButton.icon(
                      onPressed: _adUmpState.isChecking
                          ? null
                          : () async {
                        try {
                          await _adUmpService.showPrivacyOptions();
                        } catch (e) {
                          //debugPrint('Privacy options error ignored: $e');
                        }
                        await _refreshConsentInfo();
                      },
                      icon: const Icon(Icons.settings),
                      label: Text(
                        _adUmpState.isChecking
                            ? l.cmpConsentStatusChecking
                            : l.cmpOpenConsentSettings,
                      ),
                    ),
                    const SizedBox(height: 8),
                    OutlinedButton.icon(
                      onPressed: _adUmpState.isChecking ? null : _refreshConsentInfo,
                      icon: const Icon(Icons.refresh),
                      label: Text(l.cmpRefreshStatus),
                    ),
                    const SizedBox(height: 8),
                    OutlinedButton.icon(
                      onPressed: () async {
                        final messenger = ScaffoldMessenger.of(context);
                        final message = l.cmpResetStatusDone;
                        await ConsentInformation.instance.reset();
                        if (!mounted) {
                          return;
                        }
                        setState(() {
                          _adUmpState = _adUmpState.copyWith(
                            consentStatus: ConsentStatus.unknown,
                          );
                        });
                        messenger.showSnackBar(SnackBar(content: Text(message)));
                      },
                      icon: const Icon(Icons.delete_sweep_outlined),
                      label: Text(l.cmpResetStatus),
                    ),
                  ],
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildAtt(AppLocalizations l, TextTheme t) {
    if (kIsWeb || !Platform.isIOS) {
      return const SizedBox.shrink();
    }
    return SettingCard(
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        title: Text(l.attSettingsTitle, style: t.bodyMedium),
        subtitle: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const SizedBox(height: 8),
            Text(l.attDescription, style: t.bodySmall),
            const SizedBox(height: 8),
            FutureBuilder<TrackingStatus>(
              future: AppTrackingTransparency.trackingAuthorizationStatus,
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return Column(
                    children: [
                      Chip(
                        avatar: const Icon(Icons.hourglass_empty),
                        label: Text(l.attStatusChecking),
                      ),
                      const SizedBox(height: 8),
                      OutlinedButton.icon(
                        onPressed: null,
                        icon: const Icon(Icons.open_in_new),
                        label: Text(l.attOpenSettings),
                      ),
                    ],
                  );
                }
                final status = snapshot.data;
                final label = status != null
                    ? status.toString().split('.').last
                    : l.attStatusUnknown;
                return Center(
                  child: Column(
                    children: [
                      Chip(
                        avatar: const Icon(Icons.track_changes),
                        label: Text('${l.attStatusLabel} $label'),
                      ),
                      const SizedBox(height: 8),
                      OutlinedButton.icon(
                        onPressed: () => AppSettings.openAppSettings(),
                        icon: const Icon(Icons.open_in_new, size: 16),
                        label: Text(l.attOpenSettings, style: t.bodySmall),
                      ),
                    ],
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
  }

}
/// Copyright© ao-system, Inc.

/*
--- home_page.dart ---
import 'package:example/text_to_speech.dart';
class _MainHomePageState extends State<MainHomePage> {
  Future<void> _initState() async {
    await TextToSpeech.applyPreferences(Model.ttsVoiceId,Model.ttsVolume);
  }
  @override
  void dispose() {
    unawaited(TextToSpeech.stop());
  }
  void _ttsResult(String text) async {
    if (Model.ttsEnabled && Model.ttsVolume > 0.0) {
      await TextToSpeech.speak(text);
    }
  }
  Future<void> _openSetting() async {
      await TextToSpeech.applyPreferences(Model.ttsVoiceId,Model.ttsVolume);
  }
}
*/

/*
--- setting_page.dart ---
import 'package:example/text_to_speech.dart';
class _SettingPageState extends State<SettingPage> {
  bool _ttsEnabled = true;
  String _ttsVoiceId = '';
  double _ttsVolume = 1.0;
  Future<void> _initialize() async {
    //model
    _ttsEnabled = Model.ttsEnabled;
    _ttsVolume = Model.ttsVolume;
    _ttsVoiceId = Model.ttsVoiceId;
    //speech
    await TextToSpeech.applyPreferences(Model.ttsVoiceId,Model.ttsVolume);
  }
  @override
  void dispose() {
    unawaited(TextToSpeech.stop());
  }
  Future<void> _onApply() async {
    await Model.setTtsEnabled(_ttsEnabled);
    await Model.setTtsVoiceId(_ttsVoiceId);
    await Model.setTtsVolume(_ttsVolume);
  }
  Widget _buildSpeechSettings(AppLocalizations l, TextTheme t) {
    if (TextToSpeech.ttsVoices.isEmpty) {
      return SizedBox.shrink();
    }
    return Column(children:[
      SettingCard.bottom(
        child: ListTile(
          contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
          title: null,
          subtitle: DropdownButtonFormField<String>(
            initialValue: () {
              if (_ttsVoiceId.isNotEmpty && TextToSpeech.ttsVoices.any((o) => o.id == _ttsVoiceId)) {
                return _ttsVoiceId;
              }
              return TextToSpeech.ttsVoices.first.id;
            }(),
            items: TextToSpeech.buildGroupedItems(),
            onChanged: (v) {
              if (v == null) {
                return;
              }
              setState(() => _ttsVoiceId = v);
            },
          ),
        ),
      )
    ]
  }
}
*/

import 'dart:ui';

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

class TtsOption {
  final String locale;
  final String name;
  const TtsOption(this.locale, this.name);

  String get id => '$locale|$name';
  String get label => '$locale $name';
}

class TextToSpeech {
  static final FlutterTts _tts = FlutterTts();
  static final List<TtsOption> ttsVoices = [];
  static String ttsVoiceId = '';
  static TextToSpeech? _instance;
  static bool _initialized = false;

  TextToSpeech._internal();

  static Future<TextToSpeech> _getInstance() async {
    _instance ??= TextToSpeech._internal();
    if (!_initialized) {
      _initialized = true;
      await _instance!._initial();
    }
    return _instance!;
  }

  // 初期化:音声リスト作成
  Future<void> _initial() async {
    try {
      final vs = await _tts.getVoices;
      ttsVoices.clear();

      if (vs is List) {
        for (final v in vs) {
          if (v is Map && v['name'] is String && v['locale'] is String) {
            ttsVoices.add(TtsOption(v['locale']!, v['name']!));
          }
        }
      }

      // Default を定義(表示用)
      const defaultVoice = TtsOption("Default", "");

      if (ttsVoices.isEmpty) {
        // 取得できなかった → Default のみ
        ttsVoices.add(defaultVoice);
      } else {
        // Default を先頭に固定
        ttsVoices.removeWhere((v) => v.id == defaultVoice.id);
        ttsVoices.sort((a, b) => a.label.compareTo(b.label));
        ttsVoices.insert(0, defaultVoice);
      }
      // voiceId が存在しない場合は Default
      if (!ttsVoices.any((o) => o.id == ttsVoiceId)) {
        ttsVoiceId = ttsVoices.first.id;
      }
      await _setSpeechVoiceFromId();
    } catch (_) {}
  }

  // locale ごとにグループ化
  static Map<String, List<TtsOption>> _groupedVoices() {
    final Map<String, List<TtsOption>> grouped = {};
    for (final v in ttsVoices) {
      grouped.putIfAbsent(v.locale, () => []);
      grouped[v.locale]!.add(v);
    }
    return grouped;
  }

  //DropdownMenuItemを生成(Dividerで区切る)
  static List<DropdownMenuItem<String>> buildGroupedItems() {
    final grouped = _groupedVoicesDefaultFirst();
    final List<DropdownMenuItem<String>> items = [];
    bool first = true;
    grouped.forEach((locale, voices) {
      // 最初のグループの前には Divider を入れない
      if (!first) {
        items.add(
          const DropdownMenuItem<String>(
            enabled: false,
            value: null,
            child: Padding(
              padding: EdgeInsets.symmetric(vertical: 1),
              child: Divider(height: 1, thickness: 1),
            ),
          ),
        );
      }
      first = false;
      // グループ内の選択可能な項目
      for (final v in voices) {
        items.add(
          DropdownMenuItem<String>(
            value: v.id,
            child: Container(
              padding: const EdgeInsets.symmetric(vertical: 2),
              child: Text(v.label),
            ),
          ),
        );
      }
    });
    return items;
  }

  static Map<String, List<TtsOption>> _groupedVoicesSorted() {
    final grouped = _groupedVoices();
    // locale をアルファベット順にソート
    final sortedKeys = grouped.keys.toList()..sort();
    final Map<String, List<TtsOption>> sorted = {};
    for (final key in sortedKeys) {
      sorted[key] = grouped[key]!;
    }
    return sorted;
  }

  static Map<String, List<TtsOption>> _groupedVoicesDefaultFirst() {
    final grouped = _groupedVoices();
    // Default が存在しなければ通常のソート
    if (!grouped.containsKey("Default")) {
      return _groupedVoicesSorted();
    }
    // Default 以外の locale をソート
    final otherKeys = grouped.keys.where((k) => k != "Default").toList()..sort();
    final Map<String, List<TtsOption>> sorted = {};
    // Default を最上段に
    sorted["Default"] = grouped["Default"]!;
    // 残りをアルファベット順に追加
    for (final key in otherKeys) {
      sorted[key] = grouped[key]!;
    }
    return sorted;
  }

  // ttsVoiceId を登録
  static Future<void> _setTtsVoiceId(String newTtsVoiceId) async {
    await _getInstance();
    final exists = ttsVoices.any((o) => o.id == newTtsVoiceId);
    ttsVoiceId = exists ? newTtsVoiceId : ttsVoices.first.id;
    await _setSpeechVoiceFromId();
  }

  // 選択された voiceId を TTS に反映
  static Future<void> _setSpeechVoiceFromId() async {
    if (ttsVoices.isEmpty || ttsVoiceId.isEmpty) {
      return;
    }
    final idx = ttsVoiceId.indexOf('|');
    final locale = idx >= 0 ? ttsVoiceId.substring(0, idx) : '';
    final name = idx >= 0 ? ttsVoiceId.substring(idx + 1) : ttsVoiceId;
    // Default の場合 → デバイスのデフォルトロケールを使う
    if (locale == "Default") {
      final deviceLocale = PlatformDispatcher.instance.locale.toString(); //e.g. ja_JP
      final ttsLocale = deviceLocale.replaceAll('_', '-'); //e.g. ja-JP
      try {
        await _tts.setLanguage(ttsLocale);
      } catch (_) {}
      return;
    }
    // 通常の処理
    try {
      if (locale.isNotEmpty) {
        await _tts.setLanguage(locale);
      }
      await _tts.setVoice({'name': name, 'locale': locale});
    } catch (_) {}
  }


  // 設定反映
  static Future<void> applyPreferences(String ttsVoiceId, double ttsVolume) async {
    await TextToSpeech._getInstance();
    await TextToSpeech._setTtsVoiceId(ttsVoiceId);
    await TextToSpeech._setVolume(ttsVolume);
  }

  // 音量
  static Future<void> _setVolume(double volume) async {
    try {
      await _tts.setVolume(volume);
    } catch (_) {}
  }

  // 音声再生
  static Future<void> speak(String text) async {
    try {
      await _getInstance();
      await _tts.speak(text);
    } catch (_) {}
  }

  // 停止
  static Future<void> stop() async {
    try {
      await _tts.stop();
    } catch (_) {}
  }

  // ピッチ
  static Future<void> setPitch(double pitch) async {
    try {
      await _tts.setPitch(pitch);
    } catch (_) {}
  }

  // 速度
  static Future<void> setSpeechRate(double speechRate) async {
    try {
      await _tts.setSpeechRate(speechRate);
    } catch (_) {}
  }
}
/// Copyright© ao-system, Inc.

import 'package:flutter/material.dart';

class ThemeColor {
  final int? themeNumber;
  final BuildContext context;

  ThemeColor({this.themeNumber, required this.context});

  Brightness get _effectiveBrightness {
    switch (themeNumber) {
      case 1:
        return Brightness.light;
      case 2:
        return Brightness.dark;
      default:
        return Theme.of(context).brightness;
    }
  }

  bool get _isLight => _effectiveBrightness == Brightness.light;

  Color get mainBackColor => _isLight ? Color(0xFFFE0000) : Color.fromRGBO(90, 0, 0, 1.0);
  Color get mainForeColor => _isLight ? Color.fromRGBO(255, 255, 255, 0.5) : Color.fromRGBO(255, 255, 255, 0.5);
  //
  Color get backColor => _isLight ? Colors.grey[200]! : Colors.grey[900]!;
  Color get cardColor => _isLight ? Colors.white : Colors.grey[800]!;
  Color get appBarForegroundColor => _isLight ? Colors.grey[700]! : Colors.white70;
  Color get dropdownColor => cardColor;
  Color get backColorMono => _isLight ? Colors.white : Colors.black;
  Color get foreColorMono => _isLight ? Colors.black : Colors.white;

}
/// Copyright© ao-system, Inc.

import 'package:flutter/material.dart';

class ThemeModeNumber {

  static ThemeMode numberToThemeMode(int value) {
    switch (value) {
      case 1:
        return ThemeMode.light;
      case 2:
        return ThemeMode.dark;
      default:
        return ThemeMode.system;
    }
  }

}