import 'package:fis_i18n/i18n.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_ui/base_define/page.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/router/index.dart';
import 'package:flyinsono/pages/organization/org_info/widget/orgInfo_dialog.dart';
import 'package:flyinsono/pages/organization/org_info/widget/org_base_info.dart';
import 'package:flyinsono/pages/organization/org_info/widget/refer_org.dart';
import 'package:get/get.dart';

import 'index.dart';

class OrgInfoPage extends FisView<OrgInfoController> implements FPage {
  const OrgInfoPage({this.pageName = 'OrgInfoPage', Key? key})
      : super(key: key);
  @override
  final String pageName;

  @override
  FWidget build(BuildContext context) {
    final page = context.fResponsiveVoid(
      mobile: () => _MobileLayout(businessParent: this),
      desktop: () => _DesktopLayout(businessParent: this),
    );
    return page;
  }
}

// ignore: must_be_immutable
class _DesktopLayout extends FisView<OrgInfoController> implements FWidget {
  late List<OrgModel> selectReferOrgList =
      controller.state.selectReferOrgList.toSet().toList();
  final selectFocusNode = FocusNode();

  ///父级节点
  final FPage businessParent;
  _DesktopLayout({required this.businessParent, Key? key}) : super(key: key);

  @override
  FWidget build(BuildContext context) {
    final child = FContainer(
      color: Colors.white,
      padding: const EdgeInsets.symmetric(vertical: 60),
      child: FFlex(
        direction: Axis.horizontal,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          _buildOrginfos(context),
        ],
      ),
    );

    return OrgInfoDialog(
      orgInfoDialogChild: child,
    );
  }

  /// 构建输入名称
  ///
  /// [title] 输入名称
  ///
  /// [bottom] 底部距离
  FWidget _buildTitle({required String title, double? bottom = 6}) {
    return FContainer(
      /// TODO:[Gavin] 多语言-UI-整合
      width: i18nBook.isCurrentChinese ? 100 : 200,
      margin: EdgeInsets.only(
        bottom: bottom!,
      ),
      child: FText(
        title,
        style: TextStyle(
          fontSize: 16,
        ),
      ),
    );
  }

  /// 构建输入主体
  ///
  /// [widget] 主体组件
  ///
  /// [height] 具体多高
  FWidget _buildContext({required FWidget widget, required double height}) {
    return FContainer(
      height: height,

      /// TODO:[Gavin] 多语言-UI-整合
      width: i18nBook.isCurrentChinese ? 600 : 700,
      child: FCenter(
        child: widget,
      ),
    );
  }

  /// 可转诊机构
  FWidget _buildReferOrg() {
    return FRow(
      children: [
        FBorderInput(
          hintSize: 16,
          contentSize: 16,
          maxLength: 20,
          borderColor: Color.fromRGBO(234, 234, 234, 1),
          height: 38,
          mouseCursor: SystemMouseCursors.click,
          hintText: i18nBook.organization.orgNameInputTip.t,
          readOnly: true,
          // onChanged: (value) => controller.state.organizationName = value,
          onTap: () {
            Get.dialog(
              ReferOrgDialog(
                selectReferOrgList: controller.state.selectReferOrgList,
              ),
            );
          },
        )
      ],
    );
  }

  /// 转诊超时限额
  FWidget _buildTimeoutLimit() {
    return FObx(
      () {
        return FCounter(
          businessParent: businessParent,
          name: '',
          step: 0.5,
          initValue: controller.state.timeoutLimit,
          onChange: (val) {
            controller.state.timeoutLimit = val;
          },
          textController: controller.timeoutLimit,
        );
      },
    );
  }

  ///机构信息
  FWidget _buildOrginfos(context) {
    List<FWidget> children = [];
    // children.addAll(BaseOrgInfos());
    children.addAll(_buildOrgSetting());

    return FFlex(
      direction: Axis.vertical,
      children: [
        BaseOrgInfos(),
        _buildSetting(),
      ],
    );
  }

  FWidget _buildTabs() {
    return FObx(() {
      return FContainer(
        margin: EdgeInsets.only(
          /// TODO:[Gavin] 多语言-UI-整合
          left: (i18nBook.isCurrentChinese ? 100 : 200) + 15,
        ),

        /// TODO:[Gavin] 多语言-UI-整合
        width: (i18nBook.isCurrentChinese ? 600 : 700) - 15,
        child: FWrap(
          direction: Axis.horizontal,
          alignment: WrapAlignment.start,
          children: controller.state.selectReferOrgList
              .map(
                (e) => FContainer(
                  width: 180,
                  decoration: BoxDecoration(
                    border: Border.all(
                      color: Color.fromRGBO(
                        235,
                        235,
                        235,
                        1,
                      ),
                    ),
                    borderRadius: BorderRadius.all(
                      Radius.circular(4),
                    ),
                  ),
                  padding: EdgeInsets.symmetric(
                    horizontal: 10,
                  ),
                  margin: EdgeInsets.only(
                    right: 10,
                    bottom: 10,
                  ),
                  child: FRow(
                    mainAxisSize: MainAxisSize.max,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      FContainer(
                        width: 113,
                        child: FMaterialTooltip(
                          message: e.name,
                          child: FText(
                            e.name,
                            overflow: TextOverflow.ellipsis,
                          ),
                        ),
                      ),
                      FSizedBox(
                        width: 5,
                      ),
                      FIconButton(
                        businessParent: businessParent,
                        icon: FIcon(Icons.delete_outlined),
                        name: "remove",
                        iconSize: 20,
                        onPressed: () {
                          selectReferOrgList.remove(e);
                          controller.state.selectReferOrgList =
                              selectReferOrgList.toSet().toList();
                        },
                      ),
                    ],
                  ),
                ),
              )
              .toList(),
        ),
      );
    });
  }

  /// 合成组件
  FWidget _buildFormItem({required FWidget title, required FWidget widge}) {
    return FFlex(
      direction: Axis.horizontal,
      children: [
        title,
        widge,
      ],
    );
  }

  FWidget _buildSetting() {
    return FColumn(
      children: _buildOrgSetting() + [_buildTabs()],
    );
  }

  ///机构设置
  List<FWidget> _buildOrgSetting() {
    List<FWidget> settings = [
      const FSizedBox(height: 20),
      FContainer(
        /// TODO:[Gavin] 多语言-UI-整合
        width: i18nBook.isCurrentChinese ? 700 : 900,
        child: FDivider(
          color: Colors.grey,
          thickness: 1,
        ),
      ),
      const FSizedBox(height: 20),
      FContainer(
        width: i18nBook.isCurrentChinese ? 700 : 900,
        alignment: Alignment.centerLeft,
        child: FText(
          i18nBook.organization.orgSetting.t,
          style: TextStyle(
            fontSize: 20,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
      const FSizedBox(height: 20),
      _buildFormItem(
        title: _buildTitle(
          title: i18nBook.organization.privacyProtection.t,
          bottom: 0.0,
        ),
        widge: FContainer(
          height: 60.0,
          width: i18nBook.isCurrentChinese ? 600 : 700,
          alignment: Alignment.centerLeft,
          child: FObx(
            () => FSwitch(
              value: controller.state.isSafeMode,
              inactiveThumbColor: FTheme.ins.colorScheme.primary,
              inactiveTrackColor: Colors.grey.withAlpha(88),
              activeColor: Colors.white,
              activeTrackColor: FTheme.ins.colorScheme.primary,
              onChanged: (v) {
                controller.state.isSafeMode = v;
              },
            ),
          ),
        ),
      ),
      _buildFormItem(
        title: _buildTitle(
          title: i18nBook.common.canReferOrg.t,
          bottom: 0.0,
        ),
        widge: _buildContext(
          widget: FRow(
            children: [
              FSizedBox(
                width: 15,
              ),
              FContainer(
                width: 400.w,
                child: _buildReferOrg(),
              ),
            ],
          ),
          height: 60.0,
        ),
      ),
    ];

    return settings;
  }
}

class _MobileLayout extends FisView<OrgInfoController> implements FWidget {
  ///父级节点
  final FPage businessParent;

  _MobileLayout({required this.businessParent, Key? key}) : super(key: key);

  @override
  FWidget build(BuildContext context) {
    return FSafeArea(
      top: false,
      child: FScaffold(
        appBar: FAppBar.simple(
          title: i18nBook.homePage.organizationInfo.t,
          onBackClicked: () => Get.back(),
        ),
        body: FContainer(
          child: FColumn(
            children: [
              FExpanded(
                child: FContainer(
                  child: FObx(
                    () {
                      return FColumn(
                        children: [
                          SelectRow(
                            title: i18nBook.organization.orgLogo.t,
                            hintText: i18nBook.common.Select.t,
                            isImage: true,
                            imageUrl: controller.state.logoUrl,
                            onClick: () => controller.uploadImage(),
                          ),
                          InputRow(
                            title: i18nBook.organization.orgName.t,
                            hintText: i18nBook.common.pleaseEnter.t,
                            controller: controller.orgName,
                            onChanged: (value) =>
                                controller.state.organizationName = value,
                          ),
                          FContainer(
                            color: Colors.white,
                            padding: EdgeInsets.only(left: 20, right: 40),
                            margin: EdgeInsets.only(bottom: 1),
                            height: 50,
                            child: FRow(
                              children: [
                                FText(i18nBook.auth.hospitalCode.t),
                                FExpanded(child: FSizedBox()),
                                FText(controller.state.orgShortCode),
                              ],
                            ),
                          ),
                          SelectRow(
                            title: i18nBook.organization.upperOrg.t,
                            hintText: i18nBook.common.Select.t,
                            value: controller.state.parentName,
                            onClick: () => controller.openSelectOrganization(),
                          ),
                          SelectRow(
                            title: i18nBook.profileComplete.areaLocated.t,
                            hintText: i18nBook.common.Select.t,
                            value: controller.state.areaName,
                            onClick: () => controller.openSelectArea(),
                          ),
                        ],
                      );
                    },
                  ),
                ),
              ),
            ],
          ),
        ),
        bottomNavigationBar: FRow(
          children: [
            FExpanded(
              flex: 1,
              child: FBottomCanCelButton(
                name: "OrgInfo",
                businessParent: businessParent,
                title: i18nBook.common.cancel.t,
                onPressed: () => router.back(),
              ),
            ),
            FExpanded(
              flex: 1,
              child: FBottomSubmitButton(
                businessParent: businessParent,
                name: "OrgInfo",
                title: i18nBook.common.confirm.t,
                onPressed: () => controller.onSubmit(),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
