import 'dart:async';

import 'package:fis_common/env/env.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/architecture/utils/window__change_helpler.dart';
import 'package:flyinsono/global.dart';
import 'package:flyinsono/lab/color/lab_dark_colors.dart';
import 'package:flyinsono/lab/env/env.dart';
import 'package:flyinsono/pages/others/title_bar_buttons.dart';

import 'index.dart';

///过度页面
class SplashPage extends FisView<SplashController> {
  // 内容页
  FWidget _buildView(BuildContext context) {
    controller.state.second; // call for load progress
    return context.fResponsive(
      mobile: (_) => _MobileLayout(),
      desktop: (_) => _DesktopLayout(),
    );
  }

  @override
  FWidget build(BuildContext context) {
    return FFutureBuilder(
      future: _precacheImgAsset(context),
      builder: (_, data) {
        return FScaffold(
          backgroundColor: kIsLab
              ? LabColorConfig.base100
              : const Color.fromARGB(255, 61, 146, 251),
          body: _buildView(context),
        );
      },
    );
  }

  ///图像资源文件加载
  Future<void> _precacheImgAsset(BuildContext context) async {
    var arr = <String>[];
    final isZH = _checkIsChinese();
    final isRU = _checkIsRU();
    if (kIsMobile) {
      _MobileLayout.bottomRippleImgProvider =
          AssetImage("assets/images/splash/bottom_ripple.png");
      if (isZH) {
        _MobileLayout.sloganImgProvider =
            AssetImage("assets/images/splash/slogan_zh.png");
        _MobileLayout.logoImgProvider =
            AssetImage("assets/images/splash/logo_zh.png");
      } else if (isRU) {
        _MobileLayout.sloganImgProvider =
            AssetImage("assets/images/splash/slogan_ru.png");
        _MobileLayout.logoImgProvider =
            AssetImage("assets/images/splash/logo_ru.png");
      } else {
        _MobileLayout.sloganImgProvider =
            AssetImage("assets/images/splash/slogan_en.png");
        _MobileLayout.logoImgProvider =
            AssetImage("assets/images/splash/logo_en.png");
      }
      await Future.wait([
        precacheImage(_MobileLayout.bottomRippleImgProvider!, context),
        precacheImage(_MobileLayout.sloganImgProvider!, context),
        precacheImage(_MobileLayout.logoImgProvider!, context),
      ]);
    } else {
      arr.add(_getDesktopBg());
      await _preImgsBatch(context, arr);
    }
  }

  ///启动页面区分图像加载
  Future<void> _preImgsBatch(BuildContext context, List<String> keys) async {
    final futures = keys.map((e) => precacheImage(AssetImage(e), context));
    await Future.wait(futures);
  }

  static void dispose() {
    if (_MobileLayout.bottomRippleImgProvider != null) {
      _MobileLayout.bottomRippleImgProvider!.evict().then((value) {
        _MobileLayout.bottomRippleImgProvider = null;
      });
    }
    if (_MobileLayout.sloganImgProvider != null) {
      _MobileLayout.sloganImgProvider!.evict().then((value) {
        _MobileLayout.sloganImgProvider = null;
      });
    }
    if (_MobileLayout.logoImgProvider != null) {
      _MobileLayout.logoImgProvider!.evict().then((value) {
        _MobileLayout.logoImgProvider = null;
      });
    }
  }
}

class _DesktopLayout extends FisView<SplashController> {
  @override
  FWidget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return FStack(
      children: [
        if (Global.isRemoveLogo)
          FContainer(
              height: size.height,
              width: size.width,
              color: Color.fromARGB(255, 127, 176, 255))
        else
          FContainer(
            height: size.height,
            width: size.width,
            child: FImage.asset(
              _getDesktopBg(),
              fit: BoxFit.cover,
            ),
          ),
        FObx(() {
          controller.state.second;
          if (FPlatform.isWindows) {
            return FPositioned(
              top: 10,
              right: 10,
              child: TitleBarButtons(
                WindowChangeHelper.onMinimize,
                WindowChangeHelper.onClose,
                null,
                Colors.white,
                Colors.transparent,
              ),
            );
          } else {
            return FSizedBox();
          }
        }),
        FPositioned(
          child: FContainer(
            width: size.width,
            child: FCenter(
              child: FCircularProgressIndicator(),
            ),
          ),
          bottom: .44.sh,
        ),
      ],
    );
  }
}

class _MobileLayout extends FStatelessWidget {
  static ImageProvider? bottomRippleImgProvider;
  static ImageProvider? sloganImgProvider;
  static ImageProvider? logoImgProvider;

  @override
  FWidget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return FStack(
      children: [
        FPositioned(
          bottom: 0,
          width: size.width,
          height: 375.ors,
          child: FImage(
            image: bottomRippleImgProvider!,
            fit: BoxFit.fill,
          ),
        ),
        FPositioned(
          bottom: 180.ors,
          width: size.width,
          child: FCenter(
            child: FImage(
              image: sloganImgProvider!,
              width: 220.ors,
            ),
          ),
        ),
        FPositioned(
          top: 200.ors,
          width: size.width,
          child: FCenter(
            child: FImage(
              image: logoImgProvider!,
              width: 180.ors,
            ),
          ),
        ),
        FPositioned(
          child: FContainer(
            width: size.width,
            child: FCenter(
              child: FCircularProgressIndicator(
                strokeWidth: 2.0.sp,
                backgroundColor: Colors.white,
              ),
            ),
          ),
          bottom: 20.sp,
        ),
      ],
    );
  }
}

///当前系统语言是否中文
bool _checkIsChinese() {
  print("_checkIsChinese");
  print(Store.app.systemLocale.languageCode.toLowerCase());
  return Store.app.systemLocale.languageCode.toLowerCase() == 'zh';
}

///当前系统语言是否是俄文
bool _checkIsRU() {
  print("_checkIsRU");
  print(Store.app.systemLocale.languageCode.toLowerCase());
  return Store.app.systemLocale.languageCode.toLowerCase() == 'ru';
}

String _getDesktopBg() {
  if (kIsLab) {
    return "assets/images/splash/splash_lab_zh.png";
  }
  var isZH = _checkIsChinese();
  var isRU = _checkIsRU();
  if (isZH) {
    return "assets/images/splash/desktop_zh.jpg";
  }
  if (isRU) {
    return "assets/images/splash/desktop_ru.jpg";
  }
  return "assets/images/splash/desktop_en.jpg";
}
