import { ACTIVITY_TYPE } from "../../consts";
import { ActivityGuildPayModel } from "../../db/ActivityGuildPay";
import { RoleType } from "../../db/Role";
import { GuildPayData } from "../../domain/activityField/guildPay";
import { getActivitiesByType, getActivityById, pushActivities, pushActivitiesToGuild, pushActivityInter } from "./activityService";
import { ServerlistModel } from '../../db/Serverlist';
import { ActivityGuildPayRecordModel } from "../../db/ActivityGuildPayRecord";
import { getServerCreateTime } from "../redisService";


/**
   * 玩家活动数据
   *
   * @param {number} serverId 区Id
   * @param {number} activityId 活动Id
   * @param {string} roleId 角色Id
   *
   */
 export async function getGuildPayData(serverId: number, activityId: number, roleId: string, guildCode: string) {
    let activityData = await getActivityById(activityId);
    let serverTime = await getServerCreateTime(serverId);
    let playerData = new GuildPayData(activityData, 0, serverTime);
    let receiveRecord = await ActivityGuildPayRecordModel.findData(serverId, activityId, playerData.roundIndex, roleId);
    playerData.setReceiveRecord(receiveRecord);
    if(guildCode) {
        let guildRecord = await ActivityGuildPayModel.findData(serverId, activityId, playerData.roundIndex, guildCode);
        playerData.setGuildRecord(guildRecord);
    }

    return playerData;
}

export async function getGuildPayDataShow(serverId: number, activityId: number, roleId: string, guildCode: string) {
    let playerData = await getGuildPayData(serverId, activityId, roleId, guildCode);
    if(playerData && playerData.canShow && playerData.canShow()) {
        return playerData.getShowResult();
    }
    return null;
}

export async function addGuildPay(role: RoleType, price: number) {
    let { hasGuild, roleId, roleName, serverId, guildCode } = role;
    if(!hasGuild) return; 
    let serverTime = await getServerCreateTime(serverId);
    let activities = await getActivitiesByType(serverId, ACTIVITY_TYPE.GUILD_PAY);
    let pushResult: pushActivityInter[] = [];
    for(let activityData of activities) {
        let  { activityId, type: activityType } = activityData;
        let obj = new GuildPayData(activityData, 0, serverTime);   
        if(obj && obj.canPay()) {
            let { isNew, result } = await ActivityGuildPayModel.addRecord(serverId, activityId, obj.roundIndex, guildCode, {
                roleId, roleName, price, hasQuit: false
            });
            if(isNew) {
                let { memberRecord = [] } = result;
                let payMemberCnt = memberRecord.filter(member => !member.hasQuit).length;
                pushResult.push({ activityId, activityType, param: { payMember: roleName, payMemberCnt } });
            }
        }
    }
    console.log('##### addGuildPay', pushResult.length)
    await pushActivitiesToGuild(pushResult, guildCode);
}

export async function guildPayQuitGuild(serverId: number, guildCode: string, roleId: string) {
    if(!guildCode) return;

    let serverTime = await getServerCreateTime(serverId);
    let activities = await getActivitiesByType(serverId, ACTIVITY_TYPE.GUILD_PAY);
    let pushResult: pushActivityInter[] = [];
    for(let activityData of activities) {
        let  { activityId, type: activityType } = activityData;
        let obj = new GuildPayData(activityData, 0, serverTime);   
        if(obj && obj.canPay()) {
            let result = await ActivityGuildPayModel.quitGuild(serverId, activityId, obj.roundIndex, guildCode, roleId);
            if(result) {
                let { memberRecord = [] } = result;
                let records = memberRecord.filter(member => !member.hasQuit);
                let payRecord = records.map(member => member.roleName);
                pushResult.push({ activityId, activityType, param: { payRecord, payMemberCnt: payRecord.length } });
            }
        }
    }
    await pushActivitiesToGuild(pushResult, guildCode);
}

export async function changeGuildActivity(guildCode: string, serverId: number, roleId: string, sid: string) {
    let activities = await getActivitiesByType(serverId, ACTIVITY_TYPE.GUILD_PAY);
    let pushResult: pushActivityInter[] = [];
    for(let activityData of activities) {
        let  { activityId, type: activityType } = activityData;
        let obj = await getGuildPayData(serverId, activityId, roleId, guildCode);   
        pushResult.push({ activityId, activityType, param: { payRecord: obj.payRecord, payMemberCnt: obj.payMemberCnt } });
    }
    await pushActivities(pushResult, roleId, sid);
}

export async function guildDismisActivity(guildCode: string, serverId: number) {
    let activities = await getActivitiesByType(serverId, ACTIVITY_TYPE.GUILD_PAY);
    let pushResult: pushActivityInter[] = [];
    for(let activityData of activities) {
        let  { activityId, type: activityType } = activityData;
        pushResult.push({ activityId, activityType, param: { payRecord: [], payMemberCnt: 0 } });
    }
    await pushActivitiesToGuild(pushResult, guildCode);
}