import { bodyMaps, equipmentFields, idMaps, starMaps, yanYvIdMaps } from '@/convert/equipment/map';
import { findIndex } from '@/utils/array';
import { gemFields, gemMaps } from '@/convert/gem/map';
import { getAdminTemplate, TemplateYanYv } from '@/utils/admin';

export function genEquipmentTableColumns(
  lists: [equipmentFields],
  gem: [gemFields],
  wearRoles: [roleFields]
): [equipmentFields] {
  const gemMap = new Map<Map<string, any>>();
  for (let i = 0; i < gem.length; i++) {
    if (gem[i].suits === null || gem[i].suits === undefined) {
      continue;
    }
    for (let j = 0; j < gem[i].suits.length; j++) {
      const innerMap = new Map();
      innerMap.set('role', j + 1);
      innerMap.set('lv', gem[i].level);
      innerMap.set('step', gem[i].step);
      innerMap.set('gems', gem[i].suits[j].slot);
      gemMap.set(gem[i].suits[j].equip, innerMap);
    }
  }
  for (let i = 0; i < lists.length; i++) {
    lists[i] = getAttributes(lists[i], gemMap, wearRoles);
  }

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

export function getAttributes(
  fields: equipmentFields,
  gemMap: Map<string, Map<string, any>>,
  wearRoles: [roleFields]
): equipmentFields {
  fields = {
    id: fields.id,
    equipId: fields.equipId,
    wear: gemMap.has(String(fields.id)), //fields.wear,
    name: getEquipmentName(fields.equipId),
    body: getEquipmentBody(fields.equipId),
    star: getEquipmentStar(fields.equipId),
    level: getEquipmentLevel(fields.equipId),
    roleName: '-',
    stepLevel: '0',
    gems: [],
  };
  if (fields.wear === true) {
    fields.level = 'Lv.' + gemMap.get(String(fields.id)).get('lv');
    if (gemMap.get(String(fields.id)).get('step') > 0) {
      fields.stepLevel = '+' + gemMap.get(String(fields.id)).get('step');
    }
    for (let i = 0; i < wearRoles.length; i++) {
      if (wearRoles[i].order === gemMap.get(String(fields.id)).get('role')) {
        fields.roleName = wearRoles[i].name;
        gemMap
          .get(String(fields.id))
          .get('gems')
          .forEach((id) => {
            if (id > 0) {
              fields.gems.push(gemMaps[id]);
            }
          });
      }
    }
  }

  return fields;
}

export function getEquipmentName(equipId: number): string {
  const idp = equipId.toString().slice(0, 3);

  const index = findIndex(idp, getAdminTemplate() === TemplateYanYv ? yanYvIdMaps : idMaps);
  if (index === false) {
    return `-`;
  }

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

export function getEquipmentBody(equipId: number): string {
  const idp = equipId.toString().slice(0, 1);

  const index = findIndex(idp, bodyMaps);
  if (index === false) {
    return `未知`;
  }

  return bodyMaps[index].label;
}

export function getEquipmentStar(equipId: number): string {
  const idp = equipId.toString().slice(3, 5);
  const index = findIndex(idp, starMaps);
  if (index === false) {
    return `-`;
  }

  return starMaps[index].label;
}

export function getEquipmentLevel(equipId: number): string {
  const idp = equipId.toString().slice(5, 8);
  return 'Lv.' + Number(idp);
}

export function getEquipId(id: string, equips: [equipmentFields]): number {
  for (let i = 0; i < equips.length; i++) {
    // @ts-ignore
    if (equips[i].id == id) {
      return equips[i].equipId;
    }
  }

  return 0;
}
