<script setup lang="ts">
import { computed, onMounted, reactive, ref, watch } from 'vue';
import { useRoute } from 'vue-router';
import PanelCard from '../components/ui/PanelCard.vue';
import SkeletonLines from '../components/ui/SkeletonLines.vue';
import { trpc } from '../utils/trpc';
import { getNpcColor } from '../utils/npcColor';
import { formatLog } from '../utils/formatLog';

type BattleCenterResponse = Awaited<ReturnType<typeof trpc.nation.getBattleCenter.query>>;
type GeneralEntry = BattleCenterResponse['generals'][number];

type LogType = 'generalHistory' | 'battleResult' | 'battleDetail' | 'generalAction';
type LogLine = { id: number; html: string };

const logTypes: LogType[] = ['generalHistory', 'battleDetail', 'battleResult', 'generalAction'];

const logLabels: Record<LogType, string> = {
    generalHistory: '장수 열전',
    battleDetail: '전투 기록',
    battleResult: '전투 결과',
    generalAction: '개인 기록',
};

const orderOptions = [
    { key: 'recentWar', label: '최근 전투' },
    { key: 'warnum', label: '전투 횟수' },
    { key: 'turnTime', label: '최근 턴' },
    { key: 'name', label: '이름' },
] as const;

type OrderKey = (typeof orderOptions)[number]['key'];

const loading = ref(false);
const logLoading = ref(false);
const error = ref<string | null>(null);
const data = ref<BattleCenterResponse | null>(null);

const orderBy = ref<OrderKey>('turnTime');
const selectedGeneralId = ref<number>(0);

const logs = reactive<Record<LogType, LogLine[]>>({
    generalHistory: [],
    battleDetail: [],
    battleResult: [],
    generalAction: [],
});

const resolveErrorMessage = (value: unknown): string => {
    if (value instanceof Error) {
        return value.message;
    }
    if (typeof value === 'string') {
        return value;
    }
    return 'unknown_error';
};

const parseGeneralId = (value: unknown): number => {
    if (typeof value === 'string') {
        const parsed = Number(value);
        if (Number.isFinite(parsed)) {
            return parsed;
        }
    }
    if (typeof value === 'number' && Number.isFinite(value)) {
        return value;
    }
    return 0;
};

const route = useRoute();

const loadBattleCenter = async () => {
    if (loading.value) {
        return;
    }
    loading.value = true;
    error.value = null;

    try {
        data.value = await trpc.nation.getBattleCenter.query();
    } catch (err) {
        error.value = resolveErrorMessage(err);
    } finally {
        loading.value = false;
    }
};

const orderedGenerals = computed(() => {
    const list = data.value?.generals ?? [];
    const key = orderBy.value;

    const sorted = [...list].sort((lhs, rhs) => {
        switch (key) {
            case 'recentWar': {
                const lhsVal = lhs.recentWar ?? '';
                const rhsVal = rhs.recentWar ?? '';
                return rhsVal.localeCompare(lhsVal);
            }
            case 'warnum':
                return rhs.warnum - lhs.warnum;
            case 'name': {
                const lhsVal = `${lhs.npcState}${lhs.name}`;
                const rhsVal = `${rhs.npcState}${rhs.name}`;
                return lhsVal.localeCompare(rhsVal);
            }
            case 'turnTime':
            default: {
                const lhsVal = lhs.turnTime ?? '';
                const rhsVal = rhs.turnTime ?? '';
                return rhsVal.localeCompare(lhsVal);
            }
        }
    });

    return sorted;
});

const selectedGeneral = computed(() => {
    const list = data.value?.generals ?? [];
    return list.find((general) => general.id === selectedGeneralId.value) ?? null;
});

const statusLine = computed(() => {
    if (!data.value) {
        return '감찰부 정보를 불러오는 중';
    }
    return `${data.value.currentYear}년 ${data.value.currentMonth}월 · 턴 ${data.value.turnTermMinutes}분`;
});

const formatGeneralLabel = (general: GeneralEntry): string => {
    const name = general.officerLevel > 4 ? `*${general.name}*` : general.name;
    const time = general.turnTime ? general.turnTime.slice(-5) : '--:--';
    if (orderBy.value === 'recentWar') {
        return `${name} (${general.recentWar ? general.recentWar.slice(-5) : '--:--'})`;
    }
    if (orderBy.value === 'warnum') {
        return `${name} (${general.warnum}회)`;
    }
    return `${name} (${time})`;
};

let logRequestId = 0;

const loadLogs = async (generalId: number) => {
    if (!generalId) {
        return;
    }
    logLoading.value = true;
    const requestId = (logRequestId += 1);

    try {
        const responses = await Promise.all(
            logTypes.map((type) => trpc.nation.getGeneralLog.query({ generalId, type }))
        );
        if (requestId !== logRequestId || selectedGeneralId.value !== generalId) {
            return;
        }
        for (const response of responses) {
            const formatted = response.logs.map((entry) => ({
                id: entry.id,
                html: formatLog(entry.text),
            }));
            logs[response.type] = formatted;
        }
    } catch (err) {
        error.value = resolveErrorMessage(err);
    } finally {
        if (requestId === logRequestId) {
            logLoading.value = false;
        }
    }
};

const changeTargetByOffset = (offset: number) => {
    const list = orderedGenerals.value;
    if (!list.length || !selectedGeneralId.value) {
        return;
    }
    const index = list.findIndex((general) => general.id === selectedGeneralId.value);
    if (index < 0) {
        return;
    }
    let nextIndex = (index + offset) % list.length;
    if (nextIndex < 0) {
        nextIndex += list.length;
    }
    selectedGeneralId.value = list[nextIndex].id;
};

watch(
    () => orderedGenerals.value,
    (list) => {
        if (!list.length) {
            selectedGeneralId.value = 0;
            return;
        }
        if (!selectedGeneralId.value || !list.some((general) => general.id === selectedGeneralId.value)) {
            selectedGeneralId.value = list[0].id;
        }
    }
);

watch(
    () => selectedGeneralId.value,
    (generalId) => {
        if (generalId) {
            void loadLogs(generalId);
        }
    }
);

watch(
    () => route.query,
    (query) => {
        const queryId = parseGeneralId(query.generalId ?? query.gen);
        if (queryId) {
            selectedGeneralId.value = queryId;
        }
    },
    { immediate: true }
);

onMounted(() => {
    void loadBattleCenter();
});
</script>

<template>
    <main class="ref-shell battle-page">
        <header class="ref-shell__topbar">
            <div>
                <h1 class="ref-shell__title">감찰부</h1>
                <p class="ref-shell__subtitle">{{ statusLine }}</p>
            </div>
            <div class="ref-shell__actions">
                <RouterLink class="ref-shell__control" to="/">메인</RouterLink>
                <RouterLink class="ref-shell__control" to="/nation/finance">내무부</RouterLink>
                <button class="ref-shell__control" @click="loadBattleCenter">새로고침</button>
            </div>
        </header>

        <div v-if="error" class="ref-feedback ref-feedback--error" role="alert">{{ error }}</div>

        <section class="layout-grid">
            <div class="stack">
                <PanelCard title="대상 선택" subtitle="정렬 기준과 장수를 선택합니다.">
                    <div class="selector-row">
                        <button class="ref-shell__control" @click="changeTargetByOffset(-1)">◀ 이전</button>
                        <select v-model="orderBy" class="select-input">
                            <option v-for="option in orderOptions" :key="option.key" :value="option.key">
                                {{ option.label }}
                            </option>
                        </select>
                        <select v-model.number="selectedGeneralId" class="select-input">
                            <option
                                v-for="general in orderedGenerals"
                                :key="general.id"
                                :value="general.id"
                                :style="{ color: getNpcColor(general.npcState) ?? undefined }"
                            >
                                {{ formatGeneralLabel(general) }}
                            </option>
                        </select>
                        <button class="ref-shell__control" @click="changeTargetByOffset(1)">다음 ▶</button>
                    </div>
                </PanelCard>

                <PanelCard title="장수 정보">
                    <SkeletonLines v-if="loading" :lines="5" />
                    <div v-else-if="selectedGeneral" class="battle-general-card">
                        <div class="battle-general-name">
                            {{ selectedGeneral.name }} (관직 {{ selectedGeneral.officerLevel }})
                        </div>
                        <div class="battle-general-grid">
                            <span>통솔</span><strong>{{ selectedGeneral.stats.leadership }}</strong> <span>무력</span
                            ><strong>{{ selectedGeneral.stats.strength }}</strong> <span>지력</span
                            ><strong>{{ selectedGeneral.stats.intelligence }}</strong> <span>자금</span
                            ><strong>{{ selectedGeneral.gold }}</strong> <span>군량</span
                            ><strong>{{ selectedGeneral.rice }}</strong> <span>병력</span
                            ><strong>{{ selectedGeneral.crew }}</strong> <span>훈련</span
                            ><strong>{{ selectedGeneral.train }}</strong> <span>사기</span
                            ><strong>{{ selectedGeneral.atmos }}</strong> <span>부상</span
                            ><strong>{{ selectedGeneral.injury }}</strong> <span>경험</span
                            ><strong>{{ selectedGeneral.experience }}</strong> <span>공헌</span
                            ><strong>{{ selectedGeneral.dedication }}</strong> <span>전투</span
                            ><strong>{{ selectedGeneral.warnum }}회</strong>
                        </div>
                    </div>
                    <div v-if="selectedGeneral" class="general-meta">
                        <div>최근 턴: {{ selectedGeneral.turnTime ? selectedGeneral.turnTime.slice(-5) : '-' }}</div>
                        <div>최근 전투: {{ selectedGeneral.recentWar || '-' }}</div>
                        <div>전투 횟수: {{ selectedGeneral.warnum }}</div>
                    </div>
                </PanelCard>
            </div>

            <div class="stack">
                <PanelCard title="장수 기록" subtitle="열전과 전투 기록">
                    <div class="log-grid">
                        <div v-for="type in logTypes" :key="type" class="log-block">
                            <div class="log-title">{{ logLabels[type] }}</div>
                            <SkeletonLines v-if="loading || logLoading" :lines="3" />
                            <template v-else>
                                <div v-if="logs[type].length === 0" class="empty">기록이 없습니다.</div>
                                <div v-for="entry in logs[type]" :key="entry.id" class="log-line" v-html="entry.html" />
                            </template>
                        </div>
                    </div>
                </PanelCard>
            </div>
        </section>
    </main>
</template>

<style scoped>
.layout-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0;
}

.stack {
    display: contents;
}

.selector-row {
    display: grid;
    grid-template-columns: 8.333% 33.333% 50% 8.333%;
    gap: 0;
    align-items: center;
}

.select-input {
    min-width: 0;
    height: 36px;
    padding: 4px 6px;
    border: 1px solid #777;
    border-radius: 0;
    background: #303030;
    color: inherit;
    font: inherit;
}

.general-meta {
    margin: 0;
    padding: 6px 8px;
    color: #ccc;
    display: grid;
    gap: 4px;
}

.battle-general-card {
    min-height: 292px;
    background-color: #172a52;
    background-image: var(--sammo-texture-blue);
}

.battle-general-name {
    min-height: 24px;
    padding: 2px 6px;
    text-align: center;
    border-bottom: 1px solid #777;
    background: rgba(220, 220, 220, 0.85);
    color: #111;
    font-weight: 700;
}

.battle-general-grid {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
}

.battle-general-grid > * {
    min-height: 24px;
    padding: 2px 5px;
    border-right: 1px solid #777;
    border-bottom: 1px solid #777;
}

.battle-general-grid > span {
    background-color: rgba(20, 75, 42, 0.7);
    color: #fff;
    text-align: center;
}

.battle-general-grid > strong {
    text-align: right;
    font-weight: 500;
}

.log-grid {
    display: contents;
}

.log-block {
    border: 1px solid #666;
    padding: 0;
    background: #111;
    min-height: 180px;
}

.log-title {
    min-height: 34px;
    margin: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    border-bottom: 1px solid #666;
    color: orange;
    background: #252525;
    font-size: 1.3em;
    font-weight: 500;
}

.log-line {
    padding: 2px 8px;
    border-bottom: 0;
}

.empty {
    padding: 2px 8px;
    color: #999;
}

/* PanelCard is retained as a data wrapper, but its presentation follows the
   flat bootstrap rows used by the reference page. */
:deep(.panel-card) {
    height: 100%;
    border: 1px solid #666;
    border-radius: 0;
    background-color: #302016;
    background-image: var(--sammo-texture-walnut);
    box-shadow: none;
}
.stack:first-child :deep(.panel-card:first-child) {
    grid-column: 1 / -1;
    border: 0;
}
.stack:first-child :deep(.panel-card:first-child .panel-header) {
    display: none;
}
.stack:first-child :deep(.panel-card:first-child .panel-body) {
    padding: 0;
}
.stack:nth-child(2) :deep(.panel-card),
.stack:nth-child(2) :deep(.panel-body) {
    display: contents;
}
.stack:nth-child(2) :deep(.panel-header) {
    display: none;
}
:deep(.panel-header) {
    min-height: 29px;
    justify-content: center;
    padding: 0;
}
:deep(.panel-title) {
    color: skyblue;
    font-size: 18px;
    font-weight: 500;
}
:deep(.panel-header),
.log-title {
    background-image: var(--sammo-texture-green);
}

@media (max-width: 991px) {
    .layout-grid {
        grid-template-columns: 1fr;
    }

    .selector-row {
        grid-template-columns: 16.666% 25% 41.666% 16.666%;
    }

    .log-grid {
        grid-template-columns: 1fr;
    }
}
</style>
