import { EPlace, HeroType } from '../db/Hero';
import { GuildType } from '../db/Guild';
import { CHANNEL_PREFIX, HERO_GROW_MAX, HERO_INITIAL_QUALITY, MSG_SOURCE, MSG_TYPE, WAR_TYPE } from '../consts';
import { createGroupMsg, pushGroupMsgToRoom } from './chatService';
import { pick } from 'lodash';
import { isString } from 'underscore';
import { BossInstanceType } from '../db/BossInstance';
import { GroupMessageType } from '../db/GroupMessage';

async function pushNormalHeroInfoBySource(roleId: string, roleName: string, serverId: number | string, source: number, heroInfo: Partial<HeroType>) {
    const hero = pick(heroInfo, ['hName', 'hid', 'seqId', 'quality', 'star', 'starStage', 'colorStar', 'colorStarStage']);
    const content = JSON.stringify({ roleId, roleName, hero });
    const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.SYS, `${serverId}`, MSG_TYPE.RICH_TEXT, source, content, null, null);
    await pushGroupMsgToRoom(msgData);
}

export async function pushHeroQualityUpMsg(roleId: string, roleName: string, serverId: number | string, heroInfo: Partial<HeroType>) {
    await pushNormalHeroInfoBySource(roleId, roleName, serverId, MSG_SOURCE.HERO_QUALITY_UP, heroInfo);
}

export async function pushGetHero(roleId: string, roleName: string, serverId: number | string, heroInfo: Partial<HeroType>) {
    if (heroInfo.quality == HERO_INITIAL_QUALITY.ORANGE) {
        await pushNormalHeroInfoBySource(roleId, roleName, serverId, MSG_SOURCE.GET_ORANGE_HERO, heroInfo);
    } else if(heroInfo.quality == HERO_INITIAL_QUALITY.UR){
        await pushNormalHeroInfoBySource(roleId, roleName, serverId, MSG_SOURCE.GET_UR_HERO, heroInfo);
    }
}

export async function pushHeroStarMax(roleId: string, roleName: string, serverId: number | string, heroInfo: Partial<HeroType>) {
    if (heroInfo.colorStar !== HERO_GROW_MAX.STAR) {
        return;
    }
    await pushNormalHeroInfoBySource(roleId, roleName, serverId, MSG_SOURCE.HERO_STAR_MAX, heroInfo);
}

export async function pushHeroWakeUp(roleId: string, roleName: string, serverId: number | string, heroInfo: Partial<HeroType>) {
    await pushNormalHeroInfoBySource(roleId, roleName, serverId, MSG_SOURCE.HERO_WAKE_UP, heroInfo);
}

// 装备升星到12星
export async function pushEquipStarMax(roleId: string, roleName: string, serverId: number, hid: number, equip: EPlace, isUpStar: boolean) {
    const { id, equipId, star, quality, } = equip;
    if(!isUpStar || star != HERO_GROW_MAX.EQUIP_STAR) return;

    const content = JSON.stringify({ roleId, roleName, hid, eplaceId: id, equipId, star, quality });
    const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.SYS, `${serverId}`, MSG_TYPE.RICH_TEXT, MSG_SOURCE.EQUIP_STAR_UP, content, null, null);
    await pushGroupMsgToRoom(msgData);
    return msgData;
}

// 装备升品到金色
export async function pushEquipQualityMax(roleId: string, roleName: string, serverId: number, hid: number, equip: EPlace, isUpQuality: boolean) {
    const { id, equipId, star, quality, } = equip;
    if(!isUpQuality || quality != HERO_GROW_MAX.EQUIP_QUALITY) return;

    const content = JSON.stringify({ roleId, roleName, hid, eplaceId: id, equipId, star, quality });
    const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.SYS, `${serverId}`, MSG_TYPE.RICH_TEXT, MSG_SOURCE.EQUIP_QUALITY_UP, content, null, null);
    await pushGroupMsgToRoom(msgData);
    return msgData;
}

export async function pushPresent(roleId: string, roleName: string, serverId: number | string, toRoleName: string, id: number) {
    const content = JSON.stringify({ roleId, roleName, toRoleName, id });
    const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.SYS, `${serverId}`, MSG_TYPE.RICH_TEXT, MSG_SOURCE.GROUP_SEND_GIFT, content, null, null);
    await pushGroupMsgToRoom(msgData);
}

export async function pushComBtlTeamMsg(teamCode: string, roleId: string, roleName: string, type: number, source: number, content: string, targetRoleId: string, targetMsgCode: string) {
    const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.TEAM, teamCode, type, source, content, targetRoleId, targetMsgCode);
    await pushGroupMsgToRoom(msgData);
    return msgData;
}

export async function pushGuildNoticeUpdateMsg(roleId: string, roleName: string, guildInfo: Partial<GuildType>) {
    const { code, notice } = guildInfo;
    if (!code || !isString(notice)) return null;
    const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.GUILD, code, MSG_TYPE.TEXT, MSG_SOURCE.GUILD_NOTICE, notice, null, null);
    await pushGroupMsgToRoom(msgData);
    return msgData;
}

export async function pushGuildUpStructureMsg(roleId: string, roleName: string, guildInfo: Partial<GuildType>, structure: {id: number, lv: number}) {
    const { code, name } = guildInfo;
    const guild = { code, name, structure };
    const content = JSON.stringify({ roleId, roleName, guild });
    const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.GUILD, code, MSG_TYPE.RICH_TEXT, MSG_SOURCE.GUILD_STRUCTURE_LV_UP, content, null, null);
    await pushGroupMsgToRoom(msgData);
    return msgData;
}

export async function pushGuildTrainSucMsg(roleId: string, roleName: string, guildCode: string, hid: number) {
    if (!guildCode) return null;
    const content = JSON.stringify({ roleId, roleName, hid });
    const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.GUILD, guildCode, MSG_TYPE.RICH_TEXT, MSG_SOURCE.GUILD_TRAIN_SUC, content, null, null);
    await pushGroupMsgToRoom(msgData);
    return msgData;
}

export async function pushGuildBossSucMsg(roleId: string, roleName: string, guildCode: string, bossInstance: BossInstanceType) {
    const boss = pick(bossInstance, ['warId', 'bossLv']);
    const content = JSON.stringify({ roleId, roleName, boss });
    const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.GUILD, guildCode, MSG_TYPE.RICH_TEXT, MSG_SOURCE.GUILD_BOSS_SUC, content, null, null);
    await pushGroupMsgToRoom(msgData);
    return msgData;
}

export async function pushNormalEquipMsg(roleId: string, roleName: string, serverId: number, source: number, id: number, name: string, quality: number) {
    const content = JSON.stringify({ roleId, roleName, equip: { id, name, quality } });
    const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.SYS, `${serverId}`, MSG_TYPE.RICH_TEXT, source, content, null, null);
    await pushGroupMsgToRoom(msgData);
    return msgData;
}

export async function pushNormalItemMsg(roleId: string, roleName: string, serverId: number, source: number, id: number, name: string) {
    const content = JSON.stringify({ roleId, roleName, equip: { id, name } });
    const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.SYS, `${serverId}`, MSG_TYPE.RICH_TEXT, source, content, null, null);
    await pushGroupMsgToRoom(msgData);
    return msgData;
}

export async function pushTowerMsg(roleId: string, roleName: string, serverId: number, source: number, lv: number) {
    if (!shouldPushTowerMsg(lv)) return null;
    const content = JSON.stringify({ roleId, roleName, lv });
    const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.SYS, `${serverId}`, MSG_TYPE.RICH_TEXT, source, content, null, null);
    await pushGroupMsgToRoom(msgData);
    return msgData;
}

async function pushGKFirstMsg(roleId: string, roleName: string, serverId: number, source: number, warType: number, warId: number) {
    const content = JSON.stringify({ roleId, roleName, warInfo: { warType, warId } });
    const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.SYS, `${serverId}`, MSG_TYPE.RICH_TEXT, source, content, null, null);
    await pushGroupMsgToRoom(msgData);
    return msgData;
}

export async function pushMysteryFirstMsg(roleId: string, roleName: string, serverId: number, warType: number, warId: number) {
    if (warType !== WAR_TYPE.MYSTERY) return null;
    const result = await pushGKFirstMsg(roleId, roleName, serverId, MSG_SOURCE.MYSTERY_FIRST_SUC, warType, warId);
    return result;
}

export async function pushVestigeFirstMsg(roleId: string, roleName: string, serverId: number, warType: number, warId: number) {
    if (warType !== WAR_TYPE.VESTIGE) return null;
    const result = await pushGKFirstMsg(roleId, roleName, serverId, MSG_SOURCE.VESTIGE_FIRST_SUC, warType, warId);
    return result;
}

export async function pushLadderTopChangeMsg(roleId: string, roleName: string, serverId: number | string) {
    const content = JSON.stringify({ roleId, roleName });
    const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.SYS, `${serverId}`, MSG_TYPE.RICH_TEXT, MSG_SOURCE.LADDER_FIRST_CHANGE, content, null, null);
    await pushGroupMsgToRoom(msgData);
    return msgData;
}

export async function createMarqueeMsg(roleId: string, roleName: string, serverId: number | string, content: string) {
    const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.SYS, `${serverId}`, MSG_TYPE.RICH_TEXT, MSG_SOURCE.SEND_MARQUEE, content, null, null);
    return msgData;
}

export async function pushMarqueeMsg(msgData: GroupMessageType) {
    await pushGroupMsgToRoom(msgData);
}

function shouldPushTowerMsg(lv: number) {
    // 100 层之后每 50 层触发
    return lv >= 100 && lv % 50 === 0;
}
