import { CommonStore } from '@/stores/common';
import { getTagKeyMap, NODE_TYPE, tagSortList, UNDEFINED_TAG } from '@/utils/enum/node';
import { PROJECT_EXE_STATUS_CODE } from '@/utils/enum/project';

const commonStore = CommonStore();

/** 将用户数组转换成用户名字符串显示 */
export const disposeMemberList = (row:any, key:string = 'memberList') => {
  const managerNames:string[] = [];
  if (Array.isArray(row[key]) && row[key]?.length) {
    row[key].forEach(
      (item: {
      name: string;
      realName: string;
      nickname: string;
    }) => {
        managerNames.push(item.nickname || item.realName || item.name);
      });
  }
  const nameStr = managerNames.join(',');
  row.hoverTitle = nameStr;
  return nameStr;
};

export const fakeUserList = [
  { label: '张三', name: '张三', identity: 'zs', value: { 'name': '张三', 'identity': 'zs' } },
  { label: '李四', name: '李四', identity: 'ls', value: { 'name': '李四', 'identity': 'ls' } },
  { label: '王五', name: '王五', identity: 'ww', value: { 'name': '王五', 'identity': 'ww' } },
];

export const disposeTagKey = (row:any, pTagKeyList:any, tagKeyMap: any) => {
  // console.log('disposeTagKey', row, pTagKeyList, tagKeyMap);
  if ( row.nodeType !== NODE_TYPE.PERFORMANCE && pTagKeyList) {
    // task继承父级的tagKey就行
    if (row.nodeType === NODE_TYPE.TASK) {
      row.tagKeyList = [...pTagKeyList] ;
    } else {
      const tagKey:string = tagKeyMap.get(row.nodeType) || UNDEFINED_TAG;
      row.tagKeyList = [...pTagKeyList, { [tagKey]: row.uuid }] ;
    }
  }
  return row.tagKeyList;
};

export const getIdMap = (projectUuid?: string | null, phaseUuid?: string | null) => {
  const tagMap = getTagKeyMap();

  const tagSort = tagSortList.map(item => {
    if (projectUuid && item === tagMap.get(NODE_TYPE.PROJECT)) {
      return {
        key: projectUuid,
        value: item,
      };
    }
    if (phaseUuid && item === tagMap.get(NODE_TYPE.PHASE)) {
      return {
        key: phaseUuid,
        value: item,
      };
    }
    return {
      key: null,
      value: item,
    };
  });
  return tagSort;
};

export const getProjectExeStatus = (exeStatus: PROJECT_EXE_STATUS_CODE) => {
  let statusList = [];
  const statusDictList:any = commonStore.getDictData('PROJECT_EXE_STATUS').A;
  console.log('statusDictList', statusDictList);
  if (exeStatus === PROJECT_EXE_STATUS_CODE.NOT_STARTED) {
    statusList = statusDictList;
  } else if (exeStatus === PROJECT_EXE_STATUS_CODE.CLOSED) {
    statusList = statusDictList.filter((item: { value: PROJECT_EXE_STATUS_CODE; }) => item.value === PROJECT_EXE_STATUS_CODE.CLOSED);
  } else if (exeStatus === PROJECT_EXE_STATUS_CODE.COMPLETED) {
    statusList = statusDictList.filter((item: { value: PROJECT_EXE_STATUS_CODE; }) => item.value === PROJECT_EXE_STATUS_CODE.COMPLETED);
  } else if (exeStatus === PROJECT_EXE_STATUS_CODE.IN_PROGRESS) {
    statusList = statusDictList.filter((item: { value: PROJECT_EXE_STATUS_CODE; }) => item.value !== PROJECT_EXE_STATUS_CODE.NOT_STARTED && item.value !== PROJECT_EXE_STATUS_CODE.CLOSED);
  } else if (exeStatus === PROJECT_EXE_STATUS_CODE.PAUSED) {
    statusList = statusDictList.filter((item: { value: PROJECT_EXE_STATUS_CODE; }) => item.value === PROJECT_EXE_STATUS_CODE.IN_PROGRESS || item.value === PROJECT_EXE_STATUS_CODE.PAUSED);
  } else if (exeStatus === PROJECT_EXE_STATUS_CODE.POSTPONED) {
    statusList = statusDictList.filter((item: { value: PROJECT_EXE_STATUS_CODE; }) => item.value === PROJECT_EXE_STATUS_CODE.IN_PROGRESS || item.value === PROJECT_EXE_STATUS_CODE.COMPLETED || item.value === PROJECT_EXE_STATUS_CODE.PAUSED || item.value === PROJECT_EXE_STATUS_CODE.POSTPONED);
  } else {
    statusList = statusDictList;
  }
  return statusList;
};

export const filterTask = (taskTree: any[]):any[] => {
  const list = [];
  for (let index = 0; index < taskTree.length; index++) {
    if (taskTree[index].nodeType !== NODE_TYPE.TASK) {
      const obj = { ...taskTree[index], children: [] };
      if (taskTree[index].children) {
        obj.children = filterTask(taskTree[index].children);
      }
      list.push(obj);
    }
  }
  return list;
};
