import { Application, BackendSession, HandlerService, } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { STATUS, GACHA_ID, ITEM_CHANGE_REASON } from '../../../consts';
import { getPlayerNewHeroGachaData } from '../../../services/activity/newHeroGachaService';
import { RoleModel } from '../../../db/Role';
import { HeroModel } from '../../../db/Hero';
import { GachaPull } from '../../../services/activity/gachaService';
import { addItems, createHeroes, handleCost } from '../../../services/rewardService';
import { ActivityNewHeroGachaModel } from '../../../db/ActivityNewHeroGacha';
import { addReward, stringToRewardParam } from '../../../services/activity/giftPackageService';
import { RewardParam } from '../../../domain/activityField/rewardField';


export default function (app: Application) {
    new HandlerService(app, {});
    return new NewHeroGachaHandler(app);
}

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

    /************************新将擢迁, 新武将抽卡****************************/

    /**
    * @description 获取新武将抽卡活动数据
    * @param {{ activityId: number}} msg
    * @param {BackendSession} session
    * @memberof NewHeroGachaHandler
    */
    async getNewHeroGachaActivity(msg: { activityId: number }, session: BackendSession) {
        const { activityId } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');

        let playerData = await getPlayerNewHeroGachaData(activityId, serverId, roleId)

        if (!playerData) return resResult(STATUS.ACTIVITY_MISSING);

        return resResult(STATUS.SUCCESS, playerData);
    }

    /**
  * @description 选中武将
  * @param {{ activityId: number, hid:number}} msg
  * @param {BackendSession} session
  * @memberof NewHeroGachaHandler
  */
    async selectedHero(msg: { activityId: number, hid: number }, session: BackendSession) {
        const { activityId, hid } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');
        await ActivityNewHeroGachaModel.selectedHero(serverId, activityId, roleId, hid);

        return resResult(STATUS.SUCCESS);
    }

    /**
        * @description 抽卡
        * @param {{ activityId: number, hid: number, count: number}} msg
        * @param {BackendSession} session
        * @memberof NewHeroGachaHandler
        */
    async pull(msg: { activityId: number, hid: number, count: number }, session: BackendSession) {
        const { activityId, hid, count } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');
        const sid = session.get('sid');
        const roleName = session.get('roleName');
        

        let playerData = await getPlayerNewHeroGachaData(activityId, serverId, roleId)
        if (!playerData) return resResult(STATUS.ACTIVITY_MISSING);

        let item = playerData.findItem(hid);
        if (!item) {
            return resResult(STATUS.ACTIVITY_DATA_ERROR);
        }

        let { lv } = await RoleModel.findByRoleId(roleId);

        let userHeroes = await HeroModel.findByRole(roleId);

        let gachaPull = new GachaPull(GACHA_ID.TIMELIMIT, lv, userHeroes);
        gachaPull.setByActivity(item);
        let { items, heroInfo, resultList, activityData } = gachaPull.pull(count);
        let { hasGetFloor, floorCount } = gachaPull.getActivityParam();

        // 消耗东西
        let cost = item.cost.map(cur => { return { id: cur.id, count: cur.count * count } });
        let costResult = await handleCost(roleId, sid, cost, ITEM_CHANGE_REASON.NEW_HERO_GACHA_PULL);
        if (!costResult) return resResult(STATUS.ACTIVITY_RES_NOT_ENOUGH);

        //记录数据
        await ActivityNewHeroGachaModel.addRecord(serverId, activityId, roleId, hid, floorCount, JSON.stringify(resultList), hasGetFloor);

        // 给东西
        let { heroes, resultHeroes } = await createHeroes(roleId, roleName, sid, serverId, heroInfo);
        await addItems(roleId, roleName, sid, items, ITEM_CHANGE_REASON.GACHA_ITEMS);
        //固定奖励
        let rewardParamArr: Array<RewardParam> = stringToRewardParam(item.commonReward);
        let newRewardParamArr: Array<RewardParam> = rewardParamArr.map(obj => { return { id: obj.id, type: obj.type, count: obj.count * count } });
        let commonResult = await addReward(roleId, roleName, sid, serverId, newRewardParamArr, ITEM_CHANGE_REASON.NEW_HERO_GACHA_PULL)

        return resResult(STATUS.SUCCESS, {
            hid, activityId, count, randomReward: { heroes: resultHeroes, result: resultList }, commonReward: commonResult

        });
    }

}
