import 'package:flyinsono/lab/mock_data/mock_rpc.dart';
import 'package:flyinsono/managers/interfaces/entities/process_arg.dart';
import 'package:flyinsono/managers/interfaces/entities/task_info.dart';
import 'package:flyinsono/managers/interfaces/export_data.dart';
import 'package:flyinsono/managers/interfaces/import_data.dart';
import 'package:get/get.dart';
import 'package:fis_common/event/event_type.dart';
import 'package:fis_jsonrpc/rpc.dart';

///本地化任务中心
TaskCenter get localTaskCenter => _taskCenterInstance;

final _taskCenterInstance = _buildTaskCenterInstance();

TaskCenter _buildTaskCenterInstance() {
  return TaskCenter();
}

class TaskCenter {
  final importDataManager = Get.find<IImportDataManager>();
  final exportDataManager = Get.find<IExportDataManager>();
  final processChange = FEventHandler<ExportProcessArg>();

  TaskCenter() {
    processChange.addListener((sender, e) {
      importDataManager.processChange.emit(sender, e);
      exportDataManager.processChange.emit(sender, e);
    });
  }

  ///获取本地的任务标题列表
  List<TaskBaseDTO> getTaskList() {
    List<TaskBaseDTO> result = [];
    TaskBaseDTO? importTaskModel = importDataManager.getCurrentTask();
    if (importTaskModel != null) {
      result.add(importTaskModel);
    }
    TaskBaseDTO? exportTaskModel = exportDataManager.getCurrentTask();
    if (exportTaskModel != null) {
      result.add(exportTaskModel);
    }

    var resultList = result
        .map(
          (e) => TaskBaseDTO(
            code: e.code,
            taskName: e.taskName,
            status: VTaskStatus.Processing,
            taskType: e.taskType,
          ),
        )
        .toList();
    resultList.sort((a, b) {
      // 比较两个TaskBaseDTO对象的createTime属性
      if (a.createTime == null && b.createTime == null) {
        return 0; // 都是null，视为相等
      } else if (a.createTime == null) {
        return 1; // null的排在后面
      } else if (b.createTime == null) {
        return -1; // null的排在后面
      } else {
        return b.createTime!.compareTo(a.createTime!); // 按照时间降序排列，最近的时间排在最前面
      }
    });
    for (var i in resultList) {
      print(i.taskName);
    }
    return resultList;
  }

  ///获取本地的任务详情列表
  List<TaskDetail> getLocalTasks(String currentSelectedTaskId) {
    if (currentSelectedTaskId == importDataManager.taskId) {
      return importDataManager.getLocalTasks(currentSelectedTaskId);
    } else if (currentSelectedTaskId == exportDataManager.taskId) {
      return exportDataManager.getLocalTasks(currentSelectedTaskId);
    } else {
      print(
          "target id:$currentSelectedTaskId,exportDataManager.taskId:${exportDataManager.taskId}");
    }
    return [];
  }

  void canCelTask(String currentSelectedTaskId) {
    if (currentSelectedTaskId == importDataManager.taskId) {
      importDataManager.cancelImport();
    } else if (currentSelectedTaskId == exportDataManager.taskId) {
      exportDataManager.cancelExport();
    }
  }

  TaskInfo? getTaskInfo(String currentSelectedTaskId) {
    if (currentSelectedTaskId == importDataManager.taskId) {
      return importDataManager.getTaskInfo(currentSelectedTaskId);
    } else if (currentSelectedTaskId == exportDataManager.taskId) {
      return exportDataManager.getTaskInfo(currentSelectedTaskId);
    }
    return null;
  }
}
