/*
 * Vencord, a Discord client mod
 * Copyright (c) 2025 Vendicated and contributors
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

import "./style.css";

import { classNameFactory } from "@api/Styles";
import { Devs } from "@utils/constants";
import definePlugin from "@utils/types";
import { findComponentByCodeLazy } from "@webpack";
import { ChannelStore, DateUtils, GuildStore, IconUtils, NavigationRouter, Popout, SnowflakeUtils, Text, UserStore, useStateFromStores } from "@webpack/common";
import { Guild } from "discord-types/general";

const ServerProfileComponent = findComponentByCodeLazy("{guildProfile:v,fetchGuildProfile:O,fetchStatus:I}");
const cl = classNameFactory("vc-serverprofileforward-");

const ArrowSvg = () => <svg aria-hidden="true" role="img" xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 24 24"><path fill="var(--text-low-contrast)" d="M9.3 5.3a1 1 0 0 0 0 1.4l5.29 5.3-5.3 5.3a1 1 0 1 0 1.42 1.4l6-6a1 1 0 0 0 0-1.4l-6-6a1 1 0 0 0-1.42 0Z" className=""></path></svg>;

const checkForIconExistence = (guild: Guild) => {
    if (!guild) return false;
    if (!guild.icon) return false;
    return true;
};

export default definePlugin({
    name: "MoreForwardMeta",
    description: "Show server profile under forwarded messages (if available) and always show time",
    authors: [Devs.nin0dev],
    ForwardFooter(message: any) {
        const { guild_id, channel_id, message_id } = message.message.messageReference;
        const guild = useStateFromStores([GuildStore], () => GuildStore.getGuild(guild_id));
        const channel = useStateFromStores([ChannelStore], () => ChannelStore.getChannel(channel_id));

        return <div className={cl("footer")} >
            {
                guild_id && <Popout
                    position="top"
                    renderPopout={() => <ServerProfileComponent guildId={guild_id} />}
                >
                    {popoutProps => <div className={cl("footer-element")} {...popoutProps}>
                        {
                            checkForIconExistence(guild) && <img src={guild.icon && IconUtils.getGuildIconURL({
                                id: guild.id,
                                icon: guild.icon,
                                canAnimate: true,
                                size: 32
                            })} alt={`Server icon for ${guild.name}`} className={cl("guild-icon")} />
                        }
                        <Text variant="text-sm/medium" className={cl("footer-text")} style={{
                            marginLeft: checkForIconExistence(guild) ? "20px" : "0"
                        }}>{guild ? guild.name : "View server"} </Text>
                        <ArrowSvg />
                    </div>
                    }
                </Popout>
            }
            {
                channel && <div className={cl("footer-element")} onClick={() => NavigationRouter.transitionTo(`/channels/${guild_id ?? "@me"}/${channel_id}/${message_id}`)} >
                    <Text variant="text-sm/medium" className={cl("footer-text")}>{(() => {
                        /*
                            - Text channel
                            - Voice channel
                            - Announcement channel
                            - Stage channel
                            - Directory channel
                            - Forum channel
                            - Media channel
                        */
                        if ([0, 2, 5, 13, 14, 15, 16].includes(channel.type)) return `#${channel.name}`;
                        // DMs
                        if (channel.type === 1) return `@${(() => {
                            const user = UserStore.getUser(channel.recipients[0]);
                            // @ts-expect-error
                            return user.globalName || user.username;
                        })()}`;
                        // GDMs
                        if (channel.type === 3) return channel.name || (() => {
                            const users = channel.recipients.map(r => UserStore.getUser(r));
                            // @ts-expect-error
                            return users.map(u => u.globalName || u.username).join(", ");
                        })();
                        // Threads
                        if ([10, 11, 12].includes(channel.type)) return channel.name;
                    })()}</Text>
                    <ArrowSvg />
                </div>
            }
            <div className={cl("footer-element")} style={{
                pointerEvents: "none"
            }}>
                <Text variant="text-sm/medium" className={cl("footer-text")}>
                    {DateUtils.calendarFormat(new Date(SnowflakeUtils.extractTimestamp(message_id)))}
                </Text>
            </div>
        </div>;
    },
    patches: [
        {
            find: "originLabel,\"  •  \"",
            replacement: {
                match: /(let{message:\i,snapshot:\i,index:\i}=(\i))(.{0,400})return .+TEXT_LOW_CONTRAST}\)]}\)/,
                replace: "$1$3return $self.ForwardFooter($2)"
            }
        }
    ]
});
