import { COM_BTL_CONST, COM_TEAM_STATUS } from '../../consts';
import { gameData, getBossHpByBlueprtId, getDicBlueprtById } from '../../pubUtils/data';
import ComBattleTeam from './../../db/ComBattleTeam';
import { cloneDeep } from 'lodash'
import { FriendParams } from '../roleField/friend';
import { nowSeconds } from '../../pubUtils/timeUtil';
import moment = require('moment');

/**
 * 拷贝敌军数组并添加当前血量字段
 * @param source 原敌军数组，不包含当前血量字段
 */
 export function transBossHpArr(source: Array<{ dataId: number, hp: number, actorId: number }>): Array<{ dataId: number, hp: number, curHp: number, actorId: number }> {
    let desArr = [];
    source.forEach(elem => {
        let { hp } = elem;
        desArr.push(Object.assign(elem, { curHp: hp }));
    });
    return cloneDeep(desArr);
}


export class MemComBtlTeam extends ComBattleTeam  { 
    bossCurHp: number; 
    curRnd: number; 
    bossHp: number;
    sid: string;

    constructor(teamCode: string, pub: boolean, blueprtId: number, capId: string, ceLimit: number, sid: string) {
        super();
        const { lv } = getDicBlueprtById(blueprtId);
        this.lv = lv;
        const { bossHpSum, bossHpArr } = getBossHpByBlueprtId(blueprtId);
        this.bossHpArr = transBossHpArr(bossHpArr);
        this.teamCode = teamCode;
        this.pub = pub;
        this.blueprtId = blueprtId;
        this.status = COM_TEAM_STATUS.DEFAULT;
        this.capId = capId;
        this.ceLimit = ceLimit;
        this.curRnd = 0;
        this.roleCnt = 1;
        this.timeout = false;
        this.bossCurHp = bossHpSum;
        this.bossHp = bossHpSum;
        this.blacklist = [];
        this.sid = sid;
    }

    findMemberByRoleId(roleId: string) {
        return this.roleStatus.find(cur => cur.roleId == roleId);
    }

    startTeam() {
        this.status = COM_TEAM_STATUS.FIGHTING;
        this.startTime = nowSeconds();
        this.endTime = nowSeconds() + COM_BTL_CONST.BTL_TIME_LMT/1000;
        this.hasTimeExtraReward = isComBattleTimeLimit();
    }
 };


 export class ComBattleInvitation {
    teamCode: string;
    blueprtId: number;
    leader: FriendParams;
    roleCnt: number;
    status: number;

    constructor(comBattle: ComBattleTeam, leader: FriendParams) {
        this.teamCode = comBattle.teamCode;
        this.blueprtId = comBattle.blueprtId;
        this.leader = leader;
        this.roleCnt = comBattle.roleCnt;
        this.status = comBattle.status;
    }
 }

 
/**
 * 当前是否是有加成的特殊时间
 */
export function isComBattleTimeLimit() {
    let arr = gameData.comBattleRewardTime;
    for(let { from, to } of arr) {
        let fromTime = moment(moment().format(`YYYY-MM-DD ${from}`)).valueOf();
        let toTime = moment(moment().format(`YYYY-MM-DD ${to}`)).valueOf();
        let now = moment().valueOf();
        if(fromTime <= now && toTime >= now) return true;
    }
    return false;
}