import { getPrizeListFromChildOrSite } from '../common/gp-match/reward/child-reward';
import { MatchingStatus } from '../press-schedule-card-gp/utils';

import { BUTTON_MAP, ScheState } from './config';

const getJoinTypeDesc = (childInfo: Record<string, any>) => {
  const joinFunc = childInfo?.match_rule_cfg?.pubg_rule_cfg?.join_func;
  const joinTypeMap = { 1: '团队赛', 2: '个人赛' };
  const joinTypeDesc = joinTypeMap[joinFunc as 1] || '';
  return joinTypeDesc;
};

const getMatchDesc = (childInfo: Record<string, any>) => {
  const moduleCfg = childInfo.match_rule_cfg?.pubg_rule_cfg?.module_cfg || [];
  const newModuleCfg = moduleCfg.map((item: Record<string, any>) => {
    if (item.module_id === 1129) {
      return {
        ...item,
        module_name: '龙狮迎冰雪-海岛',
      };
    }
    return item;
  });

  const str = newModuleCfg[0]
    ?.module_name
    ?.split('-')
    .map((item: string) => item.trim())
    .join(' - ') || '';

  return str;
};


const baseButtonInfo = {
  buttonText: '',
  isButtonGray: false,
  isButtonPrimary: false,
};


const getButtonText = ({
  childInfo,
  cycleMatchInfo,
  isTeamMatch,
  isJoined,
  isFullTeam,
  canClaimPrizeList = [],
  isTeamLeader,
}: {
  childInfo: Record<string, any>;
  cycleMatchInfo: Record<string, any>;
  isTeamMatch: boolean;
  isJoined: boolean;
  isFullTeam: boolean;
  canClaimPrizeList?: Array<any>;
  isTeamLeader: boolean
}) => {
  const { act_start_time: actStartTime, act_end_time: actEndTime } = childInfo;
  const now = parseInt(`${Date.now() / 1000}`, 10);

  if (now > actEndTime) {
    if (isJoined) {
      if (canClaimPrizeList?.length) {
        return {
          buttonText: BUTTON_MAP.CLAIM_PRIZE,
          isButtonPrimary: true,
        };
      }
    }
    return {
      buttonText: BUTTON_MAP.GAME_END,
      isButtonGray: true,
    };
  }

  // TODO: to release
  if (now < actStartTime) {
    return {
      buttonText: BUTTON_MAP.WILL_START,
      isButtonGray: true,

      showStartMatchCountdown: true,
    };
  }


  const { matchingStatus, battleList } = cycleMatchInfo;
  console.log('battleList', battleList);
  if (matchingStatus === MatchingStatus.ON_GOING) {
    return {
      buttonText: BUTTON_MAP.MATCHING,
      isButtonGray: true,
    };
  }
  const lastBattle = battleList[battleList.length - 1];

  if (lastBattle && lastBattle.scheState === ScheState.END && matchingStatus !== MatchingStatus.ON_GOING
      && actEndTime && actEndTime > now
  ) {
    return isTeamMatch && !isTeamLeader ? {
      buttonText: BUTTON_MAP.WAIT_CAPTAIN_MATCHING,
      isButtonGray: true,
    } : {
      buttonText: BUTTON_MAP.START_MATCH,
      isButtonPrimary: true,
    };
  }

  if (matchingStatus === MatchingStatus.SUCCESS) {
    if (lastBattle?.can_enter_game) {
      return {
        buttonTip: '已匹配到对手，请尽快进入游戏',
        buttonText: BUTTON_MAP.ENTER_GAME,
        buttonCountdown: (lastBattle.enter_game_remain || 0) * 1000,
        isButtonPrimary: true,
      };
    }
    return {
      buttonTip: '等待本局结束后开始下一局匹配',
      buttonText: BUTTON_MAP.GAME_PLAYING,
      isButtonGray: true,
    };
  }

  console.log('isJoined', isJoined, childInfo);
  if (isTeamMatch) {
    if (!isJoined) {
      return {
        buttonText: BUTTON_MAP.CREATE_GAME_AND_SIGNUP,
        isButtonPrimary: true,
        showRecruitingTeams: true,
      };
    }
    if (!isFullTeam) {
      return {
        buttonTip: '队伍需要满足<span></span>人，否则无法参赛',
        buttonText: BUTTON_MAP.INVITE_OTHER,
        isButtonPrimary: true,

        showPublishRecruit: true,
      };
    }
    return !isTeamLeader ? {
      buttonText: BUTTON_MAP.WAIT_CAPTAIN_MATCHING,
      isButtonGray: true,
    } : {
      buttonText: BUTTON_MAP.START_MATCH,
      isButtonPrimary: true,
    };
  }

  return {
    buttonTip: '已开启匹配，快来加入比赛吧',
    buttonText: BUTTON_MAP.START_MATCH,
    isButtonPrimary: true,
  };
};


const checkShowChampion = ({
  isJoined,
  isEnd,
}: {
  isJoined: boolean;
  isEnd: boolean;
}) => isEnd && !isJoined;


export const checkIsEnd = (childInfo: Record<string, any>) => childInfo.act_end_time * 1000 < Date.now();


export function parseCycleChildInfo({
  childInfo,
  cycleMatchInfo,
  siteInfo,

  scoreInfo,
  championInfo,

  isTeamMatch,
  isJoined,
  isFullTeam,
  isEnd,
  canClaimPrizeList,
  isTeamLeader,
}: {
  childInfo: Record<string, any>;
  cycleMatchInfo: Record<string, any>;
  siteInfo: Record<string, any>;

  scoreInfo: Record<string, any>;
  championInfo: Record<string, any>;

  isTeamMatch: boolean;
  isJoined: boolean;
  isFullTeam: boolean;
  isEnd: boolean;
  canClaimPrizeList: Array<any>;
  isTeamLeader: boolean;
}) {
  const mapDesc = getMatchDesc(childInfo);
  const prizeList = getPrizeListFromChildOrSite(childInfo, siteInfo);
  const buttonInfo = getButtonText({
    childInfo,
    cycleMatchInfo,
    isTeamMatch,
    isJoined,
    isFullTeam,
    canClaimPrizeList,
    isTeamLeader,
  });

  console.log('prizeList', prizeList);


  const result = {
    ...childInfo,
    joinTypeDesc: getJoinTypeDesc(childInfo),
    mapDesc,
    prizeList,

    ...baseButtonInfo,
    ...buttonInfo,

    scoreInfo,

    championInfo,
    showChampion: checkShowChampion({
      isJoined,
      isEnd,
    }),
  };

  console.log('parsedChildInfo', result);
  return result;
}


