import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/pages/remedical/consultation/widgets/status_label.dart';

///设备信息卡片
class TerminalCard extends StatefulWidget implements FWidget {
  TerminalCard(
    this.isSelected,
    this.terminal, {
    Key? key,
  });

  ///是否选中
  final bool isSelected;

  ///设备信息实体类
  final DeviceExtendInfoDTO terminal;

  @override
  State<StatefulWidget> createState() => _TerminalCard();
}

class _TerminalCard extends State<TerminalCard> {
  bool _isOnline = false;

  @override
  void initState() {
    super.initState();
  }

  @override
  FWidget build(BuildContext context) {
    _isOnline = widget.terminal.isOnline;
    bool isActive = widget.isSelected;
    final child = FStack(
      children: [
        FContainer(
          child: FColumn(
            children: [
              _buildDeviceOnlineInfo(isActive),
              const FSizedBox(height: 10),
              _buildDeviceInfoLine(i18nBook.user.deviceCode.t,
                  widget.terminal.shortCode ?? i18nBook.remedical.notFill.t),
              const FSizedBox(height: 10),
              _buildDeviceInfoLine(
                  i18nBook.user.deviceType.t, widget.terminal.deviceType ?? ''),
              const FSizedBox(height: 10),
              _buildDeviceInfoLine(i18nBook.user.deviceModel.t,
                  widget.terminal.deviceModel ?? i18nBook.remedical.notFill.t),
              const FSizedBox(height: 10),
              _buildDeviceInfoLine(
                  i18nBook.user.OrganizationLocated.t,
                  widget.terminal.organizationName ??
                      i18nBook.remedical.notFill.t),
              const FSizedBox(height: 10),
            ],
          ),
        ),
      ],
    );

    return FColumn(
      children: [
        child,
        FSizedBox(height: 15),
      ],
    );
  }

  ///一行设备信息
  FWidget _buildDeviceInfoLine(String key, String value) {
    final textStyle = TextStyle(
      color: widget.isSelected ? Colors.white : Colors.black,
    );
    return FRow(
      children: [
        FContainer(
          /// TODO:[Gavin] 多语言-UI-整合
          width: i18nBook.isCurrentChinese ? 80 : 120,
          child: FText(
            key,
            style: textStyle,
          ),
        ),
        FText(
          value,
          style: textStyle,
        ),
      ],
    );
  }

  ///在线设备信息
  FWidget _buildDeviceOnlineInfo(bool isActive) {
    return FRow(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        FExpanded(
          child: FText(
            widget.terminal.displayName ?? "",
            style: TextStyle(
              color: isActive ? Colors.white : Colors.black87,
              fontSize: 18,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
        FContainer(
          alignment: Alignment.centerRight,
          width: 95,
          height: 24,
          child: _isOnline
              ? StatusLabel(
                  title: i18nBook.user.online.t,
                  color: Color.fromRGBO(124, 182, 90, 1))
              : StatusLabel(
                  title: i18nBook.user.offline.t, color: Colors.grey[500]),
        ),
      ],
    );
  }
}
