// ignore_for_file: constant_identifier_names
import 'package:fis_common/index.dart';
import 'package:fis_jsonrpc/services/device.m.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/values/font_sizes.dart';
import 'package:fis_ui/values/spacings.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/views/common/device_live/device_live_detail/state.dart';
import 'package:flyinsono/views/common/device_live/widgets/device_online_sign.dart';
import 'package:get/get.dart';

import '../controller.dart';

/// 设备信息卡片
class DeviceInfoCard extends FStatelessWidget {
  static const _C_HEIGHT = 100.0;
  static const _C_COVER_SIZE = 86.0;
  static const _C_BORDER_WIDTH = 2.0;
  static const _C_PADDING_VERTICAL = (_C_HEIGHT - _C_COVER_SIZE) / 2;
  static const _C_PADDING_HORIZONTAL = _C_PADDING_VERTICAL;
  static const _C_MARGIN_HORIZONTAL = 12.0;
  static const _C_MARGIN_VERTICAL = 2.0;
  static const _C_COVER_BGCOLOR = Colors.white;
  //Color.fromRGBO(227, 227, 227, 1);
  static const _C_DEFAULT_COVER_URL = "assets/images/mock_terminal.png";

  DeviceInfoCard(
    this.index, {
    super.key,
  }) {
    _loadData();
  }

  /// 当前索引
  final int index;

  late final DeviceLiveDetailDesktopController controller;
  late final DeviceStateModel model;
  late final DeviceExtendInfoDTO dto;

  void _loadData() {
    controller = Get.find<DeviceLiveDetailDesktopController>();
    model = controller.state.deviceList[index];
    dto = model.dto;
  }

  @override
  FWidget build(BuildContext context) {
    final mainWdiget = _buildMain(context);
    if (dto.isOnline) {
      return FInkWell(
        child: mainWdiget,
        onTap: () {
          controller.handleDeviceChecked(!model.isChecked, index);
        },
      );
    } else {
      return FStack(
        children: [
          _buildOfflineWrapper(),
          mainWdiget,
        ],
      );
    }
  }

  FWidget _buildMain(BuildContext context) {
    double paddingHor = _C_PADDING_HORIZONTAL;
    double paddingVer = _C_PADDING_VERTICAL;
    if (model.isChecked) {
      // 被选中时，padding减去border占的尺寸
      paddingHor -= _C_BORDER_WIDTH;
      paddingVer -= _C_BORDER_WIDTH;
    }
    final primaryColor = Theme.of(context).primaryColor;
    return FObx(() {
      return FContainer(
        decoration: BoxDecoration(
          border: model.isChecked
              ? Border.all(
                  color: primaryColor,
                  width: _C_BORDER_WIDTH,
                )
              : null,
        ),
        margin: const EdgeInsets.symmetric(
          vertical: _C_MARGIN_VERTICAL,
          horizontal: _C_MARGIN_HORIZONTAL,
        ),
        padding:
            EdgeInsets.symmetric(vertical: paddingVer, horizontal: paddingHor),
        height: _C_HEIGHT,
        child: FRow(
          children: [
            const FSizedBox(width: 6),
            _buildCover(primaryColor),
            const FSizedBox(width: 8),
            FExpanded(
              child: FColumn(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  FMaterialTooltip(
                    child: _buildLabelCell(dto.displayName!, FFontSizes.H5),
                    message: dto.displayName!,
                  ),
                  const FSizedBox(height: FSpacings.S),
                  _buildLabelCell(dto.deviceModel!),
                ],
              ),
            ),
          ],
        ),
      );
    });
  }

  /// 封面
  FWidget _buildCover(Color primaryColor) {
    final coverUrl = dto.headPicUrl;
    final image = coverUrl.isNullOrEmpty
        ? FImage.asset(
            _C_DEFAULT_COVER_URL,
            width: _C_COVER_SIZE,
            height: _C_COVER_SIZE,
            fit: BoxFit.fill,
          )
        : FImage.network(
            coverUrl!,
            width: _C_COVER_SIZE,
            height: _C_COVER_SIZE,
            fit: BoxFit.cover,
          );
    return FStack(
      children: [
        FContainer(color: _C_COVER_BGCOLOR, child: image),
        if (dto.isOnline)
          const FPositioned(
            top: 2,
            left: 2,
            child: DeviceOnlineSign(),
          ),
      ],
    );
  }

  FWidget _buildLabelCell(String text, [double size = FFontSizes.BODY]) {
    return FContainer(
      child: FText(
        text,
        style: TextStyle(
          color: Colors.white,
          fontSize: size,
          overflow: TextOverflow.ellipsis,
        ),
      ),
    );
  }

  FWidget _buildOfflineWrapper() {
    return FPositioned(
      top: 0,
      bottom: 0,
      right: 0,
      left: 0,
      child: FOpacity(
        opacity: 0.16,
        child: Container(
          color: Colors.white,
        ),
      ),
    );
  }
}
