import { roleFields, idMaps, yanYvIdMaps } from '@/convert/role/map';
import { UtilsTool } from '@/utils/macd/UtilsTool';
import { findIndex } from '@/utils/array';
import { getAdminTemplate, TemplateYanYv } from '@/utils/admin';

export function genRoleTableColumns(lists: [roleFields]): [roleFields] {
  for (let i = 0; i < lists.length; i++) {
    lists[i] = getRoleAttributes(lists[i]);
  }

  lists.sort((a, b) => {
    if (a.wear !== b.wear) return a.wear === true && b.wear === false ? -1 : 1;
    else if (a.order !== b.order) return a.order < b.order ? -1 : 1;
  });
  return lists;
}

export function getWearRoles(lists: [roleFields]): [roleFields] {
  for (let i = 0; i < lists.length; i++) {
    lists[i] = getRoleAttributes(lists[i]);
  }
  const wearRoles: [roleFields] = [];
  for (let i = 0; i < lists.length; i++) {
    if (lists[i].wear === true) {
      wearRoles.push(lists[i]);
    }
  }
  return wearRoles;
}

export function getRoleAttributes(fields: roleFields): roleFields {
  const data = UtilsTool.coverRoleInfo(fields.id);
  let wear = false;
  if (fields.wear || fields.order > 0) {
    // 兼容2022-12-29后的改动
    wear = true;
  }
  fields = {
    order: fields.order,
    id: fields.id,
    name: getRoleName(data.type),
    level: 'Lv.' + data.level,
    evolution: 'Lv.' + data.evolution,
    break: data.break === 1 ? '是' : '否',
    wear: wear,
    wearTag: wear ? '出战(' + fields.order + ')' : '休息',
  };
  return fields;
}

export function getRoleName(id: number): string {
  const index = findIndex(id, getAdminTemplate() === TemplateYanYv ? yanYvIdMaps : idMaps);
  if (index === false) {
    return `-`;
  }

  return getAdminTemplate() === TemplateYanYv ? yanYvIdMaps[index].label : idMaps[index].label;
}

export function genRoleOptions(id: number): any {
  console.log('id:' + id);
}
