import { STATUS } from './../../../consts/statusCode';
import { HANG_UP_CONSTS, ITEM_CHANGE_REASON, REDIS_KEY } from './../../../consts';
import { TaskHero, TowerTaskRecModel, TowerTaskRecType } from './../../../db/TowerTaskRec';
import { HangUpSpdUpRecModel } from './../../../db/HangUpSpdUpRec';
import { HangUpRecordModel } from './../../../db/HangUpRecord';
import { RoleModel } from './../../../db/Role';
import { TowerRecordModel } from './../../../db/TowerRecord';
import { Application, BackendSession } from 'pinus';
import { resResult, genCode } from '../../../pubUtils/util';
import { calcuHangUpReward, checkHangUpSpdUpCnt, refreshTasks, treatTask, getRemainTime, getTowerStatus, getHungupRewards, getTasks, checkTaskRewards, getTowerTaskCostGold, getHangSpdUpCostGold, getManyHangSpdUpCostGold, getTaskStatus } from '../../../services/battleService';
import { addItems, handleCost } from '../../../services/rewardService';
import { checkBattleHeroes } from '../../../services/normalBattleService';
import { getGoldObject } from '../../../pubUtils/itemUtils';
import { gameData } from '../../../pubUtils/data';
import * as dicParam from '../../../pubUtils/dicParam';

export default function(app: Application) {
    return new TowerBattleHandler(app);
}

export class TowerBattleHandler {
    constructor(private app: Application) {
    }

    /**
     * 获取天梯当前挑战状态
     * @param session 
     */
    async getStatus(msg: {}, session: BackendSession) {
        let roleId = session.get('roleId');
        let serverId = session.get('serverId');
        let role = await RoleModel.findByRoleId(roleId);
        let data = await getTowerStatus(role);

        return resResult(STATUS.SUCCESS, data);
    }

    /**
     * 重置天梯当前层的挑战记录
     * @param towerLv 要重置的天梯层数
     * @param session 
     */
    async resetLv(msg: {towerLv: number}, session: BackendSession) {
        let roleId = session.get('roleId');
        let { towerLv } = await RoleModel.findByRoleId(roleId);
        if (msg.towerLv !== towerLv) {
            return resResult(STATUS.TOWER_RESET_ERR);
        }
        const record = await TowerRecordModel.resetRecordByLv(roleId, towerLv);
        if (!record) {
            return resResult(STATUS.TOWER_NOT_FOUND);
        }
        return resResult(STATUS.SUCCESS, record);
    }

    async checkHangUpRewards(msg: {}, session: BackendSession) {
        let roleId = session.get('roleId');
        let result = await getHungupRewards(roleId);
        if(result.status == -1) {
            return result.resResult;
        }
        return resResult(STATUS.SUCCESS, result.data);
    }

    async recHangUpRewards(msg: {}, session: BackendSession) {
        let roleId = session.get('roleId');
        let roleName = session.get('roleName');
        let sid = session.get('sid');
        const result = await calcuHangUpReward(roleId);
        if(result.status == -1) {
            return result.resResult
        }
        let { timeReward, endLv, endTime, deltaTime, needReceiveGoods } = result.data;
 
        const goods = await addItems(roleId, roleName, sid, timeReward, ITEM_CHANGE_REASON.TOWER_HUNG_UP_REWARD);
        await HangUpRecordModel.updateRec(roleId, roleName, endLv, endTime, needReceiveGoods);

        return resResult(STATUS.SUCCESS, { endTime, hangUpPassTime: Math.floor((deltaTime%HANG_UP_CONSTS.UNIT_TIME)/1000), goods });
    }

    async hangUpSpeedUp(msg: {speedUpCnt: number}, session: BackendSession) {
        let roleId = session.get('roleId');
        let roleName = session.get('roleName');
        let sid = session.get('sid');
        if (msg.speedUpCnt <= 0) {
            return resResult(STATUS.WRONG_PARMS);
        }
        const curTime = new Date();
        const calResult = await calcuHangUpReward(roleId, true, msg.speedUpCnt, curTime);
        if(calResult.status == -1) {
            return calResult.resResult;
        }
        let { timeReward, endLv, deltaTime, needReceiveGoods } = calResult.data;

        let result = await checkHangUpSpdUpCnt(roleId, msg.speedUpCnt, curTime);
        if(result.status == -1)  return result.resResult;
        let {data: {hangUpSpdUpCnt, gold}} = result;

        const costGold = getManyHangSpdUpCostGold(hangUpSpdUpCnt, msg.speedUpCnt);
        if(costGold > gold) {
            return resResult(STATUS.TOWER_GOLD_NOT_ENOUGH);
        }

        await handleCost(roleId, sid, [getGoldObject(costGold)], ITEM_CHANGE_REASON.TOWER_HUNG_UP_SDP_UP)

        const spdUpResult = await RoleModel.hangUpSpdUp(roleId, msg.speedUpCnt, curTime);
        if (!spdUpResult) {
            return resResult(STATUS.TOWER_HANG_UP_FAILED);
        }
        const spdUpRec = await HangUpSpdUpRecModel.updateRec(roleId, roleName, msg.speedUpCnt, endLv, needReceiveGoods);
        const goods = await addItems(roleId, roleName, sid, timeReward, ITEM_CHANGE_REASON.TOWER_HUNG_UP_REWARD);
        let nextCostGold = getManyHangSpdUpCostGold(spdUpResult.hangUpSpdUpCnt, 1);

        return resResult(STATUS.SUCCESS, { goods, hangUpSpdUpCnt: dicParam.TOWER_BOOST.TOWER_BOOSTTIME - spdUpResult.hangUpSpdUpCnt, nextCostGold, hangUpPassTime: Math.floor(deltaTime/1000), rewardLv: endLv, costGold });
    }

    async getTasks(msg: {}, session: BackendSession) {
        let roleId = session.get('roleId');
        let roleName = session.get('roleName');

        let role = await RoleModel.findByRoleId(roleId);
        let result = await getTasks(role);

        return resResult(STATUS.SUCCESS, result);
    }

    async refreshTasks(msg: {}, session: BackendSession) {
        let roleId = session.get('roleId');
        let roleName = session.get('roleName');
        let curTime = new Date();
        let { towerLv, towerTaskRefTime, towerTaskReCnt=0 } = await RoleModel.findByRoleId(roleId);
        if(towerTaskReCnt >= dicParam.TOWER_SEARCH.TOWER_SEARCH_REFRESHRULE) { // 付费刷新次数
            return resResult(STATUS.TOWER_REF_CNT_NOT_ENOUGH);
        }
        let costGold = getTowerTaskCostGold(towerTaskReCnt, towerTaskRefTime);
        let {gold} = await RoleModel.findByRoleId(roleId);
        if(costGold > gold) {
            return resResult(STATUS.TOWER_GOLD_NOT_ENOUGH);
        }
        
        // 只刷掉当前面板上没有做派遣的任务
        let batchCode = genCode(8);
        let curTasks = await TowerTaskRecModel.getCurTasks(roleId); // 当前显示中的任务
        for(let task of curTasks) {
            if(task) batchCode = task.batchCode;
        }
        curTasks = await refreshTasks(towerLv, batchCode, roleId, roleName, curTasks);
        let newRole = await RoleModel.increaseTowerRefCnt(roleId, 1);
        
        let refRemainTime = getRemainTime(curTime);
        let nextCostGold = getTowerTaskCostGold(newRole.towerTaskReCnt, newRole.towerTaskRefTime);
        return resResult(STATUS.SUCCESS, { curTasks: treatTask(curTasks, curTime), costGold,  nextCostGold, refRemainTime});
    }

    async sendTaskHero(msg: {batchCode: string, tasks: {taskCode: string, heroes: number[]}[]}, session: BackendSession) {
        let roleId = session.get('roleId');

        const curTime = new Date();
        let curTasks = await TowerTaskRecModel.getCurTasks(roleId); // 当前一批的任务
        if (!curTasks || curTasks.length == 0) {
            return resResult(STATUS.TOWER_TASK_NOT_FOUND);
        }
        let usedHeroes: number[] = [], tasks: {taskCode:string, heroes: TaskHero[], completeTime: number}[] = [];
        const tasksCode: string[] = [];
        const taskMap = new Map<string, any>()
        curTasks.forEach(task => {
            let getStatusResult = getTaskStatus(task.status, task.completeTime, curTime);
            if(getStatusResult.status == 1) {
                for(let { seqId } of task.heroes) {
                    usedHeroes.push(seqId);
                }
            }
            tasksCode.push(task.taskCode);
            taskMap.set(task.taskCode, task);
        });
        for (let { taskCode, heroes: seqIds } of msg.tasks) {
            let curTask = taskMap.get(taskCode); // 数据库中，这个任务的数据
            if(!curTask) {
                return resResult(STATUS.TOWER_TASK_MISSING)
            }
            let dicTask = gameData.towerTask.get(curTask.taskId);
            if (seqIds.length !== dicTask.actorNeeded) { // 武将数，从策划表中读取
                return resResult(STATUS.TOWER_TASK_MAX_HERO);
            }
            let { isOK, heroes } = await checkBattleHeroes(roleId, seqIds);
            if(!isOK) return resResult(STATUS.BATTLE_HERO_NOT_FOUND);
            if (tasksCode.indexOf(taskCode) === -1) {
                return resResult(STATUS.TOWER_TASK_CODE_NOT_FOUND);
            }
            if (usedHeroes.length > 0) { // 是否在其他任务重使用了武将
                let used = !!seqIds.find(seqId => usedHeroes.indexOf(seqId) !== -1);
                if (used) {
                    return resResult(STATUS.TOWER_TASK_HERO_HAS_USED);
                }
            }

            let taskHeroes = heroes.map(hero => {
                return new TaskHero(hero);
            });
            tasks.push({ taskCode, heroes: taskHeroes, completeTime: dicTask.completeTime})

        }
        const recs = await TowerTaskRecModel.sendHeroes(roleId, msg.batchCode, tasks, curTime,);
        if (!recs || recs.length === 0) {
            return resResult(STATUS.TOWER_TASK_SEND_ERR);
        }
        let role = await RoleModel.increaseTowerCnt(roleId, tasks.length)
        let refRemainTime = getRemainTime(curTime);

        curTasks = await TowerTaskRecModel.getCurTasks(roleId); // 当前显示中的任务
        let costGold = getTowerTaskCostGold(role.towerTaskReCnt, role.towerTaskRefTime);
        return resResult(STATUS.SUCCESS, { curTasks: treatTask(curTasks, curTime), refRemainTime, nextCostGold: costGold });
    }

    async settleTask(msg: {batchCode: string, taskCode: string}, session: BackendSession) {
        let roleId = session.get('roleId');
        let roleName = session.get('roleName');
        let sid = session.get('sid');

        const { batchCode, taskCode } = msg;
        let allFlag = !taskCode;

        const curTime = new Date();
        let tasks: TowerTaskRecType[] = [];
        if(allFlag) {
            tasks = await TowerTaskRecModel.getCurTasks(roleId);
        } else {
            let task = await TowerTaskRecModel.getTaskByCode(roleId, taskCode);
            if(task) tasks.push(task);
        }
        if (!tasks || tasks.length == 0) {
            return resResult(STATUS.TOWER_TASK_NOT_FOUND);
        }
        const checkResult = checkTaskRewards(batchCode, tasks);
        if (!checkResult) {
            return resResult(STATUS.TOWER_TASK_BATCH_NOT_FOUND);
        }
        const { compTasks, rewards } = checkResult;
        let goods = await addItems(roleId, roleName, sid, rewards, ITEM_CHANGE_REASON.TOWER_TASK_REWARD);
        await TowerTaskRecModel.finishTask(msg.batchCode, compTasks);
        let refRemainTime = getRemainTime(curTime);

        let curTasks = await TowerTaskRecModel.getCurTasks(roleId); // 当前显示中的任务
        let role = await RoleModel.findByRoleId(roleId);
        let costGold = getTowerTaskCostGold(role.towerTaskReCnt, role.towerTaskRefTime);
        return resResult(STATUS.SUCCESS, { curTasks: treatTask(curTasks, curTime), goods, refRemainTime, nextCostGold: costGold });
    }
}
