import 'package:fis_common/index.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/widgets/webview/webview.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/managers/interfaces/webview_controller.dart';
import 'package:flyinsono/pages/main/home/widgets/dialog_page.dart';
import 'package:get/get.dart';
import 'package:webviewx/webviewx.dart';
import 'package:fis_ui/base_define/page.dart';
import 'index.dart';

class ThirdpartyPage extends FisView<FeedbackController> implements FPage {
  final webViewManager = Get.find<IWebviewControllerManager>();
  final webviewInteractiveController =
      Get.find<FWebviewInteractiveController>();

  @override
  String get pageName => "ThirdpartyPage";

  ThirdpartyPage({Key? key}) : super(key: key);
  @override
  FWidget build(BuildContext context) {
    return _buildView();
  }

  // 主视图
  FWidget _buildView() {
    var color =
        FTheme.ins.colorScheme.primary.value.toRadixString(16).substring(2);
    final args = router.getParams(NavIds.HOME) ?? {};
    var type = "PC";
    if (kIsMobile) {
      type = "Mobile";
    }
    var url = '';
    if (args.containsKey('url')) {
      url = args['url'] ?? '';
    }
    var urlRequest = StringFormatter.formatTargetCode(
      url,
      [i18nBook.locale.toCodeString('-'), color, type, Store.user.code],
      ['language', 'color', 'type', 'userId'],
    );
    var title = '';
    if (args.containsKey('name')) {
      title = args['name']!;
    }
    var index = "0";
    if (args.containsKey('index')) {
      index = args['index']!;
    }
    print(urlRequest);
    if (kIsMobile) {
      var body = FSafeArea(
        child: QuickFWidget(
          InAppWebView(
            initialUrlRequest: URLRequest(
              url: Uri.parse(urlRequest),
            ),
          ),
        ),
      );
      if (index == '0') {
        return body;
      }
      return FScaffold(
        appBar: FAppBar.simple(
          title: title,
          onBackClicked: () {
            router.back(id: NavIds.HOME);
          },
        ),
        body: body,
      );
    } else {
      return FWebViewX(
        height: MediaQuery.of(Get.context!).size.height,
        width: MediaQuery.of(Get.context!).size.width,
        initialContent: urlRequest,
        javascriptMode: JavascriptMode.unrestricted,
        ignoreAllGestures: kIsMobile ? false : webViewManager.isDialogExist,
        onWebViewCreated: kIsMobile
            ? null
            : (c) {
                webViewManager.initWebviewController(c);
              },
      );
    }
  }
}
