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);
    return;
    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);
}
