import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_habit/common/BaseArchitectural.dart';
import 'package:flutter_habit/common/I18N.dart';
import 'package:flutter_habit/common/LocalData.dart';
import 'package:flutter_habit/common/SqfliteDataBase.dart';
import 'package:flutter_habit/provider/ConfigProvider.dart';
import 'package:flutter_habit/provider/DataProvider.dart';
import 'package:flutter_habit/provider/NotificationProvider.dart';
import 'package:flutter_habit/provider/UserProvider.dart';
import 'package:flutter_habit/provider/ThemeProvider.dart';
import 'package:flutter_habit/pages/home_page.dart';
import 'package:provider/provider.dart';

/// Description: Spalsh Page
/// Time       : 08/17/2023 Thursday
/// Author     : liuyuqi.gov@msn.cn
class LoadingPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider<SplashPageProvider>(
            create: (_) => SplashPageProvider(context)),
      ],
      child: _LoadingPageView(),
    );
  }
}

// service
class SplashPageProvider extends BaseProvider {
  SplashPageProvider(BuildContext context) : super(context);

  Timer timer;
  bool isLoaded;

  Future<void> loadData(BuildContext context) async {
    // ================================== init ==================================
    await LocalData.init();

//    LocalData.getInstance().clear();

    I18N.init();
    Provider.of<ThemeProvider>(context, listen: false).init();
    Provider.of<UserProvider>(context, listen: false).init();
    Provider.of<ConfigProvider>(context, listen: false).init();
    await Provider.of<NotificationProvider>(context, listen: false).init();
    await SqfliteDataBase.init();
//    await SqfliteDataBase.resetTables();
    await Provider.of<DataProvider>(context, listen: false).init();
    isLoaded = true;
    // ================================== over ==================================
  }

  @override
  void init(BuildContext context) {
    print("---------init-------------spalsh-------------provider");
    super.init(context);
    isLoaded = false;
    timer = null;
    startCountDown(context);
    loadData(context);
  }

  void startCountDown(BuildContext context) {
    Future.delayed(Duration(seconds: 3), () {
      scalaJumpToHomePage(context);
    });
    // if (timer == null) {
    //   int minDuration = 3;
    //   timer = Timer.periodic(Duration(seconds: 5), (t) {
    //     minDuration--;
    //     if (isLoaded && minDuration <= 0) {
    //       timer.cancel();
          // scalaJumpToHomePage(context);
    //     }
    //   });
    // }
  }

  /// 跳转到Home Page
  void scalaJumpToHomePage(BuildContext context) {
    print("---------go mail page---------");
    Navigator.of(context).pushReplacement(PageRouteBuilder(
        transitionDuration: Duration(milliseconds: 600),
        pageBuilder: (context, animation, secondaryAnimation) =>
//          FadeTransition(opacity: animation, child: HomePage())));
            ScaleTransition(scale: animation, child: HomePage())));
  }

  @override
  void dispose() {
    super.dispose();
    if (timer != null) {
      timer.cancel();
    }
  }
}

class _LoadingPageView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // SplashPageProvider service =
    Provider.of<SplashPageProvider>(context, listen: false);

    return ConstrainedBox(
      constraints: BoxConstraints.expand(),
      child: Image.asset(
        "res/welcome.png",
        fit: BoxFit.fitWidth,
      ),
    );
  }
}
