import 'dart:async';

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:fis_ui/widgets/layout/button_group.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/pages/user/user_device/controller.dart';
import 'package:async/async.dart';

///设备信息集合
class TerminalInfosPage extends FisView<DeviceController> implements FPage {
  @override
  final String pageName;
  final double _sizeBetween = 100;
  final double _marginLeft = 30;
  final double _topMargin = 50;
  final AsyncMemoizer _memoizer = AsyncMemoizer();
  bool isClicked = false;
  TerminalInfosPage({this.pageName = 'TerminalInfos'});
  @override
  FWidget build(BuildContext context) {
    return FContainer(
      child: FObx(
        () {
          return FColumn(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              FExpanded(
                child: controller.state.terminals.length == 0
                    ? NoDataWidget() as FWidget
                    : FListView(
                        shrinkWrap: true,
                        children: [
                          if (controller.state.terminals.length > 0) ...[
                            _buildDeviceInfoLine(
                                i18nBook.user.deviceCode.t,
                                controller.state.selectedTerminal.shortCode ??
                                    ''),
                            _buildDeviceInfoLine(
                                i18nBook.user.deviceDescription.t,
                                controller.state.deviceDescription),
                            _buildDeviceInfoLine(
                                i18nBook.user.deviceModel.t,
                                controller.state.selectedTerminal.deviceModel ??
                                    ""),
                            _buildDeviceInfoLine(
                                i18nBook.user.OrganizationLocated.t,
                                controller.state.selectedTerminal
                                        .organizationName ??
                                    ""),
                            _buildCoverImageRow(i18nBook.user.coverImage.t),
                          ],
                        ],
                      ),
              ),
              FDivider(
                height: 1,
                color: FTheme.ins.colorScheme.line,
              ),
              _buildOperateRow(),
            ],
          );
        },
      ),
    );
  }

  FWidget _buildCoverImageRow(String title) {
    final imageWidth = 200.0;
    final imageHeight = 200.0;
    return FContainer(
      margin: EdgeInsets.only(left: _marginLeft, top: _topMargin),
      child: FTitleField(
        title,
        sizeBetween: _sizeBetween,
        controller.state.deviceImage.isNotEmpty
            ? FImage.network(
                controller.state.deviceImage,
                width: imageWidth,
                height: imageHeight,
              )
            : FImage.asset(
                "assets/images/mock_terminal.png",
                width: imageWidth,
                height: imageHeight,
              ),
      ),
    );
  }

  FWidget _buildDeviceInfoLine(String title, String field) {
    return FContainer(
      margin: EdgeInsets.only(left: _marginLeft, top: _topMargin),
      child: FRow(
        children: [
          FContainer(
            width: i18nBook.isCurrentRussian ? 180 : 135,
            child: FText(
              '$title',
              style: TextStyle(
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
          FText(
            '$field',
          ),
        ],
      ),
    );
  }

  FWidget _buildOperateRow() {
    var textStyle = TextStyle(color: Colors.black);
    bool isShowDeleteDevice = controller.state.isOrgDevice ||
        !Store.user.isPrincipal ||
        (Store.user.isPrincipal &&
            controller.state.selectedTerminal.organizationCode !=
                Store.user.departmentCode);
    var buttonStyle = ButtonStyle(
      padding: MaterialStateProperty.all(
        EdgeInsets.symmetric(vertical: 8),
      ),
    );
    final terminal = controller.state.selectedTerminal;
    final canUserEditDevice = terminal.description == null ||
        terminal.description!.isEmpty ||
        terminal.description == terminal.name;
    final List<FWidget> children = [
      if (controller.state.isOrgDevice ||
          (canUserEditDevice &&
              (terminal.description == null ||
                  terminal.description!.isEmpty ||
                  terminal.description == terminal.name))) ...[
        FOutlinedButton(
          businessParent: this,
          onPressed: () {
            controller.updateTerminal();
          },
          style: buttonStyle,
          name: i18nBook.common.modify.t,
          child: FText(
            i18nBook.common.modify.t,
            style: textStyle,
          ),
        ),
      ],
      if ((isShowDeleteDevice) && controller.state.terminals.length != 0) ...[
        FOutlinedButton(
          businessParent: this,
          onPressed: () async {
            await controller.removeTerminal(this);
          },
          style: buttonStyle,
          name: i18nBook.user.deleteDevice.t,
          child: FText(
            i18nBook.user.deleteDevice.t,
            style: textStyle,
          ),
        ),
      ],
      if (controller.state.isOrgDevice) ...[
        FOutlinedButton(
          businessParent: this,
          onPressed: () {
            if (!isClicked) {
              isClicked = true;
              // 执行逻辑代码
              controller.shareTerminal();
              Timer(Duration(seconds: 2), () {
                isClicked = false;
              });
            }
          },
          style: buttonStyle,
          name: i18nBook.user.shareDevice.t,
          child: FText(
            i18nBook.user.shareDevice.t,
            style: textStyle,
          ),
        ),
      ],
    ];
    if (children.length == 0) {
      return FSizedBox();
    }
    return FContainer(
      padding: EdgeInsets.all(10),
      child: FButtonGroup(
        buttonPadding: 15,
        children: children,
      ),
    );
  }
}
