const NUMBER_CHI_MAP = {
  1: '一',
  2: '二',
  3: '三',
  4: '四',
  5: '五',
  6: '六',
  7: '七',
};

export function getStartBattleTypeDesc(
  startBattleType: number,
  orgRoundTimeCfg: Array<{
    stime: number
  }>,
  timeStampFormat: Function,
) {
  if (startBattleType == 2) {
    return '手动开赛';
  }
  return `定时开赛(首局${timeStampFormat(orgRoundTimeCfg?.[0]?.stime, 'M月dd日 hh:mm')})`;
}

export function getPureMapName(name = '') {
  const list = name.split('-');

  return list[list.length - 1].trim();
}

export function getMatchModeDesc(moduleCfg: Array<{
  module_name: string;
}> = []) {
  const mapStr = moduleCfg
    .map(item => getPureMapName(item.module_name))
    .join(', ');
  return `共${NUMBER_CHI_MAP[moduleCfg.length as keyof typeof NUMBER_CHI_MAP]}局 (${mapStr})`;
}


export function getMatchMapDesc(moduleCfg: Array<{
  module_name: string;
}> = []) {
  return getPureMapName(moduleCfg[0]?.module_name || '');
}

export function getJoinFuncDesc(value: number) {
  return value === 2 ? '个人赛（单人)' : '团队赛（四人)';
}

export function getMinSignupNumDesc(minSignNum = 1) {
  return `${NUMBER_CHI_MAP[minSignNum as 1]}人`;
}

export function getQRCodeFromMatch(match: {
  contacts_info: string;
}) {
  const { contacts_info: contract = '' } = match;
  let res: {
    chat_group_code?: string;
    updateTime?: number;
  } = {};
  try {
    res = JSON.parse(decodeURIComponent(contract));
  } catch (err) {}


  return {
    qRCode: res?.chat_group_code || '',
    updateTime: res?.updateTime || '',
  };
}


const getMapOptionHtml = (name = '', tag = '') => {
  const html = `<div class="picker-item--match" style="position: relative;width: 100%;height: 100%;display:flex; align-items:center;justify-content: center;">${name}
<span class="picker-item-tag--match" style="position: absolute; top: 0; left: calc(50% + 60px);color: #fff;font-size: 10px;background: rgba(83, 0, 195, 0.75);width: 26px; height: 13px;line-height: 13px;">${tag}</span>
</div>`;

  if (tag) {
    return {
      html,
    };
  }
  return {
    text: name,
  };
};

export function parseNewMap(list: Array<any>) {
  const allMap = list
    .filter(item => item.group_max_team > 0)
    .map((item) => {
      const nameList = item.model_name.replace(/\s/g, '').split('-');
      return {
        nameList,
        ...item,
      };
    })
    .reduce((acc, item) => {
      const [mode, name] = item.nameList;
      const info = {
        ...item,
        code: item.model_id,
        mode,
        ...getMapOptionHtml(`${name}(推荐${item.group_auto_team}队)`, item.common_use ? '常用' : ''),
      };
      if (acc[mode]) {
        acc[mode].push(info);
      } else {
        acc[mode] = [info];
      }
      return acc;
    }, {});

  return allMap;
}

export function getActStartTime(joinEnd: number, startBattleType: string | number) {
  const tempActStartTime = joinEnd + (10 * 60);
  // 自动开赛，活动开始时间为 报名结束 + 10 分钟
  // 手动开赛，活动开始时间为 报名结束
  const actStartTime =  startBattleType == 1 ? tempActStartTime : joinEnd;

  return actStartTime;
}


export function genRoundTimeCfg({
  boType,
  roundTimeCfg = [],
  joinEnd,
  fixByActStartTime = false,
  startBattleType = 1,
}: {
  boType: number,
  roundTimeCfg: Array<any>,
  joinEnd: number,
  fixByActStartTime?: boolean,
  startBattleType?: number | string
}) {
  const result: Array<{
    round_id: number;
    stime: number
  }> = [];
  const oneHour = 60 * 60;
  const actStartTime =  getActStartTime(joinEnd, startBattleType);


  for (let i = 0; i < boType; i++) {
    const exist = roundTimeCfg[i];
    const preCfg = result[i - 1];

    let time = exist?.stime;

    // 轮次时间不能小于开赛时间
    if (!time || (fixByActStartTime && time < actStartTime)) {
      time = actStartTime + i * oneHour;
    }

    // 轮次时间至少间隔1小时
    if (preCfg && time < preCfg.stime + 1 * 60 * 60) {
      time = preCfg.stime + 1 * 60 * 60;
    }

    result.push({
      round_id: i + 1,
      stime: time,
    });
  }

  return result;
}


export function parseAwards(generalRewardDesc = '') {
  let list = [];
  try {
    list = JSON.parse(decodeURIComponent(generalRewardDesc || '[]'));
  } catch (err) {}

  return list;
}

export function stringifyAwards(list = []) {
  return encodeURIComponent(JSON.stringify(list));
}

export function parseQRCodeUpdateTime(updateTime: number, timeStampFormat: Function) {
  return timeStampFormat(updateTime || '', 'M月d日 hh:mm');
}
