import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/views/desktop/index.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/controller/real_time_consultation_controller.dart';
import 'package:flyinsono/pages/remedical/consultation/widgets/status_label.dart';
import 'package:get/get.dart';

/// 实时会诊卡片
class FConsultationCard extends StatefulWidget implements FWidget {
  const FConsultationCard(
    // this.index,
    this.realTimeConsultationInfos, {
    super.key,
  });

  // /// 实时会诊索引
  // final int index;

  /// 实时会诊信息
  final ConsultationsRecordCard? realTimeConsultationInfos;

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

class _ConsultationCardState extends State<FConsultationCard> {
  final controller = Get.find<RealTimeConsultationController>();
  bool hovering = false;

  final buttonStyle = ElevatedButton.styleFrom(
    minimumSize: const Size(60, 30),
    maximumSize: const Size(60, 30),
    alignment: Alignment.center,
  );

  @override
  FWidget build(BuildContext context) {
    return FObx(() {
      bool isActive = widget.realTimeConsultationInfos?.consultationCode ==
          controller.state.currentConsultationCode;
      return _buildDesktopLayout(isActive);
    });
  }

  FWidget _buildDesktopLayout(bool isActive) {
    var dividerLength = const EdgeInsets.only(right: 15);
    final realTimeConsultationInfos = widget.realTimeConsultationInfos!;

    if (!isActive) {
      dividerLength = const EdgeInsets.only(right: 22);
    }

    final child = SwipeCard(
      isActive: isActive,
      onTap: () {
        controller.clickRealTimeConsultationCard(
          widget.realTimeConsultationInfos!,
        );
      },
      child: FColumn(
        children: [
          FRow(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.start,
            // 信息
            children: [
              FExpanded(
                child: FColumn(
                  children: [
                    FRow(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        _buildPatientNameTemplate(isActive),
                        _buildRegistrationStatusTemplate(),
                      ],
                    ),
                    const FSizedBox(
                      height: 15,
                    ),
                    FRow(
                      children: [
                        _buildTemplate(
                          i18nBook.remedical.age.t,
                          realTimeConsultationInfos.age ?? '',
                          isActive,
                        ),
                        _buildTemplate(
                          i18nBook.remedical.gender.t,
                          controller.getLanguageValues(
                            'exam',
                            widget.realTimeConsultationInfos!.sex!,
                          ),
                          isActive,
                        ),
                      ],
                    ),
                    FContainer(
                      margin: dividerLength,
                      child: const FDivider(
                        color: Color.fromRGBO(216, 216, 216, 1),
                        thickness: 0.5,
                      ),
                    ),
                    FRow(
                      children: [
                        FContainer(
                          width: 75,
                          child: FText(
                            '申请单位',
                            style: TextStyle(
                              color: isActive ? Colors.white : Colors.black87,
                              fontWeight: isActive
                                  ? FontWeight.normal
                                  : FontWeight.bold,
                            ),
                          ),
                        ),
                        FText(
                          widget.realTimeConsultationInfos!
                                  .applyOrganizationName ??
                              '',
                          style: TextStyle(
                            color: isActive ? Colors.white : Colors.black87,
                          ),
                        ),
                      ],
                    ),
                    const FSizedBox(
                      height: 10,
                    ),
                  ],
                ),
              ),
            ],
          ),
        ],
      ),
    );

    return child;
  }

  /// 姓名
  FWidget _buildPatientNameTemplate(bool isActive) {
    var name = widget.realTimeConsultationInfos!.patientName!;
    return FExpanded(
      child: FText(
        name,
        style: TextStyle(
          color: isActive ? Colors.white : Colors.black87,
          fontSize: 18,
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }

  FWidget _buildRegistrationStatusTemplate() {
    TransactionStatusEnum? state = widget.realTimeConsultationInfos?.state;
    Color waitForColor = Color.fromARGB(255, 219, 158, 45);
    Color requestedColor = Color.fromRGBO(124, 182, 90, 1);
    String title = '';
    Color bgColors = waitForColor;
    switch (state ?? '') {
      case TransactionStatusEnum.Applied:
        title = i18nBook.realTimeConsultation.requested.t;
        bgColors = requestedColor;
        break;
      case TransactionStatusEnum.Withdrawn:
        title = i18nBook.realTimeConsultation.withdrawn.t;
        bgColors = waitForColor;
        break;
      case TransactionStatusEnum.Rejected:
        title = i18nBook.realTimeConsultation.rejected.t;
        bgColors = Colors.red;
        break;
      case TransactionStatusEnum.ToStart:
        title = i18nBook.realTimeConsultation.toBeStarted.t;
        bgColors = waitForColor;
        break;
      case TransactionStatusEnum.InProgress:
        title = i18nBook.realTimeConsultation.haveInHand.t;
        bgColors = waitForColor;
        break;

      case TransactionStatusEnum.PendingReport:
        title = i18nBook.realTimeConsultation.toBeReported.t;
        bgColors = waitForColor;
        break;
      case TransactionStatusEnum.Expired:
        title = i18nBook.realTimeConsultation.expired.t;
        bgColors = waitForColor;
        break;
      case TransactionStatusEnum.End:
        title = i18nBook.realTimeConsultation.closed.t;
        bgColors = FTheme.ins.colorScheme.primary;
        break;
      default:
        break;
    }

    return FContainer(
      alignment: Alignment.centerRight,
      width: 120,
      child: StatusLabel(
        title: title,
        color: bgColors,
      ),
    );
  }

  FWidget _buildTemplate(
    String title,
    String label,
    bool isActive,
  ) {
    return FRow(
      children: [
        FContainer(
          width: 75,
          child: FText(
            title,
            style: TextStyle(
              color: isActive ? Colors.white : Colors.black87,
              fontWeight: isActive ? FontWeight.normal : FontWeight.bold,
            ),
          ),
        ),
        FContainer(
          width: 85,
          child: FText(
            label,
            style: TextStyle(
              color: isActive ? Colors.white : Colors.black87,
            ),
          ),
        ),
      ],
    );
  }
}
