import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/architecture/values/features.dart';
import 'package:flyinsono/architecture/utils/dongle_data.dart';
import 'package:flyinsono/lab/pages/lab_setting/widgets/about_page.dart';
import 'package:flyinsono/lab/pages/lab_setting/widgets/side_router_button.dart';
import 'package:flyinsono/lab/style/lab_box_decoration.dart';
import 'package:flyinsono/managers/interfaces/server_config.dart';
import 'package:flyinsono/managers/interfaces/webview_controller.dart';
import 'package:get/get.dart';
import 'package:webviewx/webviewx.dart';
import 'package:fis_i18n/i18n.dart';

class LabSetting extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return LabSettingState();
  }
}

class LabSettingState extends State<LabSetting> {
  final manager = Get.find<IWebviewControllerManager>();
  int _index = 0;
  String _dongleId = "";
  String _userManagerUrl = '';
  bool showUserManager = true;

  /// 是否存在用户管理权限 TODO
  bool get hasUserManagerFeature =>
      Store.user.hasFeature(FeatureKeys.SystemManagementAuthority);

  @override
  void initState() {
    _dongleId = DongleData.labDongle;
    print("dongleId:$_dongleId");
    super.initState();
    showUserManager = hasUserManagerFeature;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            width: 300,
            child: _buildLeftView(),
          ),
          Expanded(child: _buildContent())
        ],
      ),
    );
  }

  Widget _buildLeftView() {
    return Container(
      margin: EdgeInsets.all(10),
      padding: EdgeInsets.all(5),
      decoration: LabBoxDecoration.base,
      child: Column(
        children: [
          SideRouterButton(
            isSelected: _index == 0,
            //TODO(Loki):翻译
            projectName: "关于",
            onClick: () {
              setState(() {
                _index = 0;
              });
            },
          ),
          if (kDebugMode)
            SideRouterButton(
              isSelected: _index == 1,
              //TODO(Loki):翻译
              projectName: "用户管理 (Dev Only)",
              onClick: () async {
                _userManagerUrl = await Get.find<IServerConfigManager>()
                        .getUserManagerUrl() ??
                    '';
                var lang = i18nBook.locale.toCodeString('-');
                _userManagerUrl = _userManagerUrl
                    .replaceAll('{0}', "zh-CN")
                    .replaceAll('{1}', _dongleId);
                print("userManagerUrl:$_userManagerUrl");
                setState(() {
                  _index = 1;
                });
              },
            ),
          // SideRouterButton(
          //   isSelected: _index == 2,
          //   //TODO(Loki):翻译
          //   projectName: "系统设置",
          //   onClick: () {
          //     setState(() {
          //       _index = 2;
          //     });
          //   },
          // ),
        ],
      ),
    );
  }

  Widget _buildContent() {
    if (_index == 0) {
      return AboutPart();
    }
    if (_index == 1) {
      return WebViewX(
        height: MediaQuery.of(Get.context!).size.height,
        width: MediaQuery.of(Get.context!).size.width,
        initialContent: _userManagerUrl,
        javascriptMode: JavascriptMode.unrestricted,
        ignoreAllGestures: manager.isDialogExist,
        onWebViewCreated: (c) async {
          manager.initWebviewController(c);
        },
      );
    }
    return SizedBox();
  }
}
