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

export const event = "channelUpdate";

export async function callback(client, oc, nc) {
    const 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;

    const auditLog = await getAuditLog(nc.guild, "CHANNEL_UPDATE");
    const 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;
    const 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";
            }
            const 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";
        }
    }
    const 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;
    const 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);
}
