<template>
    <div
        v-if="!muted.muted"
        v-show="!isDeleted"
        ref="el"
        v-hotkey="keymap"
        v-size="{ max: [500, 350, 300] }"
        class="lxwezrsl _block"
        :tabindex="!isDeleted ? '-1' : null"
        :class="{ renote: note.is_renote }"
    >
        <MagNoteSub
            v-if="conversation"
            v-for="note in conversation"
            :key="note.id"
            class="reply-to"
            :note="note"
            :detailedView="true"
        />
        <MkLoading v-else-if="appearNote.parent_note_id" mini />
        <MagNoteSub
            v-if="parentNote"
            :note="parentNote"
            class="reply-to"
            :detailedView="true"
        />

        <XNote
            ref="noteEl"
            @contextmenu.stop="onContextmenu"
            tabindex="-1"
            :note="note"
            detailedView
        ></XNote>

        <MkTab v-model="tab" :style="'underline'" @update:modelValue="loadTab">
            <option value="replies">
                <!-- <i class="ph-arrow-u-up-left ph-bold ph-lg"></i> -->
                <span v-if="note.reply_count > 0" class="count">{{
                    note.reply_count
                }}</span>
                {{ i18n.ts._notification._types.reply }}
            </option>
            <option value="renotes" v-if="note.renote_count > 0">
                <!-- <i class="ph-repeat ph-bold ph-lg"></i> -->
                <span class="count">{{ note.renote_count }}</span>
                {{ i18n.ts._notification._types.renote }}
            </option>
            <option value="reactions" v-if="magReactionCount(note) > 0">
                <!-- <i class="ph-smiley ph-bold ph-lg"></i> -->
                <span class="count">{{ magReactionCount(note) }}</span>
                {{ i18n.ts.reaction }}
            </option>
            <option value="quotes" v-if="directQuotes?.length > 0">
                <!-- <i class="ph-quotes ph-bold ph-lg"></i> -->
                <span class="count">{{ directQuotes!.length }}</span>
                {{ i18n.ts._notification._types.quote }}
            </option>
            <option value="clips" v-if="clips?.length > 0">
                <!-- <i class="ph-paperclip ph-bold ph-lg"></i> -->
                <span class="count">{{ clips.length }}</span>
                {{ i18n.ts.clips }}
            </option>
        </MkTab>

        <MagNoteSub
            v-if="directReplies && tab === 'replies'"
            v-for="note in directReplies"
            :key="note.id"
            :note="note"
            class="reply"
            :conversation="replies"
            :detailedView="true"
            :parentId="appearNote.id"
        />
        <MkLoading v-else-if="tab === 'replies' && note.reply_count > 0" />

        <MagNoteSub
            v-if="directQuotes && tab === 'quotes'"
            v-for="note in directQuotes"
            :key="note.id"
            :note="note"
            class="reply"
            :conversation="replies"
            :detailedView="true"
            :parentId="appearNote.id"
        />
        <MkLoading v-else-if="tab === 'quotes' && directQuotes?.length > 0" />

        <!-- <MkPagination
			v-if="tab === 'renotes'"
			v-slot="{ items }"
			ref="pagingComponent"
			:pagination="pagination"
		> -->
        <MkUserCardMini
            v-if="tab === 'renotes' && renotes"
            v-for="item in renotes"
            :key="item.user.id"
            :user="item.user"
            :with-chart="false"
        />
        <!-- </MkPagination> -->
        <MkLoading
            v-else-if="tab === 'renotes' && appearNote.renote_count > 0"
        />

        <div v-if="tab === 'clips' && clips.length > 0" class="_content clips">
            <MkA
                v-for="item in clips"
                :key="item.id"
                :to="`/clips/${item.id}`"
                class="item _panel"
            >
                <b>{{ item.name }}</b>
                <div v-if="item.description" class="description">
                    {{ item.description }}
                </div>
                <div class="user">
                    <MagAvatarResolvingProxy
                        :user="item.user"
                        class="avatar"
                        :show-indicator="true"
                    />
                    <MkUserName :user="item.user" :nowrap="false" />
                </div>
            </MkA>
        </div>
        <MkLoading v-else-if="tab === 'clips' && clips.length > 0" />

        <MkReactedUsers
            v-if="tab === 'reactions' && magReactionCount(appearNote) > 0"
            :note-id="note.id"
        ></MkReactedUsers>
    </div>
    <div v-else class="_panel muted" @click="muted.muted = false">
        <I18n :src="softMuteReasonI18nSrc(muted.what)" tag="small">
            <template #name>
                <MkA
                    v-user-preview="appearNote.user.id"
                    class="name"
                    :to="userPage(appearNote.user)"
                >
                    <MkUserName :user="appearNote.user" />
                </MkA>
            </template>
            <template #reason>
                <b class="_blur_text">{{ muted.matched.join(", ") }}</b>
            </template>
        </I18n>
    </div>
</template>

<script lang="ts" setup>
import {
    computed,
    onMounted,
    onUnmounted,
    onUpdated,
    Ref,
    ref,
    toRaw,
    watch,
} from "vue";
import * as misskey from "calckey-js";
import MkTab from "@/components/MkTab.vue";
import MagNoteSub from "@/components/MagNoteSub.vue";
import XNote from "@/components/MagNote.vue";
import XRenoteButton from "@/components/MagRenoteButton.vue";
import MkUserCardMini from "@/components/MkUserCardMini.vue";
import MkReactedUsers from "@/components/MkReactedUsers.vue";
import { pleaseLogin } from "@/scripts/please-login";
import { getWordSoftMute } from "@/scripts/check-word-mute";
import { userPage } from "@/filters/user";
import * as os from "@/os";
import { defaultStore } from "@/store";
import { reactionPicker } from "@/scripts/reaction-picker";
import { $i } from "@/account";
import { i18n } from "@/i18n";
import { getNoteMenu } from "@/scripts/get-note-menu";
import { useNoteCapture } from "@/scripts/use-note-capture";
import { stream } from "@/stream";
import { NoteUpdatedEvent } from "calckey-js/built/streaming.types";
import { endpoints, packed } from "magnetar-common";
import {
    magEffectiveNote,
    magReactionCount,
    magReactionToLegacy,
    resolveNote,
} from "@/scripts-mag/mag-util";

const props = defineProps<{
    note: packed.PackNoteMaybeFull;
    pinned?: boolean;
}>();

let tab = ref("replies");

let note = ref<packed.PackNoteMaybeFull>(structuredClone(toRaw(props.note)));

const softMuteReasonI18nSrc = (what?: string) => {
    if (what === "note") return i18n.ts.userSaysSomethingReason;
    if (what === "reply") return i18n.ts.userSaysSomethingReasonReply;
    if (what === "renote") return i18n.ts.userSaysSomethingReasonRenote;
    if (what === "quote") return i18n.ts.userSaysSomethingReasonQuote;

    // I don't think here is reachable, but just in case
    return i18n.ts.userSaysSomething;
};

const el = ref<HTMLElement | null>(null);
const noteEl = ref<HTMLElement | null>(null);
const menuButton = ref<HTMLElement | null>(null);
const renoteButton = ref<InstanceType<typeof XRenoteButton>>();
const reactButton = ref<HTMLElement | null>(null);
let appearNote = computed(
    () => magEffectiveNote(note.value) as packed.PackNoteMaybeFull,
);
let parentNote = ref(appearNote.value.parent_note);
const showContent = ref(false);
const isDeleted = ref(false);
const muted = ref(
    getWordSoftMute(appearNote.value, $i, defaultStore.state.mutedWords),
);
const translation = ref(null);
const translating = ref(false);
let conversation = ref<packed.PackNoteMaybeFull[]>([]);
const replies = ref<packed.PackNoteMaybeFull[]>([]);
let directReplies = ref<packed.PackNoteMaybeFull[] | null>(null);
let directQuotes = ref<packed.PackNoteMaybeFull[] | null>(null);
let clips = ref<misskey.entities.Clip[]>([]);
let renotes = ref<misskey.entities.Note[] | null>();
let isScrolling;

const keymap = {
    r: () => reply(true),
    "e|a|plus": () => react(true),
    q: () => renoteButton.value.renote(true),
    esc: blur,
    "m|o": () => menu(true),
    s: () => showContent.value !== showContent.value,
};

useNoteCapture({
    rootEl: el as Ref<HTMLElement>,
    note: appearNote,
    isDeletedRef: isDeleted,
});

function reply(viaKeyboard = false): void {
    pleaseLogin();
    os.post({
        reply: note,
        animation: !viaKeyboard,
    }).then(() => {
        focus();
    });
}

function react(viaKeyboard = false): void {
    pleaseLogin();
    blur();
    reactionPicker.show(
        reactButton.value,
        (reaction) => {
            os.api("notes/reactions/create", {
                noteId: appearNote.value.id,
                reaction: magReactionToLegacy(reaction),
            });
        },
        () => {
            focus();
        },
    );
}

function onContextmenu(ev: MouseEvent): void {
    const isLink = (el: HTMLElement) => {
        if (el.tagName === "A") return true;
        if (el.parentElement) {
            return isLink(el.parentElement);
        }
    };
    if (ev.target && isLink(ev.target as HTMLElement)) return;
    if (window.getSelection()?.toString() !== "") return;

    if (defaultStore.state.useReactionPickerForContextMenu) {
        ev.preventDefault();
        react();
    } else {
        os.contextMenu(
            getNoteMenu({
                note: note.value,
                translating,
                translation,
                menuButton: menuButton.value,
                isDeleted,
            }),
            ev,
        ).then(focus);
    }
}

function menu(viaKeyboard = false): void {
    os.popupMenu(
        getNoteMenu({
            note: note.value,
            translating,
            translation,
            menuButton: menuButton.value,
            isDeleted,
        }),
        menuButton.value ?? undefined,
        {
            viaKeyboard,
        },
    ).then(focus);
}

function focus() {
    noteEl.value?.focus();
}

function blur() {
    noteEl.value?.blur();
}

async function updateParent(note: packed.PackNoteMaybeFull) {
    if (note.parent_note) {
        parentNote.value = note.parent_note;
        return;
    } else if (note.parent_note_id) {
        parentNote.value = null;
        parentNote.value = await os.magApi(
            endpoints.GetNoteById,
            { attachments: true, context: true },
            { id: note.parent_note_id },
        );
        return;
    }

    parentNote.value = null;
}

watch(appearNote, updateParent, { immediate: true });

os.api("notes/children", {
    noteId: appearNote.value.id,
    limit: 30,
    depth: 12,
}).then((res) => {
    res = res.reduce(
        (acc: misskey.entities.Note[], resNote: misskey.entities.Note) => {
            if (resNote.userId == appearNote.value.user.id) {
                return [...acc, resNote];
            }
            return [resNote, ...acc];
        },
        [],
    );

    Promise.all(res.map(resolveNote)).then((a) => {
        replies.value = a;
        directReplies.value = a
            .filter((resNote) => resNote.parent_note_id === appearNote.value.id)
            .reverse();
        directQuotes.value = a.filter(
            (resNote) => resNote.renoted_note_id === appearNote.value.id,
        );
    });
});

if (appearNote.value.parent_note_id) {
    os.api("notes/conversation", {
        noteId: appearNote.value.parent_note_id,
        limit: 30,
    }).then(async (res) => {
        conversation.value = await Promise.all<packed.PackNoteMaybeFull>(
            res?.reverse().map(resolveNote),
        );
        focus();
    });
}

os.api("notes/clips", {
    noteId: appearNote.value.id,
}).then((res) => {
    clips.value = res as misskey.entities.Clip[];
});

// const pagination = {
// 	endpoint: "notes/renotes",
// 	noteId: note.id,
// 	limit: 10,
// };

// const pagingComponent = $ref<InstanceType<typeof MkPagination>>();

function loadTab() {
    if (tab.value === "renotes" && !renotes.value) {
        os.api("notes/renotes", {
            noteId: appearNote.value.id,
            limit: 100,
        }).then((res) => {
            renotes.value = res;
        });
    }
}

async function onNoteUpdated(noteData: NoteUpdatedEvent): Promise<void> {
    const { type, id, body } = noteData;

    let found = -1;
    if (id === appearNote.value.id) {
        found = 0;
    } else {
        for (let i = 0; i < replies.value.length; i++) {
            const reply = replies.value[i];
            if (reply.id === id) {
                found = i + 1;
                break;
            }
        }
    }

    if (found === -1) {
        return;
    }

    switch (type) {
        case "replied":
            const { id: createdId } = body;
            const replyNote = await os.magApi(
                endpoints.GetNoteById,
                {
                    context: true,
                    attachments: true,
                },
                {
                    id: createdId,
                },
            );

            replies.value.splice(found, 0, replyNote);
            if (found === 0) {
                directReplies.value = [
                    ...(directReplies.value ?? []),
                    replyNote,
                ];
            }
            break;

        case "deleted":
            if (found === 0) {
                isDeleted.value = true;
            } else {
                replies.value.splice(found - 1, 1);
            }
            break;
    }
}

document.addEventListener("wheel", () => {
    isScrolling = true;
});

onMounted(() => {
    stream.on("noteUpdated", onNoteUpdated);
    isScrolling = false;
    noteEl.value?.scrollIntoView();
});

onUpdated(() => {
    if (!isScrolling) {
        noteEl.value?.scrollIntoView();
        if (location.hash) {
            location.replace(location.hash); // Jump to highlighted reply
        }
    }
});

onUnmounted(() => {
    stream.off("noteUpdated", onNoteUpdated);
});
</script>

<style lang="scss" scoped>
.lxwezrsl {
    font-size: 1.05em;
    position: relative;
    transition: box-shadow 0.1s ease;
    contain: content;

    &:focus-visible {
        outline: none;

        &:after {
            content: "";
            pointer-events: none;
            display: block;
            position: absolute;
            z-index: 10;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            margin: auto;
            width: calc(100% - 8px);
            height: calc(100% - 8px);
            border: solid 1px var(--focus);
            border-radius: var(--radius);
            box-sizing: border-box;
        }
    }

    > .reply-to {
        margin-bottom: -16px;
        padding-bottom: 16px;
    }

    > :deep(.note-container) {
        padding-block: 28px 0;
        padding-top: 12px;
        font-size: 1.1rem;
        overflow: clip;
        outline: none;
        scroll-margin-top: calc(var(--stickyTop) + 20vh);
        &:not(:last-child) {
            border-bottom: 1px solid var(--divider);
            margin-bottom: 4px;
        }
        .article {
            cursor: unset;
            padding-bottom: 0;
        }
        &:first-child {
            padding-top: 28px;
        }
    }

    > :deep(.chips) {
        padding-block: 6px 12px;
        padding-left: 32px;
        &:last-child {
            margin-bottom: 12px;
        }
    }
    > :deep(.user-card-mini),
    > :deep(.reacted-users > *) {
        padding-inline: 32px;
        border-top: 1px solid var(--divider);
        border-radius: 0;
    }
    > :deep(.reacted-users > div) {
        padding-block: 12px;
    }

    > .reply {
        border-top: solid 0.5px var(--divider);
        padding-top: 24px;
        padding-bottom: 10px;
    }

    // Hover
    :deep(.reply > .main),
    .reply-to,
    :deep(.more) {
        position: relative;
        &::before {
            content: "";
            position: absolute;
            inset: -12px -24px;
            bottom: -0px;
            background: var(--panelHighlight);
            border-radius: var(--radius);
            opacity: 0;
            transition:
                opacity 0.2s,
                background 0.2s;
            z-index: -1;
        }
        &.reply-to {
            &::before {
                inset: 0px 8px;
            }
            &:not(.max-width_500px)::before {
                bottom: 16px;
            }
            &:first-of-type::before {
                top: 12px;
            }
            &.reply.max-width_500px:first-of-type::before {
                top: 4px;
            }
        }
        // &::after {
        // 	content: "";
        // 	position: absolute;
        // 	inset: -9999px;
        // 	background: var(--modalBg);
        // 	opacity: 0;
        // 	z-index: -2;
        // 	pointer-events: none;
        // 	transition: opacity .2s;
        // }
        &.more::before {
            inset: 0 !important;
        }
        &:hover,
        &:focus-within {
            --panel: var(--panelHighlight);
            &::before {
                opacity: 1;
                background: var(--panelHighlight) !important;
            }
        }
        // @media (pointer: coarse) {
        // 	&:has(.button:focus-within) {
        // 		z-index: 2;
        // 		--X13: transparent;
        // 		&::after {
        // 			opacity: 1;
        // 			backdrop-filter: var(--modalBgFilter);
        // 		}
        // 	}
        // }
    }
    :deep(.reply:target > .main),
    :deep(.reply-to:target) {
        z-index: 2;
        &::before {
            outline: auto;
            opacity: 1;
            background: none;
        }
    }
    &.max-width_500px {
        font-size: 0.975em;
        > .reply-to {
            &::before {
                inset-inline: -24px;
            }
            &:first-child {
                padding-top: 14px;
                &::before {
                    top: -24px;
                }
            }
        }

        > :deep(.note-container) {
            padding: 12px 0 0 0;
            font-size: 1.05rem;
            > .header > .body {
                padding-left: 10px;
            }
        }
        > .clips,
        > :deep(.user-card-mini),
        > :deep(.reacted-users > *) {
            padding-inline: 16px !important;
        }
        > :deep(.underline) {
            padding-left: 16px !important;
        }
    }

    &.max-width_300px {
        font-size: 0.825em;
    }
}

.muted {
    padding: 8px;
    text-align: center;
    opacity: 0.7;
}

.clips {
    // want to redesign at some point
    padding: 24px 32px;
    padding-top: 0;
    > .item {
        display: block;
        padding: 16px;
        // background: var(--buttonBg);
        border: 1px solid var(--divider);
        margin-bottom: var(--margin);
        transition: background 0.2s;
        &:hover,
        &:focus-within {
            background: var(--panelHighlight);
        }

        > .description {
            padding: 8px 0;
        }

        > .user {
            $height: 32px;
            padding-top: 16px;
            border-top: solid 0.5px var(--divider);
            line-height: $height;

            > .avatar {
                width: $height;
                height: $height;
            }
        }
    }
}
</style>
