import { ACTIVITY_TYPE, MAIL_TYPE, REFRESH_TIME } from '../../consts';
import { ActivityModel, ActivityModelType } from '../../db/Activity';
import { ActivityDailyCoinModel, ActivityDailyCoinModelType } from '../../db/ActivityDailyCoin';
import { RoleModel } from '../../db/Role';
import { ServerlistModel, ServerlistType } from '../../db/Serverlist';
import { DailyCoinData } from '../../domain/activityField/dailyCoinField';
import { RewardInter } from '../../pubUtils/interface';
import { stringWithTypeToRewardInter } from '../../pubUtils/roleUtil';
import { getZeroPointD, getZeroPointOfTimeD } from '../../pubUtils/timeUtil';
import { sendMailByContent } from '../mailService';
import { getRoleCreateTime, getServerCreateTime } from '../redisService';
import { getActivitiesByType, getActivityById } from './activityService';


/**
   * 玩家玩家活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
export async function getPlayerDailyCoinData(activityId: number, serverId: number, roleId: string) {
    let activityData = await getActivityById(activityId);
    let createTime = await getRoleCreateTime(roleId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new DailyCoinData(activityData, createTime, serverTime);

    let playerRecord: ActivityDailyCoinModelType = await ActivityDailyCoinModel.findData(serverId, activityData.activityId, roleId, playerData.roundIndex);
    playerData.setPlayerRecords(playerRecord);
    return playerData;
}

export async function sendUnReceivedActivityDailyCoin(servers: ServerlistType[]) {
    let today = new Date().setHours(REFRESH_TIME, 0, 0, 0);
    let yesterday = today - 86400000;
    let unreceivedRecord = await ActivityDailyCoinModel.findUnreceivedBox(new Date(yesterday), new Date(today));
    for(let { serverId } of servers) {
        let serverTime = await getServerCreateTime(serverId);
        let activities = await getActivitiesByType(serverId, ACTIVITY_TYPE.DAILY_COIN);
        for(let activityData of activities) {
            for(let playerRecord of unreceivedRecord) {
                if(playerRecord.activityId != activityData.activityId) continue;
                if(playerRecord.serverId != serverId) continue;
                await sendUnReceivedRecor(activityData, playerRecord, serverTime);
            }
        }
    }
}

async function sendUnReceivedRecor(activityData: ActivityModelType, playerRecord: ActivityDailyCoinModelType, serverTime: number) {
    let createTime = await getRoleCreateTime(playerRecord.roleId);

    let playerData = new DailyCoinData(activityData, createTime, serverTime);
    playerData.setPlayerRecords(playerRecord);
    let unreceivedRecord = playerData.getUnReceivedBox();
    if(unreceivedRecord.length > 0) {
        let rewards: RewardInter[] = [], boxIds: number[] = [];
        for(let { cellIndex, reward } of unreceivedRecord) {
            rewards.push(...stringWithTypeToRewardInter(reward));
            boxIds.push(cellIndex);
        }
        await sendMailByContent(MAIL_TYPE.DAILY_COIN, playerRecord.roleId, { goods: rewards });
        await ActivityDailyCoinModel.repaireBox(playerRecord._id, boxIds);
    }
}

export function mergeData(data: any[]) {
    let newDate = [];
    for (let item of data) {
        let index = newDate.findIndex(obj => { return obj.id === item.id });
        if (index != -1) {
            newDate[index].count += item.count;
        } else {
            newDate = newDate.concat(item);
        }
    }
    return newDate;
}
