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/widgets/outlined_progress_indicator.dart';
import 'package:fis_jsonrpc/rpc.dart';

class TaskDetailItem extends StatelessWidget {
  const TaskDetailItem({
    super.key,
    required this.title,
    required this.content,
    required this.progress,
    required this.status,
    this.operationButton,
    this.isSuccess = true,
  });

  final String title;
  final bool isSuccess;
  final String content;
  final double progress;
  final VTaskStatus status;
  final RectIconButton? operationButton;

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 50,
      margin: EdgeInsets.only(bottom: 10),
      padding: EdgeInsets.symmetric(vertical: 5, horizontal: 10),
      decoration: BoxDecoration(
        color: LabColors.base300,
        borderRadius: BorderRadius.circular(5),
        border: Border.all(
          color: LabColors.base400,
          width: 1,
        ),
      ),
      child: Row(
        children: [
          Icon(
            Icons.file_download_rounded,
            size: 30,
            color: LabColors.base500,
          ),
          SizedBox(width: 10),
          Flexible(
            flex: 2,
            fit: FlexFit.tight,
            child: Align(
              alignment: Alignment.centerLeft,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    title,
                    style: TextStyle(fontSize: 13, color: LabColors.text800),
                  ),
                  Text(
                    content,
                    style: TextStyle(fontSize: 13, color: LabColors.text600),
                  ),
                ],
              ),
            ),
          ),
          Flexible(
            flex: 3,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                Flexible(
                  flex: 1,
                  child: Container(
                    constraints: BoxConstraints(minWidth: 200),
                    child: _buildStatus(),
                  ),
                ),
                Flexible(
                  flex: 1,
                  child: Container(
                    padding: EdgeInsets.symmetric(horizontal: 10),
                    child: OutlinedProgressIndicator(
                      progress: progress,
                    ),
                  ),
                ),
                Flexible(
                  child: SizedBox(width: 20),
                ),
                operationButton ?? SizedBox(width: 0),
                Flexible(
                  child: SizedBox(width: 20),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildStatus() {
    String statusText = "";
    Color statusColor = LabColors.base800;
    IconData? statusIcon;
    if (!isSuccess && progress == 1) {
      statusText = "失败";
      statusColor = Colors.red;
    } else {
      switch (status) {
        case VTaskStatus.Unhandled:
          statusText = "待开始";
          statusColor = LabColors.base600;
          break;
        case VTaskStatus.Processing:
          statusText = "处理中";
          statusColor = LabColors.base800;
          statusIcon = Icons.autorenew_rounded;
          break;
        // case VTaskStatus.:
        //   statusText = "暂停";
        //   statusColor = LabColors.base600;
        //   break;
        case VTaskStatus.Completed:
          statusText = "已完成";
          statusColor = LabColors.green600;
          statusIcon = Icons.task_alt_rounded;
          break;
        case VTaskStatus.Canceled:
          statusText = "已取消";
          statusColor = LabColors.base600;
          break;

        default:
          statusText = "未知状态";
          statusColor = LabColors.base600;
          break;
      }
    }
    return Row(
      children: [
        Icon(
          statusIcon,
          size: 16,
          color: statusColor,
        ),
        SizedBox(width: 5),
        Text(
          statusText,
          style: TextStyle(
            fontSize: 16,
            height: 1.2,
            color: statusColor,
          ),
        ),
      ],
    );
  }
}
