import { Application, ChannelService } from 'pinus';
import { sendAllGuildRanks, sendGuildActEndMsg, calWoodenHorseAndSend, sendRaceStartMsg } from '../../../services/guildActivityService';
import { gameData } from '../../../pubUtils/data';
import { setWeek } from '../../../pubUtils/timeUtil';
import { getAllServers } from '../../../services/redisService';

export default function (app: Application) {
    return new GuildActivityRemote(app);
}

export class GuildActivityRemote {

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

    private channelService: ChannelService;

    /**
     * 从systimer服分发到guild各个服，发送排行榜数据
     */
    public async sendRankToGuilds(aid: number) {
        await sendAllGuildRanks(aid);
    }

    /**
     * 发送结束活动消息
     */
    public async guildActivityEnd(aid: number) {
        await sendGuildActEndMsg(aid);
    }

    /**
     * 计算粮草先行木马状态并发送
     */
    public async calWoodenHorseAndSend(serverId: number) {
        await calWoodenHorseAndSend(serverId);
    }

    /**
     * 发送粮草先行活动开始通知
     */
    async raceActivityStart(serverId: number) {
        await sendRaceStartMsg(serverId);
    }

    /**
     * !! 注意，这条函数会改变内存中的字典表，影响甚大，请考虑清楚后使用
     */
    async updateGuildActivityData(aid: number, isStart: boolean) {
        let guildActivity = gameData.guildActivity;
        let cur = guildActivity.get(aid);

        if(isStart) {
            let now = new Date();
            cur.startMinute = now.getMinutes();
            cur.startTime = now.getHours();
            cur.startSeconds = now.getSeconds();
        } else {
            let now = new Date(Date.now() - cur.duringTime * 1000);
            cur.startMinute = now.getMinutes();
            cur.startTime = now.getHours();
            cur.startSeconds = now.getSeconds();
        }
    }


    /**
     * !! 注意，这条函数会改变内存时间，影响甚大，请考虑清楚后使用
     */
    async setDay(week: number) {
        setWeek(week);
    }
}