import 'package:flutter/material.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:flyinsono/lab/components/rect_icon_button.dart';
import 'package:flyinsono/lab/pages/lab_task/controller.dart';
import 'package:flyinsono/lab/pages/lab_task/widgets/task_detail_item.dart';
import 'package:flyinsono/lab/style/lab_box_decoration.dart';
import 'package:get/get.dart';
import 'package:fis_jsonrpc/rpc.dart';

class TaskDetailList extends GetView<LabTaskController> {
  const TaskDetailList({
    Key? key,
    this.tabHashCode,
  }) : super(key: key);
  final String? tabHashCode;
  @override
  String? get tag => tabHashCode;
  @override
  Widget build(BuildContext context) {
    return GetBuilder<LabTaskController>(
      id: LabTaskController.taskDetailListId,
      tag: tabHashCode,
      builder: (context) {
        return Container(
          padding: EdgeInsets.all(10),
          decoration: LabBoxDecoration.base,
          child: ListView.builder(
            itemCount: controller.taskDetailList.length,
            itemBuilder: (context, index) {
              var taskDetail = controller.taskDetailList[index];
              var status = taskDetail.status;
              var taskType = controller.currentSelectedTask?.taskType;
              var taskName = taskDetail.name ?? '';
              var desc = taskDetail.description ?? '';
              var progressPercentage =
                  "${taskDetail.rate.toStringAsFixed(2).replaceAll(RegExp(r'\.00$'), '')}%";
              if (desc != '') {
                desc = desc + ' 【' + progressPercentage + '】';
              } else {
                desc = progressPercentage;
              }
              return TaskDetailItem(
                title: taskName,
                content: desc,
                progress: taskDetail.rate / 100,
                status: status,
                isSuccess: taskDetail.result,
                operationButton: (status == VTaskStatus.Completed &&
                        taskType != VTaskType.UserImport &&
                        taskType != VTaskType.UserExport)
                    ? _buildOperationButtonA(controller.taskDetailList[index],
                        controller.currentTaskInfo.patientCode)
                    : null,
              );
            },
          ),
        );
      },
    );
  }

  _buildOperationButtonA(TaskDetail task, String? patientCode) {
    String tooltipMessage = "";
    IconData icon = Icons.pause_rounded;
    Function()? onClick;
    switch (task.status) {
      case VTaskStatus.Unhandled:
      case VTaskStatus.Processing:
        // tooltipMessage = "暂停";
        // icon = Icons.pause_rounded;
        // onClick = () {
        //   controller.cancelTimer();
        // };
        break;
      case VTaskStatus.Completed:
        tooltipMessage = "查看";
        icon = Icons.folder_open_rounded;
        onClick = () {
          controller.viewTaskDetail(task, patientCode);
        };
        break;
      // case VTaskStatus.pause:
      //   tooltipMessage = "继续";
      //   icon = Icons.play_arrow_rounded;
      //   break;
      default:
        break;
    }
    return RectIconButton(
      icon: icon,
      tooltipMessage: tooltipMessage,
      onPressed: onClick,
      iconColor: LabColors.base800,
      hoverBgColor: LabColors.base400,
    );
  }
}
