
import { dispatch } from '../pubUtils/dispatcher';
import { Session, Application } from 'pinus';

export function chat(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
    let chatServers = app.getServersByType('chat');

    if (!chatServers || chatServers.length === 0) {
        cb(new Error('can not find chat servers.'));
        return;
    }
    let id = session.get('chatServer')||dispatch(session.get('roleId'), chatServers, 'chat').id;

    cb(null, id);
}

export function battle(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
    let battleServers = app.getServersByType('battle');

    if (!battleServers || battleServers.length === 0) {
        cb(new Error('can not find battle servers.'));
        return;
    }

    let rid = session.get('roleId');
    let needDispatch = false;

    if (msg.args && msg.args.length > 0) {
        for (let arg of msg.args) {
            if (!arg.route) continue;
            if (['battle.comBattleHandler.createTeam', 'battle.comBattleHandler.searchTeam'].indexOf(arg.route) !== -1) {
                needDispatch = true;
                rid = Math.random().toString(36).slice(-8);
                session.set('teamCode', rid);
            } else if (arg.route.indexOf('battle.comBattleHandler') !== -1) {
                if (arg.body.teamCode) {
                    needDispatch = true;
                    rid = arg.body.teamCode;
                }
            }
        }
    }

    if(needDispatch) {
        let id = dispatch(rid, battleServers, 'battle').id;
        cb(null, id);
    } else {
        let id = session.get('battleServer')||dispatch(rid, battleServers, 'battle').id;
        cb(null, id);
    }
}


export function guild(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
    let guildServers = app.getServersByType('guild');

    if (!guildServers || guildServers.length === 0) {
        cb(new Error('can not find guild servers.'));
        return;
    }

    let rid = session.get('roleId');
    let needDispatch = false;

    if (msg.args && msg.args.length > 0) {
        for (let arg of msg.args) {
            if (!arg.route) continue;

            if (['guild.gateActivityHandler.getGateActivity',
                'guild.gateActivityHandler.checkBattle',      // 军团活动蛮夷入侵路由，按军团路由
                'guild.gateActivityHandler.action',
                'guild.gateActivityHandler.hitGate',
                'guild.gateActivityHandler.battleEnd',
                'guild.gateActivityHandler.debugGetPrivate',
                'guild.gateActivityHandler.debugStartActivity',
                'guild.gateActivityHandler.debugEndActivity'
            ].indexOf(arg.route) !== -1) {
                needDispatch = true;
                rid = session.get('guildCode');
            } else if (['guild.cityActivityHandler.getCityStatus',
                'guild.cityActivityHandler.declareCity',
                'guild.cityActivityHandler.checkBattle',
                'guild.cityActivityHandler.hitGate',
                'guild.cityActivityHandler.battleEnd'
            ].indexOf(arg.route) !== -1) {

                if (arg.body.cityId) {
                    needDispatch = true;
                    rid = arg.body.cityId.toString();
                }
            } else if (['guild.raceActivityHandler.getRaceActivity',
                'guild.raceActivityHandler.leavePage',
                'guild.raceActivityHandler.join',
                'guild.raceActivityHandler.getRace',
                'guild.raceActivityHandler.useItem',
                'guild.raceActivityHandler.battleEnd',
            ].indexOf(arg.route) !== -1) {
                needDispatch = true;
                rid = session.get('serverId').toString();
            } else if (['guild.gvgBattleHandler.startMove',
                'guild.gvgBattleHandler.stopMove',
                'guild.gvgBattleHandler.teamSettle',
                'guild.gvgBattleHandler.teamLeave',
                'guild.gvgBattleHandler.battleEnd',
                'guild.gvgBattleHandler.battleStart',
                'guild.gvgBattleHandler.getAreaTeams',
                'guild.gvgBattleHandler.useItem',
                'guild.gvgBattleHandler.reviveTeam',
                'guild.gvgBattleHandler.saveTeam',
                'guild.gvgBattleHandler.getCity',
                'guild.gvgBattleHandler.checkMyTeam',
                'guild.gvgBattleHandler.enterCity',
                'guild.gvgBattleHandler.leaveCity',
                'guild.gvgBattleHandler.getAreaOfMyTeam',
                'guild.gvgBattleHandler.debugAddRobots',
                'guild.gvgBattleHandler.debugMoveRobots',
                'guild.gvgBattleHandler.debugStopMoveRobot',
                'guild.gvgBattleHandler.debugAttacks',
            ].indexOf(arg.route) !== -1) {
                if (arg.body.cityId) {
                    needDispatch = true;
                    rid = arg.body.cityId.toString();
                }
            }
        }
    }

    if(needDispatch) {
        let id = dispatch(rid, guildServers, 'guild').id;
        cb(null, id);
    } else {
        let id = session.get('guildServer')||dispatch(rid, guildServers, 'guild').id;
        cb(null, id);
    }
}

export function activity(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
    let activityServers = app.getServersByType('activity');
    if (!activityServers || activityServers.length === 0) {
        cb(new Error('can not find activity servers.'));
        return;
    }
    let id = session.get('activityServer')||dispatch(session.get('roleId'), activityServers, 'activity');
    cb(null, id);
}

export function gm(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
    let gmServers = app.getServersByType('gm');

    if (!gmServers || gmServers.length === 0) {
        cb(new Error('can not find gm servers.'));
        return;
    }

    let res = dispatch(session.get('roleId'), gmServers, 'gm');
    cb(null, res.id);
}

export function role(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
    let roleServers = app.getServersByType('role');

    if (!roleServers || roleServers.length === 0) {
        cb(new Error('can not find activity servers.'));
        return;
    }

    let id = session.get('roleServer')||dispatch(session.get('roleId'), roleServers).id;
    cb(null, id);
}

export function order(session: Session, msg: any, app: Application, cb: (err: Error, serverId?: string) => void) {
    let orderServers = app.getServersByType('order');

    if (!orderServers || orderServers.length === 0) {
        cb(new Error('can not find activity servers.'));
        return;
    }

    let id = session.get('orderServer')||dispatch(session.get('roleId'), orderServers, 'order').id;
    cb(null, id);
}