import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/services/device.m.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:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/desktop/names.dart';
import 'package:flyinsono/architecture/router/router.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/pages/user/user_device/controller.dart';
import 'package:flyinsono/pages/user/user_device/widgets/mobile_card_item.dart';

///手机端设备卡片样式
class MobileDeviceCard extends FisView<DeviceController> {
  ///父级节点
  final FPage businessParent;

  ///位置
  final int index;

  MobileDeviceCard(
    this.businessParent, {
    this.deviceExtendInfoDTO,
    this.isOrgdevice,
    required this.index,
  });

  ///设备信息实体类
  final DeviceExtendInfoDTO? deviceExtendInfoDTO;

  ///是否组织设备
  final bool? isOrgdevice;
  @override
  FWidget build(BuildContext context) {
    return FInkWell(
      onTap: () async {
        if (isOrgdevice ?? false) {
          controller.state.selectTerminalCardIndex = index;
          await _onDeviceTap();
        }
      },
      child: FContainer(
        height: 210,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.all(
            Radius.circular(8),
          ),
          color: Colors.white,
        ),
        margin: EdgeInsets.only(left: 15, right: 15, top: 10),
        child: FColumn(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            _buildDeviceInfosRow(),
            if (controller.state.isOrgDevice ||
                !Store.user.isPrincipal ||
                (Store.user.isPrincipal &&
                    controller.state.selectedTerminal.organizationCode !=
                        Store.user.departmentCode)) ...[
              FContainer(
                margin: EdgeInsets.only(
                  top: 10,
                  bottom: 2,
                ),
                decoration: BoxDecoration(
                  border: Border(
                    bottom: BorderSide(
                      color: Color.fromRGBO(245, 245, 245, 1),
                    ),
                  ),
                  // color: Colors.white,
                ),
              ),
              _buildOperateButtons(),
            ],
          ],
        ),
      ),
    );
  }

  ///点击设备卡片
  _onDeviceTap() async {
    await router.to(
      RouteNames.Org.AddDevice,
      parameters: {
        'scanData': deviceExtendInfoDTO?.shortCode ?? '',
        'isEdit': 'true',
      },
    );
    _dispose();
  }

  void _dispose() {
    controller.nameEditingController.text = '';
    controller.shortCodeController.text = '';
    controller.deviceTypeController.text = '';
    controller.deviceModelController.text = '';
    controller.deviceNameEditingController.text = '';
    controller.state.deviceInfo.isNewDevice = false;
    controller.orgNameController.text = '';
    controller.textEditingController.text = '';
  }

  ///手机端设备卡片
  FWidget _mobileDeviceCard(String title, String value) {
    return FRow(
      children: [
        FContainer(
          width: i18nBook.isCurrentRussian
              ? 135
              : i18nBook.isCurrentSpanish
                  ? 150
                  : 100,
          child: FText(
            '$title',
            style: TextStyle(
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
        FText(
          '$value',
        ),
      ],
    );
  }

  ///设备信息其中一行
  FWidget _buildDeviceInfosRow() {
    return FRow(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        FContainer(
          margin: EdgeInsets.only(right: 15, left: 10, bottom: 5, top: 10),
        ),
        FExpanded(
          child: FContainer(
            margin: EdgeInsets.only(top: 5),
            child: FColumn(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                FContainer(
                  child: FRow(
                    mainAxisSize: MainAxisSize.max,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      FContainer(
                        width: 220,
                        child: FText(
                          deviceExtendInfoDTO?.displayName ?? '',
                          style: TextStyle(
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                      FContainer(
                        child: FRow(
                          children: [
                            deviceExtendInfoDTO!.isOnline
                                ? _buildOnlineInfos()
                                : _buildOffLineInfos()
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
                const FSizedBox(height: 10),
                _mobileDeviceCard(
                  i18nBook.user.deviceCode.t,
                  deviceExtendInfoDTO?.shortCode ?? '',
                ),
                const FSizedBox(height: 5),
                _mobileDeviceCard(
                  i18nBook.user.deviceType.t,
                  deviceExtendInfoDTO?.deviceType ?? '',
                ),
                const FSizedBox(height: 5),
                _mobileDeviceCard(
                  i18nBook.user.deviceModel.t,
                  deviceExtendInfoDTO?.deviceModel ?? '',
                ),
                const FSizedBox(height: 5),
                _mobileDeviceCard(
                  i18nBook.user.OrganizationLocated.t,
                  deviceExtendInfoDTO?.organizationName ?? '',
                ),
                FSizedBox(
                  height: 5,
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }

  ///操作按钮集合
  FWidget _buildOperateButtons() {
    final dto = deviceExtendInfoDTO!;
    final canUserEditDevice = dto.description == null ||
        dto.description!.isEmpty ||
        dto.description == dto.name;
    return FExpanded(
      child: FRow(
        mainAxisAlignment: isOrgdevice!
            ? MainAxisAlignment.spaceAround
            : MainAxisAlignment.spaceEvenly,
        children: [
          if (canUserEditDevice)
            MobileDeviceCardItem(
              label: i18nBook.common.modify.t,
              onPressed: () {
                controller.state.selectTerminalCardIndex = index;
                _onDeviceTap();
              },
            ),
          if (isOrgdevice!)
            MobileDeviceCardItem(
              label: i18nBook.remedical.share.t,
              onPressed: () => controller.openShareUser(deviceExtendInfoDTO!),
            ),
          if (isOrgdevice!)
            MobileDeviceCardItem(
              label: i18nBook.user.userRelated.t,
              onPressed: () =>
                  controller.openConnectionUser(deviceExtendInfoDTO!),
            ),
          if (controller.state.isOrgDevice ||
              !Store.user.isPrincipal ||
              (Store.user.isPrincipal &&
                  controller.state.selectedTerminal.organizationCode !=
                      Store.user.departmentCode)) ...[
            MobileDeviceCardItem(
              label: i18nBook.user.deleteDevices.t,
              onPressed: () => controller.openDeleteDevice(
                deviceExtendInfoDTO!,
                businessParent,
              ),
            ),
          ],
        ],
      ),
    );
  }

  ///设备在线信息
  FWidget _buildOnlineInfos() {
    return FStack(
      children: [
        FContainer(
          width: 40,
          height: 22,
          decoration: new BoxDecoration(
            color: Color.fromRGBO(104, 172, 67, 1),
            borderRadius: BorderRadius.circular(11),
          ),
        ),
        FContainer(
          /// TODO:[Gavin] 多语言-UI-整合
          width: i18nBook.isCurrentChinese ? 30 : 60,
          height: 22,
          margin: EdgeInsets.only(left: 11),
          decoration: new BoxDecoration(
            color: Color.fromRGBO(104, 172, 67, 1),
            border: Border(
              top: BorderSide(
                width: 1.0,
                color: Colors.grey,
              ),
              bottom: BorderSide(
                width: 1.0,
                color: Colors.grey,
              ),
            ),
          ),
          child: FText(
            i18nBook.user.online.t,
            style: TextStyle(
              fontSize: 14,
              color: Colors.white,
              fontFamily: FTheme.ins.localeSetting.fontFamily,
            ),
          ),
        )
      ],
    );
  }

  ///设备不在线信息
  FWidget _buildOffLineInfos() {
    return FStack(
      children: [
        FContainer(
          width: 40,
          height: 22,
          decoration: new BoxDecoration(
            color: Colors.grey[500],
            borderRadius: BorderRadius.circular(11),
          ),
        ),
        FContainer(
          /// TODO:[Gavin] 多语言-UI-整合
          width: i18nBook.isCurrentChinese ? 30 : 60,
          height: 22,
          margin: EdgeInsets.only(left: 11),
          decoration: new BoxDecoration(
            color: Colors.grey[500],
            border: Border(
              top: BorderSide(
                width: 1.0,
                color: Colors.grey[500]!,
              ),
              bottom: BorderSide(
                width: 1.0,
                color: Colors.grey[500]!,
              ),
            ),
          ),
          child: FText(
            i18nBook.user.offline.t,
            style: TextStyle(
              fontSize: 14,
              color: Colors.white,
              fontFamily: FTheme.ins.localeSetting.fontFamily,
            ),
          ),
        ),
      ],
    );
  }
}
