import { Framework } from "../../framework/Framework";
import { LoginMgr } from "../common/LoginManager";
import { BattleData, BattleLayoutData, BattleLayoutPos, BattleLayoutScale, BattleOptsBase, BattleOptsKill, BattleOptsMerge, BattleOptsMove, BattleOptsNew, BattleOptsRemove, BattleOptsType } from "../data/BattleData";
import { GameEvent } from "../data/GameEvent";
import { RoleData } from "../data/RoleData";
import { UserData } from "../data/UserData";
import { BattlesConf } from "../ui/tower/conf/BattlesConf";
import { GoodsManager } from "./GoodsManager";
import { RoleManager } from "./RoleManager";
//战斗消息管理
export class BattleManager {
    
    /**
     * 发送战斗结束消息
     * @param id 新关卡id, 
     * @param win 战斗结果(1-胜利,0-失败), 
     * @param layout 布局信息, [key:BattleLayoutPos]:number
     * @param opts 操作指令顺序数组
     * @param callback 
     */
    static sendBattleDuplicateMsg(id:number,win:boolean,layout:{[key:string]:number},opts:Array<BattleOptsBase>, callback,errorFun) {
        let args = {
            id:id, //新关卡id, 
            win:win?1:0, // 战斗结果(1-胜利,0-失败), 
            layout:layout, //布局信息, 
            opts:[], //操作指令顺序数组
        }


        for(let item of opts) {
            switch(item.type) {
                case BattleOptsType.New:{
                    let item_new = item as BattleOptsNew;
                    args.opts.push([item_new.type, item_new.pos,item_new.raceID,item_new.level]);
                    break;
                }
                case BattleOptsType.Move:{
                    let item_move = item as BattleOptsMove;
                    args.opts.push([item_move.type, item_move.srcPos,item_move.targetPos]);
                    break;
                }
                case BattleOptsType.Merge:{
                    let item_merge = item as BattleOptsMerge;
                    args.opts.push([item_merge.type, item_merge.srcPos,item_merge.targetPos]);
                    break;
                }
                case BattleOptsType.Remove:{
                    let item_remove = item as BattleOptsRemove;
                    args.opts.push([item_remove.type, item_remove.pos]);
                    break;
                }
                case BattleOptsType.Kill:{
                    let item_kill = item as BattleOptsKill;
                    args.opts.push([item_kill.type, item_kill.typeID, item_kill.count]);
                    break;
                }
                
            }
        }

        LoginMgr.sendPost('battle', 'duplicate', (data) => {
            console.log(data);
            if(data.duplicate){
                BattleData.duplicate = data.duplicate;
            }
            if(data.layout){
                BattleData.layout = data.layout;
            }
            if(data.roles?.newRoles){
                RoleManager.setData(data.roles.newRoles);
            }
            if(data.fight_role){
                RoleData.fightRole = data.fight_role
                Framework.event.fireEvent(GameEvent.FightRoleChange)
            }

            if(data.awards){

                let awards = Framework.unionManager.parseServerAwards(data.awards);
                Framework.unionManager.fireEventWithItem(awards)
                Framework.unionManager.showItemTips(awards);
            }

            UserData.food_timer = data.food_timer;
            UserData.status.food = data.food;

            callback&&callback();
        }, args,errorFun)
    }

    // 重置新章节
    static sendBattleDuplicateResetMsg(mapID:number) {
        let args = {
            id:mapID, //章节id, 
        }

        LoginMgr.sendPost('battle', 'duplicate_reset', (data) => {
            if(data.duplicate){
                BattleData.duplicate = data.duplicate;
            }
            if(data.layout){
                BattleData.layout = data.layout;
            }
            if(data.awards){

                let awards = Framework.unionManager.parseServerAwards(data.awards);
                Framework.unionManager.fireEventWithItem(awards)
                Framework.unionManager.showItemTips(awards);
            }
            Framework.event.fireEvent(GameEvent.BattleDuplicateReset,data);
        }, args)
    }

    //领取章节奖励
    static sendBattleDuplicateRewardMsg(mapID:number) {
        let args = {
            id:mapID, //章节id
        }

        LoginMgr.sendPost('battle', 'duplicate_reward', (data) => {
            console.log(data);
            if(data.duplicate_reward){
                BattleData.duplicateReward = data.duplicate_reward;
            }

            if(data.awards){

                let awards = Framework.unionManager.parseServerAwards(data.awards);
                Framework.unionManager.fireEventWithItem(awards)
                Framework.unionManager.showItemTips(awards);
            }
            Framework.event.fireEvent(GameEvent.BattleDuplicateReward,data);
        }, args)
    }

    //切换同种族角色
    static sendBattleDuplicateFightRoleMsg(typeID:number, callback) {
        let args = {
            rid:typeID, //角色id
        }

        LoginMgr.sendPost('battle', 'set_fight_role', (data) => {
            RoleData.fightRole = data.fight_role
            callback&&callback();
        }, args)
    }

    //根据布局数据获取类型ID
    static getDataWithLayoutValue(value:number):BattleLayoutData{

        let fightRole = RoleData.fightRole;
        let level = value%BattleLayoutScale;
		let raceID = (value-level)/BattleLayoutScale;
        let typeID = fightRole[raceID-1];

        
        return {typeID: typeID,raceID: raceID,level: level}
    }
    


    //获取可领奖的最大章节
    static getRewardChapter(){
        let chapter = BattleData.duplicate.chapter-1;
        let curProcess = BattleData.duplicate.max_process
        let battlesConf = BattlesConf.data[curProcess];
        if(battlesConf){
            if(curProcess == battlesConf.BossProcess){
                chapter++;
            }
        }
        
        return chapter;
    }

}
