import 'package:fis_common/env/env.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/widgets/webview/webview.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/managers/interfaces/webview_controller.dart';
import 'package:get/get.dart';
import 'package:webviewx/webviewx.dart';

// import 'package:webview_flutter/webview_flutter.dart';

import 'index.dart';

///条款内容页数据源
final _docMap = {
  ClauseDocType.userAgreement: {
    "title": i18nBook.auth.userAgreement,
    "zh": "assets/docs/UsageProtocol_ZH.html",
    "en": "assets/docs/UsageProtocol_EN.html",
    "ru": "assets/docs/UsageProtocol_RU.html",
    "es": "assets/docs/UsageProtocol_ES.html"
  },
  ClauseDocType.privacyPolicy: {
    "title": i18nBook.auth.privacyPolicy.t,
    "zh": "assets/docs/PrivacyPolicy_ZH.html",
    "en": "assets/docs/PrivacyPolicy_EN.html",
    "ru": "assets/docs/PrivacyPolicy_RU.html",
    "es": "assets/docs/PrivacyPolicy_ES.html",
  },
  ClauseDocType.accountCancellation: {
    "title": i18nBook.user.fisAccountCancellation.t,
    "zh": "assets/docs/AccountCancellation_ZH.html",
    "en": "assets/docs/AccountCancellation_EN.html",
    "ru": "assets/docs/AccountCancellation_RU.html",
    "es": "assets/docs/AccountCancellation_ES.html",
  },
};

///条款页面
class ClauseViewPage extends FisView<ClauseViewController> implements FPage {
  static const Duration C_TRANSITION_DURATION = Duration(milliseconds: 200);

  @override
  final String pageName;

  ClauseViewPage({this.pageName = 'ClauseViewPage', Key? key})
      : super(key: key);
  final webviewControllerManager = Get.find<IWebviewControllerManager>();

  @override
  FWidget build(BuildContext context) {
    final docType = Get.arguments as ClauseDocType;
    final docInfo = _docMap[docType]!;
    final title = (docInfo['title']!).toString();

    final docKey = i18nBook.locale.toCodeString('-').split("-")[0];

    final assetKey = docInfo[docKey]! as String;

    return _buildWebView(
      context,
      assetKey,
    );
  }

  FWidget _buildWebView(BuildContext context, String assetKey) {
    return FFutureBuilder<String>(
      future: _loadLocalHtml(context, assetKey),
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return FCenter(child: FCircularProgressIndicator());
          // return FCenter(child: FText("${i18nBook.common.loading.t}..."));
        } else {
          return FWebViewX(
            height: MediaQuery.of(Get.context!).size.height,
            width: MediaQuery.of(Get.context!).size.width,
            initialSourceType: SourceType.html,
            initialContent: snapshot.data!,
            javascriptMode: JavascriptMode.unrestricted,
            onWebResourceError: (p0) {},
            onWebViewCreated: (controller) {
              webviewControllerManager.initWebviewController(controller);
            },
          );
        }
      },
    );
  }

  Future<String> _loadLocalHtml(BuildContext context, String assetKey) async {
    await Future.delayed(const Duration(milliseconds: 200));
    final start = DateTime.now();
    final content = await DefaultAssetBundle.of(context).loadString(assetKey);
    final end = DateTime.now();

    if (FPlatform.isIOS) {
      final spend = end.difference(start).inMilliseconds;
      int left = C_TRANSITION_DURATION.inMilliseconds - spend;
      print(left);
      if (left < 50) {
        left += 50;
      }
      // ios端偶现进场动画内加载WebView导致UI卡顿， 顾凑满动画时长再加载WebView
      await Future.delayed(Duration(milliseconds: left));
    }
    return content;
  }
}
