import { Application, ChannelService, FrontendSession, RemoterClass, HandlerService, pinus, } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { STATUS } from '../../../consts';
import { handleComBtlProgress } from '../../../services/comBattleService';
import { MemComBtlTeam } from '../../../domain/battleField/ComBattleTeamField';

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

export class ComBattleRemote {
    bossHp = 10000;

    constructor(private app: Application) {
        this.app = app;
        this.channelService = app.get('channelService');
    }

    private channelService: ChannelService;

    public async create(uid: string, sid: string, name: string, flag: boolean) {
        console.log('comBattleRemote create: ', name, flag);
        let channel = this.channelService.getChannel(name, flag);
        if (!!channel) {
            let username = uid.split('*')[0];
            let param = {
                user: username
            };
            channel.pushMessage('onAdd', resResult(STATUS.SUCCESS, param));

            if (!!channel) {
                channel.add(uid, sid);
            }
        }
        return this.get(name, flag);
    }

    /**
     * Add user into chat channel.
     *
     * @param {String} uid unique id for user
     * @param {String} sid server id
     * @param {String} name channel name
     * @param {boolean} flag channel parameter
     *
     */
    public async add(uid: string, sid: string, name: string, flag: boolean) {
        console.log('comBattleRemote add: ', name, flag);
        let channel = this.channelService.getChannel(name, flag);
        if (!!channel && !this.get(name, false).includes(uid)) {
            let username = uid.split('*')[0];
            let param = {
                user: username
            };
            channel.pushMessage('onAdd', resResult(STATUS.SUCCESS, param));

            if (!!channel) {
                channel.add(uid, sid);
            }
        }
        return this.get(name, flag);
    }

    public async available(uid: string, sid: string, name: string, flag: boolean) {
        let channel = this.channelService.getChannel(name, flag);
        if (!!channel) {
            const users = this.get(name, false);
            if (users.includes(uid)) {
                console.log('不得重复加入');
                return false;
            }
        }
        return true;
    }

    /**
     * Get user from chat channel.
     *
     * @param {Object} opts parameters for request
     * @param {String} name channel name
     * @param {boolean} flag channel parameter
     * @return {Array} users uids in channel
     *
     */
    private get(name: string, flag: boolean) {
        let users: string[] = [];
        let channel = this.channelService.getChannel(name, flag);
        if (!!channel) {
            users = channel.getMembers();
        }
        for (let i = 0; i < users.length; i++) {
            users[i] = users[i].split('*')[0];
        }
        return users;
    }

    /**
     * Kick user out chat channel.
     *
     * @param {String} uid unique id for user
     * @param {String} sid server id
     * @param {String} name channel name
     *
     */
    public async kick(uid: string, sid: string, name: string) {
        let channel = this.channelService.getChannel(name, false);
        // leave channel
        if (!!channel) {
            channel.leave(uid, sid);
        }
        let username = uid.split('*')[0];
        let param = {
            user: username
        };
        channel.pushMessage('onLeave', resResult(STATUS.SUCCESS, param));
    }

    public async hurt(uid: string, sid: string, name: string, bossHurt: number, actorHurt: [{ actorId: number, actorHurt: number }]) {
        console.log('hurt channel name: ', name);
        let channelService = this.app.get('channelService');
        this.bossHp -= bossHurt;
        let channel = channelService.getChannel(name, false);
        if (!!channel) {
            if (this.bossHp < 0) {
                this.bossHp = 0;
            }
            channel.pushMessage('bossHp', resResult(STATUS.SUCCESS, { bossHp: this.bossHp }));
        }
        return this.bossHp;
    }

    public async leaveFromBattle(roleId: string, teamCode: string ) {
        let teamMap: Map<string, MemComBtlTeam> = this.app.get('teamMap');
        if(teamMap && teamMap.has(teamCode)) {
            let myTeam = teamMap.get(teamCode);
            for(let roleStatus of myTeam.roleStatus) {
                if(roleStatus.roleId == roleId) {
                    roleStatus.battleStatus = 2; // 自己失败了
                }
            }
            let channelService = this.app.get('channelService');
            let channel = channelService.getChannel(teamCode, false);    
            
            await handleComBtlProgress(myTeam, this.app.get('robotHurtTimer'), teamMap, channel); // 结算
        }
    }
}