<template>
  <TopBackBar v-model:searchable="searchable" :title="commandName" :type="procEntryMode" />
  <div class="bg0">
    <div>장수에게 턴을 당기거라 미루라고 지시합니다.</div>
    <div class="row">
      <div class="col-12 col-lg-5">
        장수 :
        <SelectGeneral
          v-model="selectedGeneralID"
          :cities="citiesMap"
          :generals="generalList"
          :textHelper="textHelpGeneral"
          :searchable="searchable"
        />
      </div>
      <div class="col-2 col-lg-2">
        턴 :
        <b-button-group>
          <b-button :pressed="isPull" @click="isPull = true"> 당기기 </b-button>
          <b-button :pressed="!isPull" @click="isPull = false"> 미루기 </b-button>
        </b-button-group>
      </div>
      <div class="col-7 col-lg-3">
        <SelectAmount v-model="amount" :amountGuide="amountGuide" :maxAmount="maxAmount" :minAmount="minAmount" />
      </div>
      <div class="col-3 col-lg-2 d-grid">
        <b-button variant="primary" @click="submit">
          {{ commandName }}
        </b-button>
      </div>
    </div>
  </div>
  <BottomBar :title="commandName" :type="procEntryMode" />
</template>

<script lang="ts">
declare const procRes: {
  distanceList: Record<number, number[]>;
  cities: [number, string][];
  generals: procGeneralRawItemList;
  generalsKey: procGeneralKey[];
  minAmount: number;
  maxAmount: number;
  amountGuide: number[];
};

declare const staticValues: {
  commandName: string;
  entryInfo: ["General" | "Nation", unknown];
};
</script>
<script setup lang="ts">
import SelectGeneral from "@/processing/SelectGeneral.vue";
import SelectAmount from "@/processing/SelectAmount.vue";
import { ref } from "vue";
import { unwrap } from "@/util/unwrap";
import type { Args } from "@/processing/args";
import TopBackBar from "@/components/TopBackBar.vue";
import BottomBar from "@/components/BottomBar.vue";
import {
  convertGeneralList,
  getProcSearchable,
  type procGeneralItem,
  type procGeneralKey,
  type procGeneralRawItemList,
} from "@/processing/processingRes";
import { getNPCColor } from "@/utilGame";

const citiesMap = ref(
  new Map<
    number,
    {
      name: string;
      info?: string;
    }
  >()
);
for (const [id, name] of procRes.cities) {
  citiesMap.value.set(id, { name });
}

const generalList = convertGeneralList(procRes.generalsKey, procRes.generals);
const amount = ref(1);
const isPull = ref(true);

const selectedGeneralID = ref(generalList[0].no);

type procGeneralItemWithTurn0Brief = procGeneralItem & {
  turn0Brief: string;
};

function textHelpGeneral(_gen: procGeneralItem): string {
  const gen = _gen as procGeneralItemWithTurn0Brief;
  const nameColor = getNPCColor(gen.npc);
  const name = nameColor ? `<span style="color:${nameColor}">${gen.name}</span>` : gen.name;
  return `${name} [${citiesMap.value.get(unwrap(gen.cityID))?.name}] (${gen.leadership}/${gen.strength}/${
    gen.intel
  }) (${gen.turn0Brief})`;
}

async function submit(e: Event) {
  const event = new CustomEvent<Args>("customSubmit", {
    detail: {
      amount: amount.value,
      isPull: isPull.value,
      destGeneralID: selectedGeneralID.value,
    },
  });
  unwrap(e.target).dispatchEvent(event);
}

const { commandName, entryInfo } = staticValues;
const searchable = getProcSearchable();

const procEntryMode: "chief" | "normal" = entryInfo[0] == "Nation" ? "chief" : "normal";

const { minAmount, maxAmount, amountGuide } = procRes;
</script>
