import 'dart:convert';

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_project/controller.dart';
import 'package:flyinsono/lab/style/lab_box_decoration.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:flyinsono/utils/datetime.dart';
import 'package:get/get.dart';

class ProjectTimeLine extends GetView<LabProjectController> {
  const ProjectTimeLine({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LabProjectController>(
      id: LabProjectController.projectTimeLine,
      builder: (context) {
        return Container(
          clipBehavior: Clip.hardEdge,
          decoration: LabBoxDecoration.base,
          child: Column(
            children: [
              SizedBox(
                height: 10,
              ),
              Container(
                height: 40,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Expanded(
                      child: Row(
                        children: [
                          SizedBox(
                            width: 10,
                          ),
                          Text(
                            "时间轴",
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(
                              fontSize: 16,
                              height: 1,
                              fontWeight: FontWeight.bold,
                              color: LabColors.text800,
                            ),
                          ),
                          SizedBox(
                            width: 10,
                          ),
                          Icon(
                            Icons.access_time_rounded,
                            size: 20,
                            color: LabColors.text800,
                          ),

                          // TODO 添加时间轴的筛选功能
                        ],
                      ),
                    ),
                    RectIconButton(
                      icon: Icons.refresh_rounded,
                      tooltipMessage: "刷新",
                      onPressed: () {
                        controller.getTaskListByProjectCode();
                      },
                    ),
                    RectIconButton(
                      icon: Icons.eject_rounded,
                      tooltipMessage: "收起时间轴",
                      onPressed: () {
                        controller.setTimeLineCollapse(true);
                      },
                    ),
                    SizedBox(
                      width: 10,
                    ),
                  ],
                ),
              ),
              SizedBox(
                height: 10,
              ),
              Expanded(
                child: Container(
                  decoration: BoxDecoration(
                    border: Border(
                      top: BorderSide(
                        color: LabColors.base400,
                        width: 1,
                      ),
                    ),
                  ),
                  child: ListView(
                    padding: EdgeInsets.only(bottom: 20),
                    itemExtent: 65,
                    children: controller.taskList
                        .map((e) => _buildLogItem(e))
                        .toList(),
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  Widget _buildLogItem(TimeAxisDetail tast) {
    return Container(
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(
            color: LabColors.base400,
            width: 1,
          ),
        ),
      ),
      padding: const EdgeInsets.all(10),
      child: Column(
        children: [
          Row(
            children: [
              Expanded(
                child: Text(
                  _getTaskStatus(
                    tast.timeAxisType,
                    tast,
                    operatorName: tast.operatorName,
                  ),
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                    fontSize: 14,
                    color: LabColors.text800,
                  ),
                ),
              ),
            ],
          ),
          Expanded(child: SizedBox()),
          Row(
            children: [
              Text(
                DataTimeUtils.convertToLocalString(
                  tast.operateTime ?? DateTime.now(),
                ),
                style: TextStyle(
                  fontSize: 12,
                  color: LabColors.text600,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  /// 解析当前添加或者删除的用户
  String _getUserList(String? json) {
    String name = "";
    List<dynamic> userList = [];
    if (json != null) {
      List<dynamic> dataJson = jsonDecode(json);
      for (var element in dataJson) {
        userList.add(element["Name"]);
        name = userList.join("、");
      }
    }
    return name;
  }

  String _getTaskStatus(TimeAxisDetailEnum status, TimeAxisDetail tast,
      {String? operatorName}) {
    switch (status) {
      case TimeAxisDetailEnum.ProjectCreate:
        return "$operatorName 创建了项目";
      case TimeAxisDetailEnum.DeviceUpload:
        return "超声机($operatorName)上传了数据";
      case TimeAxisDetailEnum.InviteUser:
        return "邀请了成员 ${_getUserList(tast.dataJson)}";
      case TimeAxisDetailEnum.RemoveUser:
        return "移除了成员 ${_getUserList(tast.dataJson)}";
      case TimeAxisDetailEnum.UserImport:
        return "$operatorName 导入了数据";
      case TimeAxisDetailEnum.UserExport:
        return "$operatorName 导出了数据";
      case TimeAxisDetailEnum.Startanalysis:
        return "$operatorName 创建了分析任务";
      case TimeAxisDetailEnum.NalysisComplete:
        return "分析任务执行完成";
      case TimeAxisDetailEnum.DeleteSample:
        return "$operatorName 删除了样本";
      case TimeAxisDetailEnum.DeleteRecord:
        return "$operatorName 删除了检查";
      case TimeAxisDetailEnum.ProjectUpdate:
        return "$operatorName 修改了项目";
      default:
        return "未知";
    }
  }
}
