import 'dart:async';

import 'package:auto_route/auto_route.dart';
import 'package:eitc_erm_dental_flutter/app_router.gr.dart';
import 'package:eitc_erm_dental_flutter/dialog/app_start_agreement_dialog.dart';
import 'package:eitc_erm_dental_flutter/funcs.dart';
import 'package:eitc_erm_dental_flutter/generated/assets.dart';
import 'package:eitc_erm_dental_flutter/sp_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../global.dart';

///Splash页面
@RoutePage(name: "splashRoute")
class SplashPage extends StatefulWidget {
  const SplashPage({super.key});

  @override
  State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  @override
  void initState() {
    super.initState();
    //定时打开主页
    Timer(const Duration(seconds: 3), () => _gotoNext(context, true));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        alignment: Alignment.center,
        decoration: const BoxDecoration(
          image: DecorationImage(
              image: AssetImage(Assets.imagesSplashBg), fit: BoxFit.cover),
        ),
        child: Column(
          children: [
            Expanded(
              flex: 2,
              child: Column(
                mainAxisSize: MainAxisSize.min,
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  Text(
                    splashTitle,
                    textAlign: TextAlign.center,
                    style: TextStyle(color: Colors.white, fontSize: 30.sp),
                  ),
                  SizedBox(
                    height: 10.h,
                  ),
                  Text(
                    getS().appSlogan,
                    textAlign: TextAlign.center,
                    style: TextStyle(color: Colors.white, fontSize: 20.sp),
                  ),
                  SizedBox(
                    height: 25.h,
                  ),
                  Text(
                    getS().appSlogan2,
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 14.sp,
                    ),
                  ),
                ],
              ),
            ),
            const Spacer(
              flex: 3,
            )
          ],
        ),
      ),
    );
  }

  String get splashTitle => switch (appFlavor) {
        flavorHst => getS().splashTitleHst,
        _ => getS().splashTitle
      };

  ///下一步
  ///
  /// [checkAgreement]  是否检查app启动协议是否同意
  void _gotoNext(BuildContext context, bool checkAgreement) async {
    if (checkAgreement) {
      bool agreed = await SpUtil.isAppStartAgreementAgreed();
      logd("splash检查app启动协议,是否同意=$agreed");
      if (!agreed && context.mounted) {
        _showAgreement(context);
        return;
      }
    }
    if (context.mounted) {
      context.replaceRoute(const MainRoute());
      logd("splash跳转到主页");
    }
  }

  ///显示协议
  void _showAgreement(BuildContext context) async {
    bool? bo = await showDialog(
        context: context,
        barrierDismissible: false,
        builder: (ctx) {
          return AppStartAgreementDialog();
        });
    //同意了继续
    if (bo ?? false) {
      await SpUtil.setAppStartAgreementAgreed(true);
      if (context.mounted) {
        _gotoNext(context, false);
      }
    }
    //不同意就退出app
    else {
      exitApp();
    }
  }
}
