import { timeStampFormat } from 't-comm/es/time/time';

import {
  CARD_STATUS_MAP,

  // 淘汰赛
  COMPONENT_STATUS_TEXT_MAP,
  COMPONENT_STATUS_CLASS_MAP,
  COMPONENT_STATUS_MAP,
  SPECIAL_TEAM_AVATAR,
} from '../../press-schedule-card-gp/utils';

import { getKnockoutCardStatusCode } from './knockout-status';
import { getCardTeams } from './knockout-team';

/**
 * 解析和平赛事淘汰赛赛程卡片
 */
export function parseKnockoutScheCard({
  card = {},
  childInfo = {},
}: {
  card?: Record<string, any>;
  childInfo?: Record<string, any>;
}) {
  const {
    userJoinStatus = 0,
    gameDispStatus: gameDisplayStatus = 0,
    teamInfo = {},
    childInfo: { act_start_time: actStartTIme = 0,  match_rule_cfg: matchRuleCfg = {} } = {},
  } = childInfo;
  const {
    sign_team_min_mem: minSignNum = 1,
  } = matchRuleCfg;
  const isManualStart = matchRuleCfg.start_battle_type == 2;


  const {
    team_a: iTeamA = {},
    team_b: iTeamB = {},
    card_state: cardState = {},
    stime,
    countdown,
    child_id: childId,
  } = card;


  const { teamA, teamB } = getCardTeams(card);

  const boNum = card.bo_type || 1;
  const curBo = card.cur_bo || 1;

  const statusCode = getKnockoutCardStatusCode(cardState);
  const statusText = COMPONENT_STATUS_TEXT_MAP[statusCode];
  const statusClass = COMPONENT_STATUS_CLASS_MAP[statusCode];
  const statusDesc = getStatusDesc({
    isManualStart,
    statusCode,
    boNum,
    curBo,
    stime,
  });


  let myTeam: Partial<typeof teamA> = {};
  let opponentTeam: Partial<typeof teamA> = {};
  const isMyTeamInTeamA = iTeamA.my_team == 1;
  const isMyTeamInTeamB =  iTeamB.my_team == 1;
  if (isMyTeamInTeamA) {
    myTeam = teamA;
    opponentTeam = teamB;
  } else if (isMyTeamInTeamB) {
    myTeam = teamB;
    opponentTeam = teamA;
  }

  if (!Object.keys(card).length) {
    opponentTeam = teamB;
  }

  const gameRank = myTeam.rank || '-';
  const battleList = Array.from({ length: card.bo_type || 1 }).map((item: any, index: number) => {
    const res = {
      title: `第${index + 1}场`,
      timeDesc: '',
    };

    return res;
  });

  return {
    teamA,
    teamB,

    minSignNum,
    myTeam,

    // memberList: myTeam.memberList,
    // teamName: myTeam.name,
    // teamAvatar: myTeam.avatar,
    memberList: teamInfo.members?.map((item: any) => ({
      avatar: item.head ? item.head.replace('http://thirdqq', 'https://thirdqq') : SPECIAL_TEAM_AVATAR,
      name: item.nick,
    })),
    teamName: teamInfo.teamname,
    teamAvatar: teamInfo.teamavatar,

    gameStartCountdown: Math.max(0, actStartTIme * 1000 - Date.now()),

    opponentInfo: {
      ... opponentTeam,
      boNum,
      curBo,

      statusCode,
      statusText,
      statusClass,
      statusDesc,

      isMyTeamInTeamA,
      isMyTeamInTeamB,

      isTeamASpecial: teamA.isSpecial,
      isTeamBSpecial: teamB.isSpecial,

      showScore: statusCode !== COMPONENT_STATUS_MAP.NOT_START,
      teamAScore: teamA.score,
      teamBScore: teamB.score,

      teamAId: teamA.teamId,
      teamBId: teamB.teamId,

      teamAName: teamA.name,
      teamBName: teamB.name,

      teamAAvatar: teamA.avatar,
      teamBAvatar: teamB.avatar,

      // 是否显示进房按钮
      showLaunchGameButton: statusCode === COMPONENT_STATUS_MAP.ING && countdown,
      // 是否显示比赛进行中按钮
      showGamePlayingButton: statusCode === COMPONENT_STATUS_MAP.ING && !countdown,
    },
    cardState: getKnockoutCardState({
      gameDisplayStatus,
      userJoinStatus,
    }),
    gameRank,
    battleList,
    curBattleInfo: {
      room_id: cardState.room_id,
      room_pwd: cardState.room_pwd,
    },
    enterGameCountdown: countdown + Math.round(Date.now() / 1000),
    curGoingBattleIndex: curBo - 1,
    childId,
  };
}


function getKnockoutCardState({
  gameDisplayStatus = 0, // 1报名中 2待比赛 3比赛中 4已结束
  userJoinStatus = 0, // 1、用户已报名
}: {
  curJoinStatus?: number;
  cardState?: Record<string, any>;
  gameDisplayStatus?: number;
  userJoinStatus?: number;
}) {
  if (userJoinStatus != 1) {
    return CARD_STATUS_MAP.NOT_SIGN_UP;
  }

  if (gameDisplayStatus == 4) {
    return CARD_STATUS_MAP.KNOCK_OUT_RESULT;
  }

  if (gameDisplayStatus == 1 && userJoinStatus == 1) {
    return CARD_STATUS_MAP.KNOCK_OUT_SIGNED_UP;
  }

  return CARD_STATUS_MAP.KNOCK_OUT_OPPONENT;
}


function getStatusDesc({
  statusCode, isManualStart, stime, curBo, boNum,
}: Record<string, any>) {
  if (statusCode === COMPONENT_STATUS_MAP.END) {
    return ' ';
  }
  if (statusCode === COMPONENT_STATUS_MAP.ING) {
    return `当前${curBo}/${boNum}局`;
  }
  if (statusCode === COMPONENT_STATUS_MAP.NOT_START) {
    if (isManualStart) {
      return '等待管理员开赛';
    }
    return `${timeStampFormat(stime, 'M/d hh:mm')}开赛`;
  }
}
