import { Application, ChannelService, HandlerService, } from 'pinus';
// import { sendRolesMails } from '../../../services/mailService';
import { reloadResources } from '../../../pubUtils/data';
import { HeroUpdate } from '../../../db/Hero';
import { RoleUpdate } from '../../../db/Role';
import { SkinUpdate } from '../../../db/Skin';
import { RankFirstModel, RankFirstType } from '../../../db/RankFirst';
import { getInitRoleInfo } from '../../../pubUtils/roleUtil';
import { DEFAULT_HEROES } from '../../../consts';
import { Figure } from '../../../domain/dbGeneral';
import { getDefaultRoleInfo } from '../../../services/roleService';
import { PVPConfigType } from '../../../db/SystemConfig';
import { treatRoleName, taflush } from '../../../services/sdkService';
import { getServerMainten, setServerMainten, stopServerMainten } from '../../../services/gmService';

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

export class RoleRemote {

    constructor(private app: Application) {
        this.app = app;
        this.channelService = app.get('channelService');
        this.setInitRole();
        this.loadRankFirst();
    }
    private channelService: ChannelService;
    private initHeroes: Map<number, HeroUpdate> = new Map(); // hid => hero
    private initRole: RoleUpdate = {};
    private initSkins: Map<number, SkinUpdate> = new Map(); // hid => skin
    private figureInfo: {heads: Figure[], frames: Figure[], spines: Figure[]};
    private rankFirstRewards: Map<number, Map<number, RankFirstType>> = new Map();

    private async loadRankFirst() {
        let rankFirstRewards = await RankFirstModel.findAllRankRewards();
        for(let rank of rankFirstRewards) {
            if(!this.rankFirstRewards.has(rank.serverId)) {
                this.rankFirstRewards.set(rank.serverId, new Map());
            }
            this.rankFirstRewards.get(rank.serverId).set(rank.id, rank);
        }
        this.app.set('rankFirstRewards', this.rankFirstRewards);
    }

    public setRankFirst(rank: RankFirstType) {
        console.log('****** setRankFirst', rank.serverId, rank.id, rank)
        if(!this.rankFirstRewards.has(rank.serverId)) {
            this.rankFirstRewards.set(rank.serverId, new Map());
        }
        this.rankFirstRewards.get(rank.serverId).set(rank.id, rank);
        this.app.set('rankFirstRewards', this.rankFirstRewards);
    }

    public getRankFirstById(serverId: number, id: number) {
        return this.rankFirstRewards.get(serverId)?.get(id);
    }

    public setInitRole() {
        let result = getInitRoleInfo();
        let { role, heroes, skins, figureInfo } = result;
        for(let hero of heroes) {
            this.initHeroes.set(hero.hid, hero);
        }
        for(let skin of skins) {
            this.initSkins.set(skin.hid, skin);
        }
        this.initRole = role;
        this.figureInfo = figureInfo;
        const initRoleInfos = getDefaultRoleInfo(this.initHeroes, this.initSkins, this.initRole, this.figureInfo);
        this.app.set('initRoleInfos', initRoleInfos);
        this.app.set('initHeroes', this.initHeroes);
        this.app.set('initSkins', this.initSkins);
    }

    public getInitRoleInfos() {
        let initRoleInfos = this.app.get('initRoleInfos');
        if (!initRoleInfos) {
            initRoleInfos = getDefaultRoleInfo(this.initHeroes, this.initSkins, this.initRole, this.figureInfo);
            this.app.set('initRoleInfos', initRoleInfos);
        }
        return initRoleInfos;
    }

    public getInitHeroes() {
        let result: HeroUpdate[] = [];
        for(let hid of DEFAULT_HEROES) {
            result.push(this.initHeroes.get(hid));
        }
        return result;
    }

    public getInitSkins() {
        let result: SkinUpdate[] = [];
        for(let hid of DEFAULT_HEROES) {
            result.push(this.initSkins.get(hid));
        }
        return result;
    }

    public getInitHeroById(hid: number) {
        return {
            heroInfo: this.initHeroes.get(hid),
            skinInfo: this.initSkins.get(hid)
        }
    }


    /**
     * 重载json资源
     */
    public async reloadResources() {
        reloadResources();
    }


    public setPvpSeasonNum(pvpConfig: PVPConfigType) {
        if(pvpConfig) {
            this.app.set('pvpSeasonNum', pvpConfig.seasonNum);
            this.app.set('pvpSeasonEndTime', pvpConfig.seasonEndTime);
        }
    }

    public async treatRoleName(roleId: string) {
        await treatRoleName(roleId);
    }

    public setServerMainten(serverIds: number[], startTime: number, endTime: number) {
        setServerMainten(serverIds, startTime, endTime);
    }

    public stopServerMainten(serverIds: number[]) {
        stopServerMainten(serverIds);
    }

    public getServerMainten(serverId: number) {
        return getServerMainten(serverId);
    }

    public taflush() {
        return taflush();
    }
}
