import humanizeDuration from 'humanize-duration';
import getEmojiByName from '../utils/getEmojiByName.js';

export const event = 'channelUpdate';

export async function callback(client, oc, nc) {
    try {
        let config = await client.memory.readGuildInfo(nc.guild.id);
        const { getAuditLog, log, NucleusColors, entry, renderDelta, renderUser, renderChannel } = client.logger

        if (nc.parent && (nc.parent.id === config.tickets.category)) return

        let auditLog = await getAuditLog(nc.guild, 'CHANNEL_UPDATE');
        let audit = auditLog.entries.filter(entry => entry.target.id === nc.id).first();
        if (audit.executor.id === client.user.id) return;

        let emoji:string;
        let readableType:string;
        let displayName:string ;
        let changes = {
            channelId: entry(nc.id, `\`${nc.id}\``),
            channel: entry(nc.id, renderChannel(nc)),
            edited: entry(new Date().getTime(), renderDelta(new Date().getTime())),
            editedBy: entry(audit.executor.id, renderUser((await nc.guild.members.fetch(audit.executor.id)).user)),
        }
        if (oc.name !== nc.name) changes["name"] = entry([oc.name, nc.name], `${oc.name} -> ${nc.name}`);
        if (oc.position !== nc.position) changes["position"] = entry([oc.position, nc.position], `${oc.position} -> ${nc.position}`);

        switch (nc.type) {
            case 'GUILD_TEXT': {
                emoji = "CHANNEL.TEXT.EDIT";
                readableType = "Text";
                displayName = "Text Channel"
                let oldTopic = oc.topic, newTopic = nc.topic;
                if (oldTopic) {
                    if (oldTopic.length > 256) oldTopic = `\`\`\`\n${oldTopic.replace('`', "'").substring(0, 253) + '...'}\n\`\`\``
                    else oldTopic = `\`\`\`\n${oldTopic.replace('`', "'")}\n\`\`\``
                } else { oldTopic = "None"; }
                if (newTopic) {
                    if (newTopic.length > 256) newTopic = `\`\`\`\n${newTopic.replace('`', "'").substring(0, 253) + '...'}\n\`\`\``
                    else newTopic = `\`\`\`\n${newTopic.replace('`', "'")}\n\`\`\``
                } else { newTopic = "None"; }
                let nsfw = ["", ""]
                nsfw[0] = oc.nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`;
                nsfw[1] = nc.nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`;
                if (oc.topic !== nc.topic) changes["description"] = entry([oc.topic, nc.topic], `\nBefore: ${oldTopic}\nAfter: ${newTopic}`);
                if (oc.nsfw !== nc.nsfw) changes["nsfw"] = entry([oc.nsfw, nc.nsfw], `${nsfw[0]} -> ${nsfw[1]}`);
                if (oc.rateLimitPerUser !== nc.rateLimitPerUser) changes["rateLimitPerUser"] = entry(
                    [oc.rateLimitPerUser, nc.rateLimitPerUser],
                    `${humanizeDuration(oc.rateLimitPerUser * 1000)} -> ${humanizeDuration(nc.rateLimitPerUser * 1000)}`
                );

                break;
            }
            case 'GUILD_NEWS': {
                emoji = "CHANNEL.TEXT.EDIT";
                readableType = "News";
                displayName = "News Channel"
                let oldTopic = oc.topic, newTopic = nc.topic;
                if (oldTopic) {
                    if (oldTopic.length > 256) oldTopic = `\`\`\`\n${oldTopic.replace('`', "'").substring(0, 253) + '...'}\n\`\`\``
                    else oldTopic = `\`\`\`\n${oldTopic.replace('`', "'")}\n\`\`\``
                } else { oldTopic = "None"; }
                if (newTopic) {
                    if (newTopic.length > 256) newTopic = `\`\`\`\n${newTopic.replace('`', "'").substring(0, 253) + '...'}\n\`\`\``
                    else newTopic = `\`\`\`\n${newTopic.replace('`', "'")}\n\`\`\``
                } else { newTopic = "None"; }
                if (oc.nsfw !== nc.nsfw) changes["nsfw"] = entry([oc.nsfw, nc.nsfw], `${oc.nsfw ? "On" : "Off"} -> ${nc.nsfw ? "On" : "Off"}`);
                break;
            }
            case 'GUILD_VOICE': {
                emoji = "CHANNEL.VOICE.EDIT";
                readableType = "Voice";
                displayName = "Voice Channel"
                if (oc.bitrate !== nc.bitrate) changes["bitrate"] = entry([oc.bitrate, nc.bitrate], `${oc.bitrate} -> ${nc.bitrate}`);
                if (oc.userLimit !== nc.userLimit) changes["maxUsers"] = entry([oc.userLimit, nc.userLimit], `${oc.userLimit ? oc.userLimit : "Unlimited"} -> ${nc.userLimit}`);
                if (oc.rtcRegion !== nc.rtcRegion) changes["region"] = entry(
                    [oc.rtcRegion, nc.rtcRegion],
                    `${oc.rtcRegion || "Automatic"} -> ${nc.rtcRegion || "Automatic"}`
                );
                break;
            }
            case 'GUILD_STAGE': {
                emoji = "CHANNEL.VOICE.EDIT";
                readableType = "Stage";
                displayName = "Stage Channel"
                let oldTopic = oc.topic, newTopic = nc.topic;
                if (oldTopic) {
                    if (oldTopic.length > 256) oldTopic = `\`\`\`\n${oldTopic.replace('`', "'").substring(0, 253) + '...'}\n\`\`\``
                    else oldTopic = `\`\`\`\n${oldTopic.replace('`', "'")}\n\`\`\``
                } else { oldTopic = "None"; }
                if (newTopic) {
                    if (newTopic.length > 256) newTopic = `\`\`\`\n${newTopic.replace('`', "'").substring(0, 253) + '...'}\n\`\`\``
                    else newTopic = `\`\`\`\n${newTopic.replace('`', "'")}\n\`\`\``
                } else { newTopic = "None"; }
                if (oc.bitrate !== nc.bitrate) changes["bitrate"] = entry([oc.bitrate, nc.bitrate], `${oc.bitrate} -> ${nc.bitrate}`);
                if (oc.userLimit !== nc.userLimit) changes["maxUsers"] = entry([oc.userLimit, nc.userLimit], `${oc.userLimit ? oc.userLimit : "Unlimited"} -> ${nc.userLimit}`);
                if (oc.rtcRegion !== nc.rtcRegion) changes["region"] = entry(
                    [oc.rtcRegion, nc.rtcRegion],
                    `${oc.rtcRegion || "Automatic"} -> ${nc.rtcRegion || "Automatic"}`
                );
                break;
            }
            case 'GUILD_CATEGORY': {
                emoji = "CHANNEL.CATEGORY.EDIT";
                readableType = "Category";
                displayName = "Category"
                break;
            }
            default: {
                emoji = "CHANNEL.TEXT.EDIT";
                readableType = "Channel";
                displayName = "Channel"
            }
        }
        let t = oc.type.split("_")[1];
        if (oc.type !== nc.type) changes["type"] = entry([oc.type, nc.type], `${t[0] + t.splice(1).toLowerCase()} -> ${readableType}`);
        if (!(Object.values(changes).length - 4)) return
        let data = {
            meta:{
                type: 'channelUpdate',
                displayName: displayName + ' Edited',
                calculateType: 'channelUpdate',
                color: NucleusColors.yellow,
                emoji: emoji,
                timestamp: audit.createdTimestamp
            },
            list: changes,
            hidden: {
                guild: nc.guild.id
            }
        }
        log(data);
    } catch {}
}