import 'package:fis_common/index.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class FPatchInfoCard extends FStatefulWidget implements FPage {
  ///更新包信息
  final DevicePatchDTO patchInfo;

  final bool isSelected;

  final void Function(bool)? onTap;

  FPatchInfoCard(
      {required this.patchInfo, this.isSelected = false, this.onTap});

  @override
  String get pageName => 'FPatchInfoCard';
  @override
  FState<FStatefulWidget> createState() {
    return FPatchInfoCardState();
  }
}

class FPatchInfoCardState extends FState<FPatchInfoCard> {
  TextStyle _textStyle = TextStyle(fontSize: 16);
  bool _isSelected = false;
  DevicePatchDTO get patchInfo => widget.patchInfo;

  @override
  FWidget build(BuildContext context) {
    return FGestureDetector(
      businessParent: widget,
      name: 'FPatchInfoCard',
      onTap: () {
        setState(() {
          _isSelected = !_isSelected;
        });
        widget.onTap?.call(_isSelected);
      },
      child: FContainer(
        decoration: BoxDecoration(
          color: Colors.white, // 设置容器背景颜色
          borderRadius: BorderRadius.circular(8), // 设置圆角边框
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.5), // 阴影颜色
              blurRadius: 5, // 模糊半径
              spreadRadius: 1, // 扩散半径
              offset: Offset(3, 3), // 阴影偏移
            ),
          ],
        ),
        margin: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
        padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
        child: FColumn(
          children: [
            FRow(
              children: [
                FExpanded(
                  child: _buildFirstColumn(),
                ),
                if (_isSelected) ...[
                  FIcon(
                    Icons.check_circle,
                    color: Colors.green,
                  ),
                ] else ...[
                  FIcon(
                    Icons.circle_outlined,
                    color: Colors.grey,
                  ),
                ],
                FSizedBox(
                  width: 10,
                )
              ],
            ),
            _verticalDivider(),
            FContainer(
                // padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
                child: _buildSecondColumn()),
          ],
        ),
      ),
    );
  }

  @override
  void didUpdateWidget(FPatchInfoCard old) {
    if (widget.isSelected != old.isSelected) {
      setState(() {
        _isSelected = widget.isSelected;
      });
    }
    super.didUpdateWidget(old);
  }

  FWidget _buildFirstColumn() {
    return FContainer(
      child: FColumn(
        // mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          FText(
            patchInfo.name ?? '',
            style:
                _textStyle.copyWith(fontSize: 18, fontWeight: FontWeight.bold),
            textAlign: TextAlign.start,
          ),
          if (patchInfo.description != null &&
              patchInfo.description!.isNotEmpty)
            FText(
              patchInfo.description ?? '',
              style: _textStyle.copyWith(fontSize: 14, color: Colors.grey),
              textAlign: TextAlign.start,
            ),
        ],
      ),
    );
  }

  FWidget _buildSecondColumn() {
    DateFormat dateFormat = DateFormat('yyyy-MM-dd');
    var fileSize = patchInfo.fileSize / 1024 / 1024;
    return FColumn(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _buildTitleValue(
              i18nBook.remoteControl.applicableSystem.t,
              patchInfo.osVersion.isNotNullOrEmpty
                  ? patchInfo.osVersion!
                  : i18nBook.remoteControl.universal.t),
          _buildTitleValue(
              i18nBook.remoteControl.applicableSoftware.t,
              patchInfo.softwareVersion.isNotNullOrEmpty
                  ? patchInfo.softwareVersion!
                  : i18nBook.remoteControl.universal.t),
          _buildTitleValue(
              i18nBook.remoteControl.size.t,
              i18nBook.remoteControl.sizeMB
                  .translate([fileSize.toStringAsFixed(2)])),
          _buildTitleValue(i18nBook.remoteControl.releaseTime.t,
              dateFormat.format(patchInfo.createTime ?? DateTime.now())),
        ]);
  }

  FWidget _buildTitleValue(String title, String value) {
    return FRow(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        FContainer(
          /// TODO:[Gavin] 多语言-UI-整合
          width: i18nBook.isCurrentChinese ? 80 : 140,
          child: FText(
            title,
            style: TextStyle(fontWeight: FontWeight.bold),
          ),
        ),
        const FSizedBox(width: 10),
        FExpanded(
          child: FText(
            value,
            overflow: TextOverflow.fade,
          ),
        ),
      ],
    );
  }

  FWidget _verticalDivider() {
    return FContainer(
      margin: EdgeInsets.symmetric(vertical: 10),
      color: Colors.grey[400],
      height: 1,
    );
  }
}
