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: bingoonline
description: "bingoonline"

publish_to: 'none'

version: 1.6.1+30

environment:
  sdk: ^3.11.5

dependencies:        #flutter pub upgrade --major-versions
  flutter:
    sdk: flutter
  flutter_localizations:     # flutter gen-l10n
    sdk: flutter
  intl: ^0.20.2
  package_info_plus: ^10.1.0
  shared_preferences: ^2.0.17
  video_player: ^2.5.1
  flutter_svg: ^2.0.10+1
  http: ^1.0.0
  flutter_tts: ^4.0.2
  google_mobile_ads: ^8.0.0
  audioplayers: ^6.5.1
  google_fonts: ^8.0.2
  collection: ^1.18.0
  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.3.2     #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: '#3DF99D'
  image: 'assets/image/splash.png'
  color_dark: '#3DF99D'
  image_dark: 'assets/image/splash.png'
  fullscreen: true
  android_12:
    icon_background_color: '#3DF99D'
    image: 'assets/image/splash.png'
    icon_background_color_dark: '#3DF99D'
    image_dark: 'assets/image/splash.png'

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

  assets:
    - assets/image/
    - assets/movie/
    - assets/sound/

/// Copyright© ao-system, Inc.

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

import 'package:bingoonline/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:bingoonline/_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:bingoonline/l10n/app_localizations.dart';
import 'package:bingoonline/_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:audioplayers/audioplayers.dart';

import 'package:bingoonline/model.dart';

class AudioPlay {
  late AudioPlayer _playerMachine;

  //constructor
  AudioPlay() {
    constructor();
  }
  void constructor() async {
    _playerMachine = AudioPlayer();
    await _playerMachine.setSource(AssetSource('sound/machine.wav'));
  }
  void dispose() {
    _playerMachine.dispose();
  }

  Future<void> initMachine(double audioRatio) async {
    await _playerMachine.setReleaseMode(ReleaseMode.stop);
    await _playerMachine.setVolume(Model.machineVolume);
    await _playerMachine.setPlaybackRate(audioRatio);
  }

  Future<void> playMachine() async {
    if (Model.machineVolume == 0) {
      return;
    }
    await _playerMachine.seek(Duration.zero);
    await _playerMachine.resume();
  }

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

class CardCell {
  int number = 0;
  int open = 0;
  CardCell(this.number, this.open); //constructor
  int getNumber() {
    return number;
  }
  int getOpen() {
    return open;
  }
  void setNumber(int number) {
    this.number = number;
  }
  void setOpen(int open) {
    this.open = open;
  }
}
/// Copyright© ao-system, Inc.

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

import 'package:bingoonline/l10n/app_localizations.dart';
import 'package:bingoonline/model.dart';
import 'package:bingoonline/card_page.dart';
import 'package:bingoonline/card_offline.dart';
import 'package:bingoonline/ad_banner_widget.dart';
import 'package:bingoonline/theme_color.dart';
import 'package:bingoonline/main.dart';


class CardInitPage extends StatefulWidget {
  const CardInitPage({super.key});
  @override
  State<CardInitPage> createState() => _CardInitPageState();
}
class _CardInitPageState extends State<CardInitPage> {
  bool _onlineButtonDisable = false;
  String _message = '';
  String _messageConnectionCodeCard = '';
  int _inputNumber = 0;
  late ThemeColor _themeColor;

  @override
  void initState() {
    super.initState();
    Model.cardIsInitialToNew();
    Model.setConnectionCodeCardWatchFlag(false);
  }

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

  @override
  Widget build(BuildContext context) {
    final l = AppLocalizations.of(context)!;
    final int number = Model.connectionCodeCard;
    _messageConnectionCodeCard = '${l.currentConnectionCode}: ${number == 0 ? l.none : number.toString()}';
    return Theme(
      data: ThemeData(
        colorScheme: ColorScheme.fromSeed(
          seedColor: Colors.orange,
          brightness: _themeColor.isLight ? Brightness.light : Brightness.dark,
        ),
        useMaterial3: true,
      ),
      child: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          elevation: 0,
          title: Text(l.cardSetting, style: TextStyle(color: _themeColor.cardInitColor)),
        ),
        body: SafeArea(
          child: Column(children:[
            Expanded(
              child: GestureDetector(
                onTap: () => FocusScope.of(context).unfocus(),  //背景タップでキーボードを仕舞う
                child: SingleChildScrollView(
                  child: Padding(
                    padding: const EdgeInsets.only(top: 0, left: 12, right: 12, bottom: 100),
                    child: Column(
                      children: [
                        _buildSection1(l),
                        _buildSection2(l),
                        _buildSection3(l),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ]),
        ),
        bottomNavigationBar: AdBannerWidget(adManager: MainApp.of(context).adManager),
      )
    );
  }

  Widget _buildSection1(AppLocalizations l) {
    return SizedBox(
      width: double.infinity,
      child: Card(
        margin: const EdgeInsets.only(top: 12),
        color: _themeColor.cardColor,
        elevation: 0,
        shadowColor: Colors.transparent,
        surfaceTintColor: Colors.transparent,
        child: Padding(
          padding: const EdgeInsets.only(left: 16, right: 16, top: 16, bottom: 16),
          child: Column(
            children: [
              SizedBox(
                child: Text(l.enterTheConnectionCodeIssuedByTheOrganizerToJoinTheGame,
                  style: Theme.of(context).textTheme.bodySmall,
                  textAlign: TextAlign.left,
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(top: 16),
                child: SizedBox(
                  child: Text(l.connectionCode6DigitNumber,
                    style: Theme.of(context).textTheme.bodySmall,
                    textAlign: TextAlign.center,
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(top: 1, left: 60, right: 60),
                child: TextField(
                  textAlign: TextAlign.center,
                  keyboardType: TextInputType.number, //数字のキーボード
                  inputFormatters: [FilteringTextInputFormatter.digitsOnly],
                  //初期値を設定する場合 controller: TextEditingController(text: 'aaa'),
                  decoration: const InputDecoration(hintText: '000000',
                    hintStyle: TextStyle(color: Colors.black26)
                  ),
                  style: const TextStyle(fontSize: 30),
                  onChanged: (text) {
                    _inputNumber = int.tryParse(text) ?? 0;
                  },
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(top: 16),
                child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children:[
                      _onlineGame(l),
                      const SizedBox(height:20),
                      _offlineGame(l),
                    ]
                )
              ),
              Padding(
                padding: const EdgeInsets.only(top: 16),
                child: SizedBox(
                  child: Text(_message,
                    style: Theme.of(context).textTheme.bodySmall?.copyWith(color: Colors.redAccent),
                    textAlign: TextAlign.center,
                  ),
                ),
              ),
            ]
          )
        )
      )
    );
  }

  Widget _buildSection2(AppLocalizations l) {
    return SizedBox(
      width: double.infinity,
      child: Card(
        margin: const EdgeInsets.only(top: 12),
        color: _themeColor.cardColor,
        elevation: 0,
        shadowColor: Colors.transparent,
        surfaceTintColor: Colors.transparent,
        child: Padding(
          padding: const EdgeInsets.only(left: 16, right: 16, top: 16, bottom: 16),
          child: Column(
            children: [
              SizedBox(
                child: Text(l.replaceWithANewCard,
                  style: Theme.of(context).textTheme.bodySmall,
                  textAlign: TextAlign.left,
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(top: 4),
                child: _newCard(l),
              ),
            ]
          )
        )
      )
    );
  }

  Widget _buildSection3(AppLocalizations l) {
    return SizedBox(
      width: double.infinity,
      child: Card(
        margin: const EdgeInsets.only(top: 12),
        color: _themeColor.cardColor,
        elevation: 0,
        shadowColor: Colors.transparent,
        surfaceTintColor: Colors.transparent,
        child: Padding(
          padding: const EdgeInsets.only(left: 16, right: 16, top: 16, bottom: 16),
          child: Column(
            children: [
              SizedBox(
                child: Text(_messageConnectionCodeCard,
                  style: Theme.of(context).textTheme.bodySmall?.copyWith(color: _themeColor.cardInitColor),
                  textAlign: TextAlign.center,
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(top: 4),
                child: _cardPage(l),
              ),
            ]
          )
        )
      )
    );
  }

  Widget _onlineGame(AppLocalizations l) {
    return OutlinedButton.icon(
      onPressed: _onlineButtonDisable
          ? null
          : () {
        setState(() {
          _onlineButtonDisable = true;
        });
        _connectionCodeCardJoin(l);
      },
      style: Theme.of(context).outlinedButtonTheme.style?.copyWith(
        foregroundColor: WidgetStateProperty.all(_themeColor.cardInitColor),
        side: WidgetStateProperty.all(
          BorderSide(color: _themeColor.cardInitColor),
        ),
      ),
      icon: const Icon(Icons.cloud_outlined),
      label: Text(l.joinOnline),
    );
  }

  Widget _offlineGame(AppLocalizations l) {
    return OutlinedButton.icon(
      onPressed: () {
        setState(() {
          Model.setConnectionCodeCard(0);
          _message = l.joinedOfflineWithoutUsingAConnectionCode;
        });
      },
      style: Theme.of(context).outlinedButtonTheme.style?.copyWith(
        foregroundColor: WidgetStateProperty.all(_themeColor.cardInitColor),
        side: WidgetStateProperty.all(
          BorderSide(color: _themeColor.cardInitColor),
        ),
      ),
      icon: const Icon(Icons.cloud_off_outlined),
      label: Text(l.joinOffline),
    );
  }

  Widget _newCard(AppLocalizations l) {
    return OutlinedButton.icon(
      onPressed: () {
        showDialog(
          context: context,
          builder: (_) {
            return AlertDialog(
              title: Text(l.newCard),
              content: Text(l.replaceWithANewCardIsItOk),
              actions: [
                TextButton(
                  onPressed: () => Navigator.pop(context),
                  child: Text(l.cancel),
                ),
                TextButton(
                  onPressed: () {
                    Model.cardNew();
                    Navigator.pop(context);
                  },
                  child: Text(l.ok),
                ),
              ],
            );
          },
        );
      },
      style: Theme.of(context).outlinedButtonTheme.style?.copyWith(
        foregroundColor: WidgetStateProperty.all(_themeColor.cardInitColor),
        side: WidgetStateProperty.all(
          BorderSide(color: _themeColor.cardInitColor),
        ),
      ),
      icon: const Icon(Icons.insert_drive_file_outlined),
      label: Text(l.newCard),
    );
  }

  Widget _cardPage(AppLocalizations l) {
    return Directionality(
      textDirection: TextDirection.rtl,
      child: ElevatedButton.icon(
        onPressed: () {
          if (Model.connectionCodeCard == 0) {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => const CardOfflinePage()),
            );
          } else {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => const CardPage()),
            );
          }
        },
        style: Theme.of(context).elevatedButtonTheme.style?.copyWith(
          foregroundColor: WidgetStateProperty.all(Colors.white),
          backgroundColor: WidgetStateProperty.all(_themeColor.cardInitColor),
          elevation: WidgetStateProperty.all(2),
        ),
        icon: const Icon(Icons.chevron_left),
        label: Text(l.bingoCard),
      ),
    );
  }


  void _connectionCodeCardJoin(AppLocalizations l) async {
    if (_inputNumber < 100000 || _inputNumber > 999999) {
      setState(() {
        _message = l.wrongNumber;
      });
      _onlineButtonDisable = false;
      return;
    }
    final int number = await Model.connectionCodeCardJoin(_inputNumber);
    for (int i = 5; i > 0; i--) {
      await Future.delayed(const Duration(milliseconds: 1000));
      setState(() {
        _message = '${l.connecting}.$i';
      });
    }
    if (number == 0) {
      setState(() {
        _message = l.wrongNumberOrHasExpired;
      });
    } else {
      setState(() {
        _message = '${l.connected}: $number';
      });
    }
    _onlineButtonDisable = false;
  }

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

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

import 'package:bingoonline/l10n/app_localizations.dart';
import 'package:bingoonline/model.dart';
import 'package:bingoonline/ad_banner_widget.dart';
import 'package:bingoonline/theme_color.dart';
import 'package:bingoonline/main.dart';

class CardOfflinePage extends StatefulWidget {
  const CardOfflinePage({super.key});
  @override
  State<CardOfflinePage> createState() => _CardOfflinePageState();
}
class _CardOfflinePageState extends State<CardOfflinePage> with WidgetsBindingObserver {
  late ThemeColor _themeColor;

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

  void _initState() async {
    _wakelock();
    Model.cardCellSetOpenClose();
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    WakelockPlus.disable();
    super.dispose();
  }

  //need with WidgetsBindingObserver
  //WidgetsBinding.instance.addObserver(this);
  //WidgetsBinding.instance.removeObserver(this);
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.resumed:
        _wakelock();
        break;
      case AppLifecycleState.inactive:
      case AppLifecycleState.paused:
      case AppLifecycleState.detached:
      case AppLifecycleState.hidden:
        WakelockPlus.disable();
        break;
    }
  }

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

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

  void _cellTap(int ptr) {
    final int num = Model.cardCellPtrGetNumber(ptr);
    if (Model.ballHistoryContain(num)) {
      setState(() {
        Model.ballHistoryRemove(num);
      });
    } else {
      setState(() {
        Model.ballHistoryAdd(num);
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    final l = AppLocalizations.of(context)!;
    return Scaffold(
      backgroundColor: _themeColor.cardBackColor,
      appBar: AppBar(
        centerTitle: true,
        elevation: 0,
        title: Text(l.cardOffline),
        foregroundColor: const Color.fromRGBO(255,255,255,1),
        backgroundColor: Colors.transparent,
      ),
      body: SafeArea(
        child: Column(children:[
          Expanded(
            child: SingleChildScrollView(
              child: Padding(
                padding: const EdgeInsets.only(left: 16, right: 16, top: 8, bottom: 100),
                child: Column(
                  children: [
                    Container(
                      decoration: const BoxDecoration(
                        boxShadow: [
                          BoxShadow(
                            color: Color.fromRGBO(0,0,0,0.1),
                            spreadRadius: 2,
                            blurRadius: 2,
                            offset: Offset(0,1), // changes position of shadow
                          ),
                        ],
                      ),
                      child: Table(
                        border: TableBorder.all(width: 4, color: _themeColor.cardFrameColor),
                        columnWidths: const <int, TableColumnWidth>{
                          0: FlexColumnWidth(1),
                          1: FlexColumnWidth(1),
                          2: FlexColumnWidth(1),
                          3: FlexColumnWidth(1),
                          4: FlexColumnWidth(1),
                        },
                        children: [
                          TableRow(children:[_cellBingo('B'),_cellBingo('I'),_cellBingo('N'),_cellBingo('G'),_cellBingo('O')]),
                          TableRow(children:[
                            InkWell(onTap:(){_cellTap(0);},child:_cellNumber(0)),
                            InkWell(onTap:(){_cellTap(5);},child:_cellNumber(5)),
                            InkWell(onTap:(){_cellTap(10);},child:_cellNumber(10)),
                            InkWell(onTap:(){_cellTap(15);},child:_cellNumber(15)),
                            InkWell(onTap:(){_cellTap(20);},child:_cellNumber(20)),
                          ]),
                          TableRow(children:[
                            InkWell(onTap:(){_cellTap(1);},child:_cellNumber(1)),
                            InkWell(onTap:(){_cellTap(6);},child:_cellNumber(6)),
                            InkWell(onTap:(){_cellTap(11);},child:_cellNumber(11)),
                            InkWell(onTap:(){_cellTap(16);},child:_cellNumber(16)),
                            InkWell(onTap:(){_cellTap(21);},child:_cellNumber(21)),
                          ]),
                          TableRow(children:[
                            InkWell(onTap:(){_cellTap(2);},child:_cellNumber(2)),
                            InkWell(onTap:(){_cellTap(7);},child:_cellNumber(7)),
                            InkWell(onTap:(){_cellTap(12);},child:_cellNumber(12)),
                            InkWell(onTap:(){_cellTap(17);},child:_cellNumber(17)),
                            InkWell(onTap:(){_cellTap(22);},child:_cellNumber(22)),
                          ]),
                          TableRow(children:[
                            InkWell(onTap:(){_cellTap(3);},child:_cellNumber(3)),
                            InkWell(onTap:(){_cellTap(8);},child:_cellNumber(8)),
                            InkWell(onTap:(){_cellTap(13);},child:_cellNumber(13)),
                            InkWell(onTap:(){_cellTap(18);},child:_cellNumber(18)),
                            InkWell(onTap:(){_cellTap(23);},child:_cellNumber(23)),
                          ]),
                          TableRow(children:[
                            InkWell(onTap:(){_cellTap(4);},child:_cellNumber(4)),
                            InkWell(onTap:(){_cellTap(9);},child:_cellNumber(9)),
                            InkWell(onTap:(){_cellTap(14);},child:_cellNumber(14)),
                            InkWell(onTap:(){_cellTap(19);},child:_cellNumber(19)),
                            InkWell(onTap:(){_cellTap(24);},child:_cellNumber(24)),
                          ]),
                        ],
                      ),
                    ),
                    Container(height:80),
                  ],
                ),
              ),
            ),
          ),
        ]),
      ),
      bottomNavigationBar: AdBannerWidget(adManager: MainApp.of(context).adManager),
    );
  }

  Widget _cellBingo(String str) {
    return Container(
      padding: const EdgeInsets.all(5),
      color: _themeColor.cardFrameColor,
      child: Center(child: Text(str, style: TextStyle(color: Colors.white))),
    );
  }

  Widget _cellNumber(int ptr) {
    final int num = Model.cardCellPtrGetNumber(ptr);
    final bool openClose = Model.cardCellPtrGetOpenBool(ptr);
    final String numStr = (num == 0) ? 'FREE' : num.toString();
    final double fontSize = (num == 0) ? 20 : 30;
    Container openMark = Container(
      margin: const EdgeInsets.all(10),
      decoration: BoxDecoration(
        shape: BoxShape.circle,
        color: const Color.fromRGBO(255,0,0,0.3),
        border: Border.all(width: 4,color: const Color.fromRGBO(255,50,0,0.2)),
      ),
    );
    Container closeMark = Container();
    return AspectRatio(
      aspectRatio: 1,
      child: Stack(
        children: [
          Container(
            color: _themeColor.cardCellColor,
            child: Center(child: Text(numStr,
              style: TextStyle(
                fontSize: fontSize,
              ),
            )),
          ),
          openClose ? openMark : closeMark,
        ],
      ),
    );
  }

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

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:wakelock_plus/wakelock_plus.dart';

import 'package:bingoonline/l10n/app_localizations.dart';
import 'package:bingoonline/model.dart';
import 'package:bingoonline/progress_table.dart';
import 'package:bingoonline/ad_banner_widget.dart';
import 'package:bingoonline/theme_color.dart';
import 'package:bingoonline/main.dart';

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

class _CardPageState extends State<CardPage> with WidgetsBindingObserver {
  late Timer _timer;
  bool _timerToggle = false;
  late ThemeColor _themeColor;

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

  void _initState() async {
    _wakelock();
    Model.cardCellSetOpenClose();
    Model.setConnectionCodeCardWatchFlag(true);
    _timer = Timer.periodic(const Duration(seconds: 5), (timer) {
      _timerToggle = _timerToggle ? false : true;
      Model.connectionCodeCardWatch();
      setState(() {});
      if (Model.connectionCodeCardWatchFlag == false) {
        _timer.cancel();
      }
    });
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    WakelockPlus.disable();
    Model.setConnectionCodeCardWatchFlag(false);
    _timer.cancel();
    super.dispose();
  }

  //need with WidgetsBindingObserver
  //WidgetsBinding.instance.addObserver(this);
  //WidgetsBinding.instance.removeObserver(this);
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.resumed:
        _wakelock();
        break;
      case AppLifecycleState.inactive:
      case AppLifecycleState.paused:
      case AppLifecycleState.detached:
      case AppLifecycleState.hidden:
        WakelockPlus.disable();
        break;
    }
  }

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

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

  @override
  Widget build(BuildContext context) {
    final l = AppLocalizations.of(context)!;
    return Scaffold(
      backgroundColor: _themeColor.cardBackColor,
      appBar: AppBar(
        centerTitle: true,
        elevation: 0,
        title: Text((Model.connectionCodeCard == 0) ? l.card : Model.connectionCodeCard.toString()),
        backgroundColor: Colors.transparent,
      ),
      body: SafeArea(
        child: Column(children:[
          Expanded(
            child: SingleChildScrollView(
              child: Padding(
                padding: const EdgeInsets.only(left: 16, right: 16, top: 8, bottom: 100),
                child: Column(
                  children: [
                    Container(
                      decoration: const BoxDecoration(
                        boxShadow: [
                          BoxShadow(
                            color: Color.fromRGBO(0,0,0,0.1),
                            spreadRadius: 2,
                            blurRadius: 2,
                            offset: Offset(0,1), // changes position of shadow
                          ),
                        ],
                      ),
                      child: Table(
                        border: TableBorder.all(width: 4, color: _themeColor.cardFrameColor),
                        columnWidths: const <int, TableColumnWidth>{
                          0: FlexColumnWidth(1),
                          1: FlexColumnWidth(1),
                          2: FlexColumnWidth(1),
                          3: FlexColumnWidth(1),
                          4: FlexColumnWidth(1),
                        },
                        children: [
                          TableRow(children:[_cellBingo('B'),_cellBingo('I'),_cellBingo('N'),_cellBingo('G'),_cellBingo('O')]),
                          TableRow(children:[_cellNumber(0),_cellNumber(5),_cellNumber(10),_cellNumber(15),_cellNumber(20)]),
                          TableRow(children:[_cellNumber(1),_cellNumber(6),_cellNumber(11),_cellNumber(16),_cellNumber(21)]),
                          TableRow(children:[_cellNumber(2),_cellNumber(7),_cellNumber(12),_cellNumber(17),_cellNumber(22)]),
                          TableRow(children:[_cellNumber(3),_cellNumber(8),_cellNumber(13),_cellNumber(18),_cellNumber(23)]),
                          TableRow(children:[_cellNumber(4),_cellNumber(9),_cellNumber(14),_cellNumber(19),_cellNumber(24)]),
                        ],
                      ),
                    ),
                    Container(height:30),
                    ProgressTable().table(context, _themeColor),
                  ],
                ),
              ),
            ),
          ),
        ]),
      ),
      bottomNavigationBar: AdBannerWidget(adManager: MainApp.of(context).adManager),
    );
  }

  Widget _cellBingo(String str) {
    return Container(
      padding: const EdgeInsets.all(5),
      color: _themeColor.cardFrameColor,
      child: Center(child: Text(str,
        style: TextStyle(color: _timerToggle ? Colors.black : Colors.white),
      )),
    );
  }

  Widget _cellNumber(int ptr) {
    final int num = Model.cardCellPtrGetNumber(ptr);
    final bool openClose = Model.cardCellPtrGetOpenBool(ptr);
    final String numStr = (num == 0) ? 'FREE' : num.toString();
    final double fontSize = (num == 0) ? 20 : 30;
    Container openMark = Container(
      margin: const EdgeInsets.all(10),
      decoration: BoxDecoration(
        shape: BoxShape.circle,
        color: const Color.fromRGBO(255,0,0,0.3),
        border: Border.all(width: 4,color: const Color.fromRGBO(255,50,0,0.2)),
      ),
    );
    Container closeMark = Container();
    return AspectRatio(
      aspectRatio: 1,
      child: Stack(
        children: [
          Container(
            color: _themeColor.cardCellColor,
            child: Center(child: Text(numStr,
              style: TextStyle(
                fontSize: fontSize,
              ),
            )),
          ),
          openClose ? openMark : closeMark,
        ],
      ),
    );
  }

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

class ConstValue {
  ConstValue._();

  //image
  static const String topBack = 'assets/image/machine_card.webp';


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

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

import 'package:bingoonline/l10n/app_localizations.dart';
import 'package:bingoonline/const_value.dart';
import 'package:bingoonline/setting_page.dart';
import 'package:bingoonline/machine_init_page.dart';
import 'package:bingoonline/card_init_page.dart';
import 'package:bingoonline/ad_banner_widget.dart';
import 'package:bingoonline/theme_color.dart';
import 'package:bingoonline/loading_screen.dart';
import 'package:bingoonline/model.dart';
import 'package:bingoonline/main.dart';

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

class _MainHomePageState extends State<MainHomePage> with WidgetsBindingObserver {
  late ThemeColor _themeColor;
  bool _isReady = false;

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

  void _initState() async {
    _themeColor = ThemeColor(themeNumber: Model.themeNumber, context: context);
    _wakelock();
    setState(() {
      _isReady = true;
    });
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    WakelockPlus.disable();
    super.dispose();
  }

  //need with WidgetsBindingObserver
  //WidgetsBinding.instance.addObserver(this);
  //WidgetsBinding.instance.removeObserver(this);
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.resumed:
        _wakelock();
        break;
      case AppLifecycleState.inactive:
      case AppLifecycleState.paused:
      case AppLifecycleState.detached:
      case AppLifecycleState.hidden:
        WakelockPlus.disable();
        break;
    }
  }

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

  Future<void> _openSetting() async {
    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();
    }
    if (mounted) {
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    if (!_isReady) {
      return LoadingScreen();
    }
    return Scaffold(
      backgroundColor: _themeColor.machineBackColor,
      body: SafeArea(
        child: Stack(
          children: [
            Container(
              decoration: const BoxDecoration(
                image: DecorationImage(
                  image: AssetImage(ConstValue.topBack),
                  fit: BoxFit.cover,
                ),
              ),
            ),
            SafeArea(
              child: Padding(
                padding: const EdgeInsets.only(top: 5, left: 10, right: 10, bottom: 0),
                child: Row(children:[
                  const Spacer(),
                  OutlinedButton(
                    onPressed: _openSetting,
                    style: OutlinedButton.styleFrom(
                      foregroundColor: const Color.fromRGBO(0,0,0,0.4),
                      side: const BorderSide(
                        color: Color.fromRGBO(0,0,0,0.4),
                      ),
                    ),
                    child: Text(AppLocalizations.of(context)!.setting),
                  ),
                ]),
              ),
            ),
            Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Directionality(
                    textDirection: TextDirection.rtl,
                    child: ElevatedButton.icon(
                      onPressed: () {
                        Navigator.push(
                          context,
                          MaterialPageRoute<void>(
                            builder: (BuildContext context) => const MachineInitPage(),
                          ),
                        );
                      },
                      style: ElevatedButton.styleFrom(
                        foregroundColor: const Color.fromRGBO(255,255,255,1),
                        backgroundColor: const Color.fromRGBO(0,0,0,0.3),
                        elevation: 0,
                      ),
                      icon: const Icon(Icons.chevron_left),
                      label: Text(AppLocalizations.of(context)!.bingoMachine,style: const TextStyle(fontSize:20)),
                    ),
                  ),
                  Container(height: 30),
                  Directionality(
                    textDirection: TextDirection.rtl,
                    child: ElevatedButton.icon(
                      onPressed: () {
                        Navigator.push(
                          context,
                          MaterialPageRoute<void>(
                            builder: (BuildContext context) => const CardInitPage(),
                          ),
                        );
                      },
                      style: ElevatedButton.styleFrom(
                        foregroundColor: const Color.fromRGBO(255,255,255,1),
                        backgroundColor: const Color.fromRGBO(0,0,0,0.3),
                        elevation: 0,
                      ),
                      icon: const Icon(Icons.chevron_left),
                      label: Text(AppLocalizations.of(context)!.bingoCard,style: const TextStyle(fontSize:20)),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
      bottomNavigationBar: AdBannerWidget(adManager: MainApp.of(context).adManager),
    );
  }
}
/// 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 'package:flutter/material.dart';

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

class HistoryTable {
  late ThemeColor _themeColor;

  Widget table(BuildContext context, ThemeColor themeColor) {
    _themeColor = themeColor;

    return Wrap(
      spacing: 2, //セル間の横スペース
      runSpacing: 2, //セル間の縦スペース
      children: List.generate(75, (index) {
        return SizedBox(
          width: MediaQuery.of(context).size.width / 10 - 4,
          child: cellHistory(index),
        );
      }),
    );
  }

  Widget cellHistory(int ptr) {
    if (Model.ballHistoryLength() > ptr) {
      return Container(
        padding: const EdgeInsets.symmetric(vertical: 2),
        decoration: BoxDecoration(
          color: (Model.ballHistoryLength() - 1 == ptr)
              ? _themeColor.progressCellColor2
              : _themeColor.progressCellColor1,
          borderRadius: BorderRadius.circular(64),
        ),
        child: Center(
          child: Text(
            Model.ballHistoryValue(ptr).toString(),
            style: const TextStyle(
              fontSize: 16,
            ),
          ),
        ),
      );
    } else {
      return Container();
    }
  }

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

import 'package:flutter/material.dart';

import 'package:bingoonline/l10n/app_localizations.dart';
import 'package:bingoonline/machine_page.dart';
import 'package:bingoonline/model.dart';
import 'package:bingoonline/ad_banner_widget.dart';
import 'package:bingoonline/theme_color.dart';
import 'package:bingoonline/main.dart';


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

class _MachineInitPageState extends State<MachineInitPage> {
  int _machineSpeed = 1;
  bool _onlineButtonDisable = false;
  String _connectionMessage = '';
  late ThemeColor _themeColor;

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

  @override
  Widget build(BuildContext context) {
    final l = AppLocalizations.of(context)!;
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        elevation: 0,
        title: Text(l.machineSetting, style: TextStyle(color: _themeColor.machineForeColor)),
      ),
      body: SafeArea(
        child: Column(children:[
          Expanded(
            child: GestureDetector(
              onTap: () => FocusScope.of(context).unfocus(),
              child: SingleChildScrollView(
                child: Padding(
                  padding: const EdgeInsets.only(top: 0, left: 12, right: 12, bottom: 100),
                  child: Column(
                    children: [
                      _buildSection1(l),
                      _buildSection2(l),
                      _buildSection3(l),
                    ],
                  ),
                ),
              ),
            ),
          ),
        ]),
      ),
      bottomNavigationBar: AdBannerWidget(adManager: MainApp.of(context).adManager),
    );
  }

  Widget _buildSection1(AppLocalizations l) {
    final bodySmall = Theme.of(context).textTheme.bodySmall;
    return SizedBox(
      width: double.infinity,
      child: Card(
        margin: const EdgeInsets.only(top: 12),
        color: _themeColor.cardColor,
        elevation: 0,
        shadowColor: Colors.transparent,
        surfaceTintColor: Colors.transparent,
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Text(
                l.giveTheConnectionCodeToTheParticipant,
                style: bodySmall,
                textAlign: TextAlign.left,
              ),
              const SizedBox(height: 8),
              Column(
                children: [
                  _onlineGame(l),
                  const SizedBox(height: 1),
                  _offlineGame(l),
                ],
              ),
              const SizedBox(height: 8),
              Text(
                l.connectionCode,
                style: bodySmall,
                textAlign: TextAlign.center,
              ),
              const SizedBox(height: 8),
              Text(
                Model.getConnectionCodeMachineStr(),
                style: const TextStyle(fontSize: 80),
                textAlign: TextAlign.center,
              ),
              const SizedBox(height: 8),
              Text(
                _connectionMessage,
                style: bodySmall?.copyWith(color: Colors.redAccent),
                textAlign: TextAlign.center,
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildSection2(AppLocalizations l) {
    final bodySmall = Theme.of(context).textTheme.bodySmall;
    return SizedBox(
      width: double.infinity,
      child: Card(
        margin: const EdgeInsets.only(top: 12),
        color: _themeColor.cardColor,
        elevation: 0,
        shadowColor: Colors.transparent,
        surfaceTintColor: Colors.transparent,
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Text(
                l.initializeTheLotteryResults,
                style: bodySmall,
              ),
              const SizedBox(height: 8),
              _newGame(l),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildSection3(AppLocalizations l) {
    return SizedBox(
      width: double.infinity,
      child: Card(
        margin: const EdgeInsets.only(top: 12),
        color: _themeColor.cardColor,
        elevation: 0,
        shadowColor: Colors.transparent,
        surfaceTintColor: Colors.transparent,
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
          child: Column(
            children: [
              _machinePage(l),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(l.speed),
                  const SizedBox(width: 16),
                  DropdownButton<int>(
                    value: _machineSpeed,
                    items: const [
                      DropdownMenuItem(value: 1, child: Text('1')),
                      DropdownMenuItem(value: 2, child: Text('2')),
                      DropdownMenuItem(value: 3, child: Text('3')),
                      DropdownMenuItem(value: 4, child: Text('4')),
                      DropdownMenuItem(value: 5, child: Text('5')),
                    ],
                    onChanged: (value) {
                      if (value == null) return;
                      setState(() {
                        _machineSpeed = value;
                      });
                    },
                  ),
                ],
              )
            ],
          ),
        ),
      ),
    );
  }

  Widget _onlineGame(AppLocalizations l) {
    return OutlinedButton.icon(
      onPressed: _onlineButtonDisable ? null : () {
        showDialog(
          context: context,
          builder: (_) {
            return AlertDialog(
              title: Text(l.heldOnline),
              content: Text(l.connectionCodeWillBeIssuedIsItOk),
              actions: <Widget>[
                TextButton(
                  child: Text(l.cancel),
                  onPressed: () {
                    Navigator.pop(context);
                  },
                ),
                TextButton(
                  child: Text(l.ok),
                  onPressed: () {
                    Navigator.pop(context);
                    _connectionCodeMachineCreate(l);
                  },
                ),
              ],
            );
          },
        );
      },
      style: OutlinedButton.styleFrom(
          foregroundColor: _themeColor.machineForeColor,
          side: BorderSide(
            color: _themeColor.machineForeColor,
          ),
      ),
      icon: const Icon(Icons.cloud_outlined),
      label: Text(l.heldOnline),
    );
  }

  Widget _offlineGame(AppLocalizations l) {
    return OutlinedButton.icon(
      onPressed: () {
        showDialog(
          context: context,
          builder: (_) {
            return AlertDialog(
              title: Text(l.heldOffline),
              content: Text(l.connectionCodeWillBeInvalidIsItOk),
              actions: <Widget>[
                TextButton(
                  child: Text(l.cancel),
                  onPressed: () => Navigator.pop(context),
                ),
                TextButton(
                  child: Text(l.ok),
                  onPressed: () => {
                    setState(() {
                      Model.setConnectionCodeMachine(0);
                      _connectionMessage = l.connectionCodeIsNoLongerValid;
                    }),
                    Navigator.pop(context),
                  },
                ),
              ],
            );
          },
        );
      },
      style: OutlinedButton.styleFrom(
        foregroundColor: _themeColor.machineForeColor,
        side: BorderSide(
          color: _themeColor.machineForeColor,
        ),
      ),
      icon: const Icon(Icons.cloud_off_outlined),
      label: Text(l.heldOffline),
    );
  }

  Widget _newGame(AppLocalizations l) {
    return OutlinedButton.icon(
      onPressed: () {
        showDialog(
          context: context,
          builder: (_) {
            return AlertDialog(
              title: Text(l.newGame),
              content: Text(l.lotteryResultIsInitializedIsItOk),
              actions: <Widget>[
                TextButton(
                  child: Text(l.cancel),
                  onPressed: () => Navigator.pop(context),
                ),
                TextButton(
                  child: Text(l.ok),
                  onPressed: () => {
                    _connectionCodeMachineClear(l),
                    Navigator.pop(context),
                  },
                ),
              ],
            );
          },
        );
      },
      style: OutlinedButton.styleFrom(
        foregroundColor: _themeColor.machineForeColor,
        side: BorderSide(
          color: _themeColor.machineForeColor,
        ),
      ),
      icon: const Icon(Icons.insert_drive_file_outlined),
      label: Text(l.newGame),
    );
  }

  Widget _machinePage(AppLocalizations l) {
    return Directionality(
        textDirection: TextDirection.rtl,
        child: ElevatedButton.icon(
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute<void>(
                builder: (BuildContext context) => MachinePage(machineSpeed: _machineSpeed),
              ),
            );
          },
          style: ElevatedButton.styleFrom(
            foregroundColor: const Color.fromRGBO(255,255,255,1),
            backgroundColor: _themeColor.machineForeColor,
            elevation: 2,
          ),
          icon: const Icon(Icons.chevron_left),
          label: Text(l.bingoMachine),
        )
    );
  }

  void _connectionCodeMachineCreate(AppLocalizations l) async {
    setState(() {
      _onlineButtonDisable = true;
      _connectionMessage = l.connectionCodeIsBeingIssued;
    });
    final int number = await Model.connectionCodeMachineCreate();
    for (int i = 10; i > 0; i--) {
      await Future.delayed(const Duration(milliseconds: 1000));
      setState(() {
        _connectionMessage = l.connectionCodeIsBeingIssued + i.toString();
      });
    }
    setState(() {
      _onlineButtonDisable = false;
      _connectionMessage = (number == 0) ? l.failedToConnectPleaseTryAgain : l.connectionCodeHasBeenIssued;
    });
  }

  void _connectionCodeMachineClear(AppLocalizations l) async {
    final bool ret = await Model.connectionCodeMachineClear();
    setState(() {
      _connectionMessage = (ret) ? l.theLotteryResultsHaveBeenInitialized : l.failedToInitializeLotteryResults;
    });
  }
}
/// Copyright© ao-system, Inc.

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:wakelock_plus/wakelock_plus.dart';

import 'package:bingoonline/loading_screen.dart';
import 'package:bingoonline/l10n/app_localizations.dart';
import 'package:bingoonline/model.dart';
import 'package:bingoonline/text_to_speech.dart';
import 'package:bingoonline/machine_history_table.dart';
import 'package:bingoonline/progress_table.dart';
import 'package:bingoonline/ad_banner_widget.dart';
import 'package:bingoonline/theme_color.dart';
import 'package:bingoonline/audio_play.dart';
import 'package:bingoonline/main.dart';

class MachinePage extends StatefulWidget {
  const MachinePage({super.key, required this.machineSpeed});
  final int machineSpeed;
  @override
  State<MachinePage> createState() => _MachinePageState();
}

class _MachinePageState extends State<MachinePage> with WidgetsBindingObserver {
  VideoPlayerController? _videoController;
  Completer<void>? _videoPreparingCompleter;
  late double _ballSizeMax;
  late double _ballTextSizeMax;
  Duration _movieDuration = const Duration(milliseconds: 7000);
  double _ballSize = 0;
  double _ballTextSize = 0;
  double _ballTextMarginSize = 0;
  Duration _ballAnimationDuration = const Duration(milliseconds: 0);
  final Duration _ballAnimationDurationMin = const Duration(milliseconds: 0);
  final Duration _ballAnimationDurationMax = const Duration(milliseconds: 500);
  int _ballNumber = 0;
  bool _startButtonIsDisabled = false;
  String _message = '';
  late AudioPlay _audioPlay;
  late ThemeColor _themeColor;
  bool _isReady = false;

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

  void _initState() async {
    _audioPlay = AudioPlay();
    unawaited(_initializeVideoPlayer());
    double audioRatio = 1.0;
    if (widget.machineSpeed == 1) {
      _movieDuration = const Duration(milliseconds: 6900);
      audioRatio = 1.0;
    } else if (widget.machineSpeed == 2) {
      _movieDuration = const Duration(milliseconds: 3300);
      audioRatio = 2.0;
    } else if (widget.machineSpeed == 3) {
      _movieDuration = const Duration(milliseconds: 2100);
      audioRatio = 3.0;
    } else if (widget.machineSpeed == 4) {
      _movieDuration = const Duration(milliseconds: 1600);
      audioRatio = 4.0;
    } else if (widget.machineSpeed == 5) {
      _movieDuration = const Duration(milliseconds: 1100);
      audioRatio = 5.0;
    }
    _audioPlay.initMachine(audioRatio);
    await TextToSpeech.applyPreferences(Model.ttsVoiceId,Model.ttsVolume);
    Future.delayed(Duration(milliseconds: 500), () {
      setState(() {
        _isReady = true;
      });
    });
  }
  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    WakelockPlus.disable();
    final controller = _videoController;
    _videoController = null;
    if (controller != null) {
      unawaited(controller.dispose());
    }
    _audioPlay.dispose();
    unawaited(TextToSpeech.stop());
    super.dispose();
  }

  //need with WidgetsBindingObserver
  //WidgetsBinding.instance.addObserver(this);
  //WidgetsBinding.instance.removeObserver(this);
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.resumed:
        _wakelock();
        break;
      case AppLifecycleState.inactive:
      case AppLifecycleState.paused:
      case AppLifecycleState.detached:
      case AppLifecycleState.hidden:
        WakelockPlus.disable();
        break;
    }
  }

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

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

  Future<VideoPlayerController> _createVideoController() async {
    final controller = VideoPlayerController.asset('assets/movie/bingo4mbps.mp4',
      videoPlayerOptions: VideoPlayerOptions(mixWithOthers: true),
    );
    await controller.initialize();
    return controller;
  }

  Future<void> _initializeVideoPlayer({bool autoPlay = false, bool recreate = false}) async {
    while (_videoPreparingCompleter != null) {
      await _videoPreparingCompleter!.future;
    }
    final completer = Completer<void>();
    _videoPreparingCompleter = completer;
    VideoPlayerController? previousController;
    bool replacedController = false;
    try {
      previousController = recreate ? _videoController : null;
      if (previousController != null) {
        try {
          await previousController.pause();
        } catch (_) {}
      }
      final controller = await _createVideoController();
      await controller.setPlaybackSpeed(widget.machineSpeed.toDouble());
      if (!mounted) {
        await controller.dispose();
        return;
      }
      setState(() {
        _videoController = controller;
      });
      replacedController = true;
      if (autoPlay) {
        try {
          await controller.play();
        } catch (_) {}
      }
    } finally {
      if (replacedController && previousController != null) {
        unawaited(previousController.dispose());
      }
      if (!completer.isCompleted) {
        completer.complete();
      }
      _videoPreparingCompleter = null;
    }
  }

  Future<void> _restartMachineVideo() async {
    await _initializeVideoPlayer(autoPlay: true, recreate: true);
  }

  void _updateBallMetrics(BuildContext context) {
    _ballSizeMax = MediaQuery.of(context).size.width / 2;
    _ballTextSizeMax = _ballSizeMax * 0.7;
    _ballTextMarginSize = (_ballSizeMax - _ballTextSizeMax) / 2;
  }

  int nextBallNumber() {
    List<int> numbers = [];
    for (int i = 1; i <= 75; i++) {
      if (Model.ballHistoryContain(i) == false) {
        numbers.add(i);
      }
    }
    if (numbers.isEmpty) {
      return 0;
    }
    numbers.shuffle();
    return numbers[0];
  }

  @override
  Widget build(BuildContext context) {
    if (!_isReady) {
      return LoadingScreen();
    }
    _updateBallMetrics(context);
    final int connectionCodeMachine = Model.connectionCodeMachine;
    final l = AppLocalizations.of(context)!;
    return Scaffold(
      backgroundColor: _themeColor.machineMovieBackColor,
      appBar: AppBar(
        centerTitle: true,
        elevation: 0,
        title: Text((connectionCodeMachine == 0) ? l.bingoMachine : connectionCodeMachine.toString()),
        backgroundColor: Colors.transparent,
      ),
      body: SafeArea(
        child: ListView(
          padding: const EdgeInsets.only(left: 8, right: 8, top: 0, bottom: 100),
          children: [
            _buildVideoSection(context),
            _buildMessageSection(),
            HistoryTable().table(context, _themeColor),
            const SizedBox(height: 20),
            ProgressTable().table(context, _themeColor),
          ],
        ),
      ),
      bottomNavigationBar: AdBannerWidget(adManager: MainApp.of(context).adManager),
    );
  }

  Widget _buildVideoSection(BuildContext context) {
    final l = AppLocalizations.of(context)!;
    return Stack(
      children: [
        AspectRatio(aspectRatio: 1,
          child: _buildVideoPlayer(),
        ),
        Positioned(
          bottom: 3,
          right: 7,
          child: OutlinedButton(
            onPressed: _startButtonIsDisabled
              ? null
              : () async {
                final endedMessage = l.ended;
                final failedMessage = l.failedToConnect;
                setState(() {
                  _startButtonIsDisabled = true;
                  _ballAnimationDuration = _ballAnimationDurationMin;
                  _ballSize = 0;
                  _ballTextSize = 0;
                  _message = '';
                });
                try {
                  unawaited(_audioPlay.playMachine());
                  await _restartMachineVideo();

                  await Future.delayed(_movieDuration);
                  if (!mounted) {
                    return;
                  }
                  setState(() {
                    _ballNumber = nextBallNumber();
                    _ballAnimationDuration = _ballAnimationDurationMax;
                    _ballSize = _ballSizeMax;
                    _ballTextSize = _ballTextSizeMax;
                  });
                  await Future.delayed(_ballAnimationDurationMax);
                  if (!mounted) {
                    return;
                  }
                  if (_ballNumber != 0 && Model.ttsEnabled) {
                    TextToSpeech.speak(_ballNumber.toString());
                  }
                  await Future.delayed(_ballAnimationDurationMax);
                  if (!mounted) {
                    return;
                  }
                  String message = '';
                  if (_ballNumber == 0) {
                    message = endedMessage;
                  } else {
                    final bool ret = await Model.ballHistoryAdd(_ballNumber);
                    if (!mounted) {
                      return;
                    }
                    if (!ret) {
                      message = failedMessage;
                    }
                  }
                  if (!mounted) {
                    return;
                  }
                  setState(() {
                    _message = message;
                  });
                  await Future.delayed(const Duration(milliseconds: 800));
                } finally {
                  if (mounted) {
                    setState(() {
                      _startButtonIsDisabled = false;
                    });
                  }
                }
              },
            style: OutlinedButton.styleFrom(
              foregroundColor: Colors.white,
              side: const BorderSide(
                color: Colors.white,
              ),
            ),
            child: Text(l.start),
          ),
        ),
        Container(
          margin: const EdgeInsets.only(left: 8, top: 8),
          child: Stack(
            children: [
              SizedBox(
                child: AnimatedContainer(
                  duration: _ballAnimationDuration,
                  width: _ballSize,
                  height: _ballSize,
                  child: SvgPicture.asset('assets/image/ball.svg'),
                ),
              ),
              Positioned(
                top: _ballTextMarginSize,
                left: _ballTextMarginSize,
                child: SizedBox(
                  child: AnimatedContainer(
                    duration: _ballAnimationDuration,
                    width: _ballTextSize,
                    height: _ballTextSize,
                    child: FittedBox(
                      fit: BoxFit.fitWidth,
                      child: Text(
                        (_ballNumber == 0) ? 'END' : _ballNumber.toString(),
                        style: GoogleFonts.ubuntu(color: Colors.black, fontWeight: FontWeight.w700),
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        )
      ],
    );
  }

  Widget _buildVideoPlayer() {
    final controller = _videoController;
    if (controller != null && controller.value.isInitialized) {
      return ClipRRect(
        borderRadius: BorderRadius.circular(14),
        child: VideoPlayer(controller),
      );
    }
    return Container(
      decoration: BoxDecoration(
        color: Colors.black,
        borderRadius: BorderRadius.circular(14),
      ),
    );
  }

  Widget _buildMessageSection() {
    return Padding(
      padding: const EdgeInsets.only(top: 2, bottom: 30),
      child: Text(_message,
        style: Theme.of(context).textTheme.bodySmall?.copyWith(color: Colors.redAccent),
        textAlign: TextAlign.center,
      ),
    );
  }

}
/// 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:bingoonline/l10n/app_localizations.dart';
import 'package:bingoonline/loading_screen.dart';
import 'package:bingoonline/model.dart';
import 'package:bingoonline/theme_mode_number.dart';
import 'package:bingoonline/parse_locale_tag.dart';
import 'package:bingoonline/home_page.dart';
import 'package:bingoonline/ad_ump_status.dart';
import 'package:bingoonline/att_service.dart';
import 'package:bingoonline/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.green;
    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:async';
import 'dart:convert' as convert;
import 'dart:ui' as ui;
import 'package:http/http.dart';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';

import 'package:bingoonline/card_cell.dart';
import 'package:bingoonline/l10n/app_localizations.dart';
import 'package:bingoonline/_secrets.dart';

class Model {
  Model._();

  static const String _prefConnectionCodeMachine = 'connectionCodeMachine';
  static const String _prefConnectionCodeMachineTimestamp = 'connectionCodeMachineTimestamp';
  static const String _prefConnectionCodeCard = 'connectionCodeCard';
  static const String _prefBallHistories = 'ballHistories';
  static const String _prefCardNumbers = 'cardNumbers';
  static const String _prefMachineVolume = 'machineVolume';
  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 int _connectionCodeMachine = 0;
  static int _connectionCodeMachineTimestamp = 0;
  static int _connectionCodeCard = 0;
  static List<int> _ballHistories = [];
  static List<int> _cardNumbers = [];
  static double _machineVolume = 1.0;
  static bool _ttsEnabled = true;
  static double _ttsVolume = 1.0;
  static String _ttsVoiceId = '';
  static bool _wakelockEnabled = false;
  static int _themeNumber = 0;
  static String _languageCode = '';
  //
  static bool _connectionCodeCardWatchFlag = false;
  static int _connectionErrorCount = 0;
  static int _lastBallHistoryLength = 0;
  static int _sameLastBallHistoryLengthCount = 0;

  static final List<CardCell> _cardCells = [
    CardCell(1, 0),
    CardCell(2, 0),
    CardCell(3, 0),
    CardCell(4, 0),
    CardCell(5, 0),
    CardCell(16, 0),
    CardCell(17, 0),
    CardCell(18, 0),
    CardCell(19, 0),
    CardCell(20, 0),
    CardCell(31, 0),
    CardCell(32, 0),
    CardCell(0, 1),
    CardCell(34, 0),
    CardCell(35, 0),
    CardCell(46, 0),
    CardCell(47, 0),
    CardCell(48, 0),
    CardCell(49, 0),
    CardCell(50, 0),
    CardCell(61, 0),
    CardCell(62, 0),
    CardCell(63, 0),
    CardCell(64, 0),
    CardCell(65, 0),
  ];

  static int get connectionCodeMachine => _connectionCodeMachine;
  static int get connectionCodeMachineTimestamp => _connectionCodeMachineTimestamp;
  static int get connectionCodeCard => _connectionCodeCard;
  static List<int> get ballHistories => _ballHistories;
  static List<int> get cardNumbers => _cardNumbers;
  static double get machineVolume => _machineVolume;
  static bool get ttsEnabled => _ttsEnabled;
  static double get ttsVolume => _ttsVolume;
  static String get ttsVoiceId => _ttsVoiceId;
  static bool get wakelockEnabled => _wakelockEnabled;
  static int get themeNumber => _themeNumber;
  static String get languageCode => _languageCode;
  static bool get connectionCodeCardWatchFlag => _connectionCodeCardWatchFlag;

  static Future<void> ensureReady() async {
    if (_ready) {
      return;
    }
    final prefs = await SharedPreferences.getInstance();
    //
    _connectionCodeMachine = (prefs.getInt(_prefConnectionCodeMachine) ?? 0).clamp(0, 999999);
    _connectionCodeMachineTimestamp = prefs.getInt(_prefConnectionCodeMachineTimestamp) ?? 0;
    _connectionCodeCard = (prefs.getInt(_prefConnectionCodeCard) ?? 0).clamp(0, 999999);
    _ballHistories = (prefs.getString(_prefBallHistories) ?? '')
        .split(',')
        .where((item) => item.isNotEmpty)
        .map<int>((item) => int.parse(item))
        .toList();
    _cardNumbers = (prefs.getString(_prefCardNumbers) ?? '')
        .split(',')
        .where((item) => item.isNotEmpty)
        .map<int>((item) => int.parse(item))
        .toList();
    _machineVolume = (prefs.getDouble(_prefMachineVolume) ?? 1.0).clamp(0.0,1.0);
    _ttsEnabled = prefs.getBool(_prefTtsEnabled) ?? true;
    _ttsVolume = (prefs.getDouble(_prefTtsVolume) ?? 1.0).clamp(0.0, 1.0);
    _ttsVoiceId = prefs.getString(_prefTtsVoiceId) ?? '';
    _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);
    _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> setConnectionCodeMachine(int value) async {
    _connectionCodeMachine = value;
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setInt(_prefConnectionCodeMachine, value);
  }

  static Future<void> setConnectionCodeCard(int value) async {
    _connectionCodeCard = value;
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setInt(_prefConnectionCodeCard, value);
  }

  static Future<void> setMachineVolume(double value) async {
    _machineVolume = value;
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setDouble(_prefMachineVolume, value);
  }

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

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

  static Future<void> setTtsVoiceId(String value) async {
    _ttsVoiceId = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString(_prefTtsVoiceId, 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 prefs = await SharedPreferences.getInstance();
    await prefs.setInt(_prefThemeNumber, value);
  }

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

  //---------------------------------------------

  //ボール履歴をクリア
  static void _ballHistoryClear() async {
    _ballHistories.clear();
    _cardCellAllClose();
    final prefs = await SharedPreferences.getInstance();
    await prefs.remove(_prefBallHistories);
  }
  //ボールを追加
  static Future<bool> ballHistoryAdd(int value) async {
    if (_ballHistories.contains(value)) {  //2重登録防止
      return true;
    }
    _ballHistories.add(value);
    cardCellSetOpenClose();
    final String str = _ballHistories.map<String>((int value) => value.toString()).join(',');
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString(_prefBallHistories,str);
    if (await _connectionCodeMachineSetBallHistory() == false) {
      return false;
    }
    return true;
  }
  //ボールを履歴から削除
  static Future<bool> ballHistoryRemove(int num) async {
    if (_ballHistories.contains(num) == false) {
      return true;
    }
    _ballHistories.remove(num);
    cardCellSetOpenClose();
    final String str = _ballHistories.map<String>((int value) => value.toString()).join(',');
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString(_prefBallHistories,str);
    if (await _connectionCodeMachineSetBallHistory() == false) {   //オフライン想定なのでサーバーに送信されることは無い
      return false;
    }
    return true;
  }
  //ボール履歴に含まれるか
  static bool ballHistoryContain(int num) {
    return _ballHistories.contains(num);
  }
  //ボール履歴の最後か
  static bool ballHistoryIsLast(int num) {
    if (_ballHistories.isEmpty) {
      return false;
    }
    return (_ballHistories[_ballHistories.length - 1] == num) ? true : false;
  }
  //ボール履歴の長さ
  static int ballHistoryLength() {
    return _ballHistories.length;
  }
  //ボール履歴の値
  static int ballHistoryValue(int ptr) {
    if (_ballHistories.isEmpty) {
      return 0;
    }
    if (_ballHistories.length <= ptr) {
      return 0;
    }
    return _ballHistories[ptr];
  }

  //---------------------------------------------

  //新しいカードを用意
  static void cardNew() async {
    List<List<int>> cells = [
      [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],
      [16,17,18,19,20,21,22,23,24,25,26,27,28,29,30],
      [31,32,33,34,35,36,37,38,39,40,41,42,43,44,45],
      [46,47,48,49,50,51,52,53,54,55,56,57,58,59,60],
      [61,62,63,64,65,66,67,68,69,70,71,72,73,74,75],
    ];
    cells[0].shuffle();
    cells[1].shuffle();
    cells[2].shuffle();
    cells[3].shuffle();
    cells[4].shuffle();
    _cardCells[0] = CardCell(cells[0][0], 0);
    _cardCells[1] = CardCell(cells[0][1], 0);
    _cardCells[2] = CardCell(cells[0][2], 0);
    _cardCells[3] = CardCell(cells[0][3], 0);
    _cardCells[4] = CardCell(cells[0][4], 0);
    _cardCells[5] = CardCell(cells[1][0], 0);
    _cardCells[6] = CardCell(cells[1][1], 0);
    _cardCells[7] = CardCell(cells[1][2], 0);
    _cardCells[8] = CardCell(cells[1][3], 0);
    _cardCells[9] = CardCell(cells[1][4], 0);
    _cardCells[10] = CardCell(cells[2][0], 0);
    _cardCells[11] = CardCell(cells[2][1], 0);
    _cardCells[12] = CardCell(0, 1);
    _cardCells[13] = CardCell(cells[2][2], 0);
    _cardCells[14] = CardCell(cells[2][3], 0);
    _cardCells[15] = CardCell(cells[3][0], 0);
    _cardCells[16] = CardCell(cells[3][1], 0);
    _cardCells[17] = CardCell(cells[3][2], 0);
    _cardCells[18] = CardCell(cells[3][3], 0);
    _cardCells[19] = CardCell(cells[3][4], 0);
    _cardCells[20] = CardCell(cells[4][0], 0);
    _cardCells[21] = CardCell(cells[4][1], 0);
    _cardCells[22] = CardCell(cells[4][2], 0);
    _cardCells[23] = CardCell(cells[4][3], 0);
    _cardCells[24] = CardCell(cells[4][4], 0);
    //
    List<int> numbers = [];
    for (int i = 0; i < 25; i++) {
      numbers.add(_cardCells[i].number);
    }
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString(_prefCardNumbers,numbers.join(','));
  }
  //カードが初期状態なら新しいカードを用意
  static void cardIsInitialToNew() async {
    if (_cardCells[0].getNumber() != 1 || _cardCells[1].getNumber() != 2 || _cardCells[2].getNumber() != 3 || _cardCells[3].getNumber() != 4 || _cardCells[4].getNumber() != 5) {
      return;
    }
    if (_cardCells[5].getNumber() != 16 || _cardCells[6].getNumber() != 17 || _cardCells[7].getNumber() != 18 || _cardCells[8].getNumber() != 19 || _cardCells[9].getNumber() != 20) {
      return;
    }
    final String str = _cardNumbers.map<String>((int value) => value.toString()).join(',');
    if (str.isEmpty) {
      cardNew();
    } else {
      List<String> ary = str.split(',');
      for (int i = 0; i < 25; i++) {
        _cardCells[i].setNumber(int.parse(ary[i]));
      }
    }
  }
  //カードのセルを全て閉じる
  static void _cardCellAllClose() {
    for (int i = 0; i < 25; i++) {
      _cardCells[i].setOpen(0);
    }
    _cardCells[12].setOpen(1);
  }
  //カードのセルをボール履歴でopen/closeをセットする
  static void cardCellSetOpenClose() {
    for (int i = 0; i < 25; i++) {
      final int num = _cardCells[i].getNumber();
      if (_ballHistories.contains(num) || num == 0) {
        _cardCells[i].setOpen(1);
      } else {
        _cardCells[i].setOpen(0);
      }
    }
  }
  //カードのセル位置の番号を返す
  static int cardCellPtrGetNumber(int ptr) {
    return _cardCells[ptr].getNumber();
  }
  //カードのセル位置のopen/closeをboolで返す
  static bool cardCellPtrGetOpenBool(int ptr) {
    return (_cardCells[ptr].getOpen() == 1) ? true : false;
  }

  //---------------------------------------------

  //マシン接続コードを文字列で返す
  static String getConnectionCodeMachineStr() {
    if (_connectionCodeMachine == 0) {
      return '------';
    }
    return _connectionCodeMachine.toString();
  }
  //マシン接続コードをリモート側で生成
  static Future<int> connectionCodeMachineCreate() async {
    final Uri url = Uri.https(Secrets.apiUrl,'',{'mode':'create'});
    final Response? response = await _getWithTimeout(url);
    int number = 0;
    if (response != null && response.statusCode == 200) {
      final Map<String,dynamic> json = convert.jsonDecode(response.body) as Map<String,dynamic>;
      number = json['number'];
    }
    _connectionCodeMachine = number;
    await setConnectionCodeMachine(_connectionCodeMachine);
    await _connectionCodeMachineTimestampUpdate();  //マシン接続コードタイムスタンプを更新(新規作成)
    return number;
  }
  //マシン接続コードのボール履歴をクリア
  static Future<bool> connectionCodeMachineClear() async {
    _ballHistoryClear();
    if (_connectionCodeMachine == 0) {
      return true;  //成功
    }
    await _connectionCodeMachineTimestampUpdate();  //マシン接続コードタイムスタンプを更新
    final Uri url = Uri.https(Secrets.apiUrl,'',{'mode':'set','number':_connectionCodeMachine.toString(),'ball_history':''});
    final Response? response = await _getWithTimeout(url);
    if (response != null && response.statusCode == 200) {
      return true;  //成功
    }
    return false; //失敗
  }
  //マシン接続コードのボール履歴をリモートへ送信
  static Future<bool> _connectionCodeMachineSetBallHistory() async {
    if (_connectionCodeMachine == 0) {
      return true;  //成功
    }
    await _connectionCodeMachineTimestampUpdate();  //マシン接続コードタイムスタンプを更新
    final Uri url = Uri.https(Secrets.apiUrl,'',{'mode':'set','number':_connectionCodeMachine.toString(),'ball_history':_ballHistories.join(',')});
    final Response? response = await _getWithTimeout(url);
    if (response != null && response.statusCode == 200) {
      return true;  //成功
    }
    return false; //失敗
  }
  //マシン接続コードのタイムスタンプを保存(更新)
  static Future<void> _connectionCodeMachineTimestampUpdate() async {
    _connectionCodeMachineTimestamp = (DateTime.now().millisecondsSinceEpoch / 1000).floor();
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setInt(_prefConnectionCodeMachineTimestamp, _connectionCodeMachineTimestamp);
  }

  //---------------------------------------------

  //カードがリモートへ確認するフラグをセット
  static void setConnectionCodeCardWatchFlag(bool value) {
    _connectionCodeCardWatchFlag = value;
    _connectionErrorCount = 0;
  }

  //---------------------------------------------

  //カード接続コードでボール履歴を取得
  static Future<int> connectionCodeCardJoin(int inputNumber) async {
    _connectionCodeCard = 0;
    final Uri url = Uri.https(Secrets.apiUrl,'',{'mode':'get','number':inputNumber.toString()});
    final Response? response = await _getWithTimeout(url);
    if (response != null && response.statusCode == 200) {
      final Map<String,dynamic> json = convert.jsonDecode(response.body) as Map<String,dynamic>;
      final int number = json['number'];
      final String history = json['ball_history'];
      if (inputNumber == number) {
        _connectionCodeCard = inputNumber;
        _ballHistories = [];
        history.split(',').forEach((n) {
          final int num = int.tryParse(n) ?? 0;
          if (num != 0) {
            _ballHistories.add(num);
          }
        });
        cardCellSetOpenClose();
      }
    }
    await setConnectionCodeCard(_connectionCodeCard);
    return _connectionCodeCard;   //0は失敗、それ以外は成功
  }
  //カード接続コードでボール履歴をウオッチ
  static Future<bool> connectionCodeCardWatch() async {
    if (_connectionCodeCardWatchFlag == false || _connectionCodeCard == 0) {
      return true;
    }
    final Uri url = Uri.https(Secrets.apiUrl,'',{'mode':'get','number':_connectionCodeCard.toString()});
    final Response? response = await _getWithTimeout(url);
    if (response != null && response.statusCode == 200) {
      final Map<String,dynamic> json = convert.jsonDecode(response.body) as Map<String,dynamic>;
      final int number = json['number'];
      final String history = json['ball_history'];
      if (_connectionCodeCard == number) {
        _ballHistories = [];
        history.split(',').forEach((n) {
          final int num = int.tryParse(n) ?? 0;
          if (num != 0) {
            _ballHistories.add(num);
          }
        });
        cardCellSetOpenClose();
        if (_lastBallHistoryLength != _ballHistories.length) {
          _lastBallHistoryLength = _ballHistories.length;
          _sameLastBallHistoryLengthCount = 0;
        } else {
          _sameLastBallHistoryLengthCount += 1;
        }
        if (_sameLastBallHistoryLengthCount > 360) { //60/5秒*30分 30分間マシンが放置の場合は更新を停止
          _connectionCodeCardWatchFlag = false;
          return false;
        }
        _connectionErrorCount = 0;
        return true;
      }
    }
    _connectionErrorCount += 1;
    if (_connectionErrorCount > 10) { //接続エラーが10回続いた場合は更新を停止
      _connectionCodeCardWatchFlag = false;
      return false;
    }
    return true;
  }

  static Future<Response?> _getWithTimeout(Uri url) async {
    try {
      return await http.get(url).timeout(Duration(seconds: 5));
    } on TimeoutException {
      return null;
    } catch (_) {
      return null;
    }
  }

}

/// 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:bingoonline/model.dart';
import 'package:bingoonline/theme_color.dart';

class ProgressTable {
  late ThemeColor _themeColor;

  Widget table(BuildContext context, ThemeColor themeColor) {
    _themeColor = themeColor;

    // BINGOヘッダー + 数字セル
    final List<Widget> cells = [
      ...['B', 'I', 'N', 'G', 'O'].map((str) => cellProgressBingo(str)),
      ...[
        1, 16, 31, 46, 61,
        2, 17, 32, 47, 62,
        3, 18, 33, 48, 63,
        4, 19, 34, 49, 64,
        5, 20, 35, 50, 65,
        6, 21, 36, 51, 66,
        7, 22, 37, 52, 67,
        8, 23, 38, 53, 68,
        9, 24, 39, 54, 69,
        10, 25, 40, 55, 70,
        11, 26, 41, 56, 71,
        12, 27, 42, 57, 72,
        13, 28, 43, 58, 73,
        14, 29, 44, 59, 74,
        15, 30, 45, 60, 75,
      ].map((number) => cellProgress(number)),
    ];

    return GridView(
      shrinkWrap: true,
      physics: NeverScrollableScrollPhysics(),
      gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
        maxCrossAxisExtent: MediaQuery.of(context).size.width / 5,
        mainAxisSpacing: 4,
        crossAxisSpacing: 4,
        childAspectRatio: 2,
      ),
      children: cells,
    );
  }

  Widget cellProgressBingo(String str) {
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 8),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(36),
      ),
      child: Center(
        child: Text(str, style: const TextStyle(color: Colors.white)),
      ),
    );
  }

  Widget cellProgress(int num) {
    Color bgColor = _themeColor.progressCellColor0;
    if (Model.ballHistoryContain(num)) {
      bgColor = _themeColor.progressCellColor1;
      if (Model.ballHistoryIsLast(num)) {
        bgColor = _themeColor.progressCellColor2;
      }
    }
    return Container(
      decoration: BoxDecoration(
        color: bgColor,
        borderRadius: BorderRadius.circular(36),
      ),
      child: Center(
        child: Text(
          num.toString(),
          style: const TextStyle(fontSize: 16),
        ),
      ),
    );
  }
}
/// Copyright© ao-system, Inc.

import 'package:flutter/material.dart';

import 'package:bingoonline/theme_color.dart';
import 'package:bingoonline/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:bingoonline/setting_card.dart";
import 'package:bingoonline/l10n/app_localizations.dart';
import 'package:bingoonline/text_to_speech.dart';
import 'package:bingoonline/ad_banner_widget.dart';
import 'package:bingoonline/ad_ump_status.dart';
import 'package:bingoonline/theme_color.dart';
import 'package:bingoonline/loading_screen.dart';
import 'package:bingoonline/model.dart';
import 'package:bingoonline/main.dart';
import 'package:bingoonline/_secrets.dart';
import 'package:bingoonline/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;
  bool _wakelockEnabled = false;
  int _themeNumber = 0;
  String _languageCode = '';
  final _inAppReview = InAppReview.instance;
  double _machineVolume = 1.0;
  bool _ttsEnabled = true;
  String _ttsVoiceId = '';
  double _ttsVolume = 1.0;
  bool _isReady = false;

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

  void _initState() async {
    //ump
    _adUmpService = AdUmpService();
    await _refreshConsentInfo();
    //model
    _machineVolume = Model.machineVolume;
    _ttsEnabled = Model.ttsEnabled;
    _ttsVoiceId = Model.ttsVoiceId;
    _ttsVolume = Model.ttsVolume;
    _wakelockEnabled = Model.wakelockEnabled;
    _themeNumber = Model.themeNumber;
    _languageCode = Model.languageCode;
    //speech
    await TextToSpeech.applyPreferences(Model.ttsVoiceId,Model.ttsVolume);
    setState(() {
      _isReady = true;
    });
  }

  @override
  void 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; });
    }
  }

  void _onApply() async {
    FocusScope.of(context).unfocus();
    await Model.setMachineVolume(_machineVolume);
    await Model.setTtsEnabled(_ttsEnabled);
    await Model.setTtsVolume(_ttsVolume);
    await Model.setTtsVoiceId(_ttsVoiceId);
    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),
        actions: [
          IconButton(
            icon: const Icon(Icons.check),
            onPressed: _onApply
          ) ,
        ],
      ),
      body: SafeArea(
        child: Column(children:[
          Expanded(
            child: GestureDetector(
              onTap: () => FocusScope.of(context).unfocus(),
              child: SingleChildScrollView(
                child: Padding(
                  padding: const EdgeInsets.only(left: 8, right: 8, top: 8, bottom: 100),
                  child: Column(
                    children: [
                      _buildVolume(l, t),
                      _buildSpeech(l, t),
                      _buildWakelockEnabled(l, t),
                      _buildTheme(l, t),
                      _buildLanguage(l, t),
                      _buildReview(l, t),
                      _buildCmp(l, t),
                      _buildAtt(l, t),
                      _buildUsage(l, t),
                    ],
                  ),
                ),
              ),
            ),
          ),
        ])
      ),
      bottomNavigationBar: AdBannerWidget(adManager: MainApp.of(context).adManager),
    );
  }

  Widget _buildVolume(AppLocalizations l, TextTheme t) {
    return SettingCard(
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
        title: Text(l.machineVolume, style: t.bodyMedium),
        subtitle: Row(
          children: [
            Text(
              _machineVolume.toStringAsFixed(1),
              textAlign: TextAlign.right,
            ),
            Expanded(
              child: Slider(
                value: _machineVolume,
                min: 0.0,
                max: 1.0,
                divisions: 10,
                label: _machineVolume.toStringAsFixed(1),
                onChanged: (value) {
                  setState(() {
                    _machineVolume = 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)),
                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),
        minVerticalPadding: 0,
        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),
        minVerticalPadding: 0,
        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),
                      ),
                    ],
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildUsage(AppLocalizations l, TextTheme t) {
    return SettingCard(
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        title: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(l.usage1, style: t.bodySmall),
            const SizedBox(height: 16),
            Text(l.usage2, style: t.bodySmall),
            const SizedBox(height: 16),
            Text(l.usage3, style: t.bodySmall),
            const SizedBox(height: 16),
            Text(l.usage4, 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;
  bool get isLight => _isLight;

  //machine
  Color get machineBackColor => const Color.fromRGBO(0,233,131,1);
  Color get machineForeColor => _isLight ? Colors.green[500]! : Colors.greenAccent[700]!;
  Color get machineMovieBackColor => _isLight ? Color.fromRGBO(40,245,146,1) : Colors.green[900]!;
  //card
  Color get cardInitColor => _isLight ? Colors.orange[500]! : Colors.orangeAccent[700]!;
  Color get cardBackColor => _isLight ? Color.fromRGBO(197,172,119,1) : Color.fromRGBO(67, 52, 29, 1.0);
  Color get cardFrameColor => _isLight ? Colors.amber[500]! : Colors.lime[900]!;
  Color get cardCellColor => _isLight ? Colors.white : Colors.grey[800]!;
  //progress
  Color get progressMachineFrameColor => _isLight ? Color.fromRGBO(40,245,146, 1.0) : Colors.green[800]!;
  Color get progressCardFrameColor => _isLight ? Colors.brown[300]! : Colors.grey[900]!;
  Color get progressCellColor0 => _isLight ? Colors.white : Colors.grey[800]!;
  Color get progressCellColor1 => _isLight ? Colors.yellow : Colors.lime[900]!;
  Color get progressCellColor2 => _isLight ? Colors.orange : Colors.orange[900]!;
  //setting page
  Color get backColor => _isLight ? Colors.grey[300]! : 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 borderColor => _isLight ? Colors.grey[300]! : Colors.grey[700]!;
  Color get inputFillColor => _isLight ? Colors.grey[50]! : Colors.grey[900]!;
}
/// 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;
    }
  }
}