import { STATUS } from './../../../consts/statusCode';
import { RoleModel, RoleType } from './../../../db/Role';
import { UserModel, UserType } from '../../../db/User';
import { GMUserModel } from '../../../db/GMUser';
import { Application, HandlerService, pinus, } from 'pinus';
import { FrontendSession } from 'pinus';
import { HeroModel } from './../../../db/Hero';
import { genCode, generateStr, resResult } from '../../../pubUtils/util';
import { COM_BTL_QUALITY, HERO_SELECT, DEBUG_MAGIC_WORD, REDIS_KEY, TASK_TYPE, ENTERY_ROLE_PICK, COUNTER, DEFAULT_LV, TA_USERSET_TYPE, LOG_TYPE, JEWEL_SELECT, ITEM_SELECT, SKIN_SELECT, PUSH_ROUTE } from '../../../consts';
// import { loginRefresh } from '../../../services/playerEventService';
import { nowSeconds, getZeroPoint } from '../../../pubUtils/timeUtil';
import { rmRoleFromQueue, roleLeave, getRoleOnlineInfo, roleLogin, getOnlineRoleByUserCode } from '../../../services/redisService';

import { addRoleToGuildChannel, addRoleToSysChannel, addRoleToWorldChannel, leaveGroupShopChannel, leaveGuildAuctionChannel, leaveGuildChannel, leaveGVGAreaChannel, leaveGVGAreaTeamChannel, leaveGVGCityTeamChannel, leaveSysChannel, leaveWorldAuctionChannel, leaveWorldChannel } from '../../../services/chatService';
import { reportOneOnline, savePlayTime } from '../../../services/authenticateService';
import { checkTaskInEntry, } from '../../../services/task/taskService';
import { pushData, kickUser, getModuleData, assignServer, leaveServer } from '../../../services/connectorService';
// import { setComBtlOnUserLeave } from '../../../services/comBattleService';
import Counter from '../../../db/Counter';
import { getExpByLv } from '../../../pubUtils/data';
import { reportCreateRoleEventToTa, reportTAEvent, reportTAUserSet } from '../../../services/sdkService';
import { saveLoginAndOutLog } from '../../../pubUtils/logUtil';
import { sendMessageToAllWithSuc, sendMessageToUserWithSuc } from '../../../services/pushService';
import { checkServerIsOpen, getIpLocation } from '../../../services/roleService';
import { dispatch } from '../../../pubUtils/dispatcher';
import { leaveRaceActivityToRemote } from '../../../services/guildActivity/guildActivityService';
import { getHiddenData } from '../../../services/dataService';
import { leaveCity } from '../../../services/gvg/gvgBattleService';

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

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

    /**
     * New client entry chat server.
     *
     * @param  {Object}   msg     request message
     * @param  {Object}   session current session object
     */
    async enter(msg: { token: string, serverId: number }, session: FrontendSession) {
        // console.log('****** entry before', Date.now());
        let self = this;
        let serverId = msg.serverId;

        let user = await UserModel.findUserByToken(msg.token);
        if (!user) {
            console.log('user token not found');
            return resResult(STATUS.TOKEN_ERR);
        }
        let onlineRoleId = await getOnlineRoleByUserCode(user.userCode);

        if (!!onlineRoleId) { // 多地登陆踢下线
            let connect = await getRoleOnlineInfo(onlineRoleId);
            if (connect.sid = self.app.getServerId()) {
                await kickUser(self.app, onlineRoleId);
            } else {
                await self.app.rpc.connector.connectorRemote.remoteLogin.toServer(connect.sid, onlineRoleId);
            }
        }

        let ip = this.getIp(session);
        if(!await checkServerIsOpen(serverId, ip, user.uid)) {
            return resResult(STATUS.SERVER_IS_NOT_OPEN)
        }


        let ipLocation = await getIpLocation(ip);
        let role = await RoleModel.findByUidAndSetTime(user.uid, serverId, ip, ipLocation, null, true);
        if (!role) {
            return resResult(STATUS.ROLE_NOT_FOUND);
        }
        
        let serverName = this.app.getServerId();
        await roleLogin(role.roleId, user.userCode, serverName, role.createTime, role.serverId, role.lv, role.topLineupCe); // 保存在线用户
        await this.addSession(user, role, session);

        await reportCreateRoleEventToTa(role, ip);
        saveLoginAndOutLog(LOG_TYPE.LOGIN, session);
        addRoleToSysChannel(role.roleId, self.app.get('serverId'), role.serverId);
        addRoleToWorldChannel(role.roleId, self.app.get('serverId'), role.serverId);
        reportOneOnline(role.roleId, user.userCode, self.app.get('serverId'), true, user);

        // 任务
        checkTaskInEntry(serverId, role.roleId, self.app.get('serverId'), role);
        if (role.hasGuild) {
            addRoleToGuildChannel(role.roleId, self.app.get('serverId'), role.guildCode);
        }

        let todayZeroPoint = getZeroPoint();

        // console.log('****** entry after', Date.now());
        return resResult(STATUS.SUCCESS, { roleId: role.roleId, todayZeroPoint });
    }

    async getData(msg: {  }, session: FrontendSession) {
        let ip = session.get('ip');
        let roleId = session.get('roleId');
        let role = await RoleModel.findByRoleId(roleId, '-_id', true, true);

        // 推送数据
        pushData(role.hasInit, role, session);

        let todayZeroPoint = getZeroPoint();
        return resResult(STATUS.SUCCESS, { todayZeroPoint });
    }

    private getIp(session: FrontendSession) {
        return session.remoteAddress.ip.replace('::ffff:', '');
    }

    async addSession(user: UserType, role: RoleType, session: FrontendSession) {
        const self = this;
        let ip = this.getIp(session);

        await session.abind(role.roleId);
        session.set('userid', role.userInfo.uid);
        session.set('roleId', role.roleId);
        session.set('loginTime', nowSeconds());
        session.set('roleName', role.roleName);
        session.set('eventStatus', role.eventStatus);
        session.set('blockType', role.blockType);
        session.set('sid', self.app.get('serverId'));
        session.set('serverId', role.serverId);
        session.set('guildCode', role.guildCode);
        session.set('ip', ip);
        session.set('vipStartTime', role.vipStartTime||0);
        session.set('channel', user.channelInfo?.platformAppid||'pc');
        session.push('userid', () => { });
        session.push('sid', () => { });
        session.push('roleId', () => { });
        session.push('roleName', () => { });
        session.push('eventStatus', () => { });
        session.push('serverId', () => { });
        session.push('loginTime', () => { });
        session.push('guildCode', () => { });
        session.push('blockType', () => { });
        session.push('ip', () => { });
        session.push('vipStartTime', () => { });
        session.push('channel', () => { });
        assignServer(role.roleId, session);

        console.log('#####', role.serverId, role.guildCode)
        // console.log(role.guildCode)
        // session.push('rid', function (err) {
        //     if (err) {
        //         console.error('set rid for session service failed! error is : %j', err.stack);
        //     }
        // });
        session.on('closed', this.onUserLeave.bind(this));
    }


    /**
     * 每天5点时的刷新数据.
     *
     * @param  {Object}   msg     request message
     * @param  {Object}   session current session object
     */
    async refresh(msg: {}, session: FrontendSession) {
        let self = this;
        let roleId = session.get('roleId');

        let role = await RoleModel.findByRoleIdAndSetTime(roleId, '-_id', true);
        if (!role) {
            return resResult(STATUS.ROLE_NOT_FOUND);
        }

        let { serverId } = role;
        // 任务
        checkTaskInEntry(serverId, role.roleId, self.app.get('serverId'), role);
        // 推送数据
        pushData(role.hasInit, role, session, 'refresh');

        let todayZeroPoint = getZeroPoint();
        return resResult(STATUS.SUCCESS, { todayZeroPoint });
    }

    async getHiddenData(msg: { }, session: FrontendSession) {
        let data = getHiddenData();
        return resResult(STATUS.SUCCESS, data);
    }

    /**
     * User log out handler
     *
     * @param {Object} app current application
     * @param {Object} session current session object
     *
     */
    async onUserLeave(session: FrontendSession, reason: string) {
        if (!session || !session.uid) {
            return;
        }

        let roleId = session.get('roleId');
        let sid = session.get('sid');
        let serverId = session.get('serverId');
        let loginTime = session.get('loginTime');
        const guildCode = session.get('guildCode');
        const teamCode: string = session.get('teamCode');
        roleLeave(roleId).then(function (roleInfo) {
            if (roleInfo.isOnline) {
                savePlayTime(roleInfo.userCode);
            }
        });
        leaveRaceActivityToRemote(serverId, guildCode, roleId);
        reportTAUserSet(TA_USERSET_TYPE.ADD, roleId, { total_play_time: nowSeconds() - loginTime });
        rmRoleFromQueue(roleId, sid); // 删除redis中寻宝的匹配记录
        let channelService = this.app.get('channelService');
        let channel = channelService.getChannel(roleId, true);
        channel.leave(roleId, sid);
        try {
            await leaveSysChannel(roleId, sid, serverId);
            await leaveWorldChannel(roleId, sid, serverId);
            await leaveGuildChannel(roleId, sid, guildCode);
            await leaveGuildAuctionChannel(roleId, sid, guildCode);
            await leaveWorldAuctionChannel(roleId, sid, serverId);
            await leaveGroupShopChannel(roleId, sid);
            await leaveGVGAreaChannel(roleId, sid);
            await leaveGVGAreaTeamChannel(roleId, sid);
            await leaveGVGCityTeamChannel(roleId, sid);
            await pinus.app.rpc.guild?.guildRemote.leaveCity.broadcast(true, roleId, serverId, guildCode);
        } catch(e) {
            console.error(e.stack);
        }
        RoleModel.updateRoleInfo(roleId, { quitTime: nowSeconds() });
        // if(teamCode) { // 如果有寻宝中的队伍，那么等于战败
        //     setComBtlOnUserLeave(roleId, teamCode)
        // }
        saveLoginAndOutLog(LOG_TYPE.LOGINOUT, session);
        leaveServer(session);
    }


    /**
     * New client entry chat server.
     *
     * @param  {Object}   msg     request message
     * @param  {Object}   session current session object
     */
    async gmEnter(msg: { token: string }, session: FrontendSession) {

        let self = this;

        let user = await GMUserModel.getGmAccountByToken(msg.token);
        if (!user) {
            console.log('user token not found');
            return resResult(STATUS.TOKEN_ERR);
        }
        if(!user.isEnable) {
            return resResult(STATUS.GM_NO_AUTHORITY_GET);
        }

        // console.log(self.app.get('serverId'));
        await session.abind(user.username);
        session.set('uid', user.uid);
        session.set('roleId', user.username);
        session.set('roleName', user.name);
        session.set('groupId', user.groupId);
        session.set('eventStatus', 0);
        session.set('sid', self.app.get('serverId'));
        session.push('uid', () => { });
        session.push('sid', () => { });
        session.push('roleId', () => { });
        session.push('roleName', () => { });
        session.push('groupId', () => { });
        session.push('eventStatus', () => { });
        // session.push('rid', function (err) {
        //     if (err) {
        //         console.error('set rid for session service failed! error is : %j', err.stack);
        //     }
        // });
        session.on('closed', this.onGmUserLeave.bind(this));

        sendMessageToUserWithSuc(user.username, 'onGMENTER', {}, self.app.get('serverId'))

        // put user into channel
        return resResult(STATUS.SUCCESS, {
            env: this.app.get('env')
        });
    }

    /**
     * User log out handler
     *
     * @param {Object} app current application
     * @param {Object} session current session object
     *
     */
    async onGmUserLeave(session: FrontendSession, reason: string) {
        if (!session || !session.uid) {
            return;
        }
    }

    /**
     * ! 仅用于测试
     * @description 测试接口，用于查询和返回 token
     * @param {{ tel: string, magicWord: string }} msg 要查询的手机号；需要验证的密码
     * @param {FrontendSession} session
     * @returns
     * @memberof EntryHandler
     */
    async debugQueryToken(msg: { tel: string, magicWord: string }, session: FrontendSession) {
        const { tel, magicWord } = msg;
        if (magicWord !== DEBUG_MAGIC_WORD) {
            return resResult(STATUS.TOKEN_ERR);
        }
        const token = await UserModel.findTokenByTel(tel);
        return resResult(STATUS.SUCCESS, { token });
    }

    /**
     * ! 仅用于测试
     * @description 测试接口，用于查询和返回 token
     * @param {{ seqId: string, magicWord: string }} msg 要查询的手机号；需要验证的密码
     * @param {FrontendSession} session
     * @returns
     * @memberof EntryHandler
     */
    async debugQueryTokenById(msg: { uid: number, magicWord: string }, session: FrontendSession) {
        const { uid, magicWord } = msg;
        console.log('debugQueryTokenById msg:', msg);
        if (magicWord !== DEBUG_MAGIC_WORD) {
            return resResult(STATUS.TOKEN_ERR);
        }
        const user = await UserModel.findTokenByUid(uid);
        console.log('debugQueryTokenById got user:', user);
        return resResult(STATUS.SUCCESS, { user });
    }

    async debugGetRole(msg: { magicWord: string, type: string }, session: FrontendSession) {
        const { magicWord, type = 'role' } = msg;
        if (magicWord !== DEBUG_MAGIC_WORD) {
            return resResult(STATUS.TOKEN_ERR);
        }
        let roleId = session.get('roleId');
        let role = await RoleModel.findByRoleId(roleId, '-_id', true, true);
        let module = await getModuleData(type, { role, session });
        return resResult(STATUS.SUCCESS, { role: module });
    }

    async debugQueryTokenByTel(msg: { tel: string, magicWord: string, serverId: number }, session: FrontendSession) {
        const { tel, magicWord, serverId } = msg;
        if (magicWord !== DEBUG_MAGIC_WORD) {
            return resResult(STATUS.TOKEN_ERR);
        }
        let token = await UserModel.findTokenByTel(tel);
        if(!token) {
                    // 用户注册登录
            token = generateStr(256);
            let {user} = await UserModel.createOrUpdate(false, tel, token, 'pc', ' ', pinus.app.get('env'), '', '');
            const roleId = genCode(10);
            const code = genCode(6);
            const seqId = await Counter.getNewCounter(COUNTER.ROLE) || -1;
    
            await RoleModel.createRole(user.uid, serverId, { roleId, code, roleName: roleId, seqId, lv: DEFAULT_LV, exp: (getExpByLv(DEFAULT_LV - 1) || { sum: 0 }).sum || 0 }, '');
        }
        console.log('debugQueryTokenByTel got user:', token);
        return resResult(STATUS.SUCCESS, { user: token });
    }

    async debugPushRefToOnlineUsers(msg: { magicWord: string, todayZeroPoint: number }, session: FrontendSession) {
        const { magicWord, todayZeroPoint } = msg;
        console.log('debugQueryTokenById msg:', msg);
        if (magicWord !== DEBUG_MAGIC_WORD) {
            return resResult(STATUS.TOKEN_ERR);
        }

        await sendMessageToAllWithSuc(PUSH_ROUTE.REFRESH_TIME, { todayZeroPoint });
        return resResult(STATUS.SUCCESS);
    }
}