<template>
  <div>
    <h1 class="font-bold text-center text-2xl">Jtag</h1>
    <div class="flex flex-col">
      <p class="grow">Jtag Addr: {{ eqps.boardAddr }}</p>
      <p class="grow">Jtag Port: {{ eqps.boardPort.toString() }}</p>
      <div class="flex justify-between grow">
        <p>
          IDCode: 0x{{ jtagIDCode.toString(16).padStart(8, "0").toUpperCase() }}
        </p>
        <button class="btn btn-circle w-6 h-6" :onclick="getIDCode">
          <svg class="icon opacity-70 fill-primary" viewBox="0 0 1024 1024" version="1.1"
            xmlns="http://www.w3.org/2000/svg" p-id="4865" width="200" height="200">
            <path
              d="M894.481158 505.727133c0 49.589418-9.711176 97.705276-28.867468 143.007041-18.501376 43.74634-44.98454 83.031065-78.712713 116.759237-33.728172 33.728172-73.012897 60.211337-116.759237 78.712713-45.311998 19.156292-93.417623 28.877701-143.007041 28.877701s-97.695043-9.721409-142.996808-28.877701c-43.756573-18.501376-83.031065-44.98454-116.76947-78.712713-33.728172-33.728172-60.211337-73.012897-78.712713-116.759237-19.156292-45.301765-28.867468-93.417623-28.867468-143.007041 0-49.579185 9.711176-97.695043 28.867468-142.996808 18.501376-43.74634 44.98454-83.031065 78.712713-116.759237 33.738405-33.728172 73.012897-60.211337 116.76947-78.712713 45.301765-19.166525 93.40739-28.877701 142.996808-28.877701 52.925397 0 104.008842 11.010775 151.827941 32.745798 46.192042 20.977777 86.909395 50.79692 121.016191 88.608084 4.389984 4.860704 8.646937 9.854439 12.781094 14.97097l0-136.263453c0-11.307533 9.168824-20.466124 20.466124-20.466124 11.307533 0 20.466124 9.15859 20.466124 20.466124l0 183.64253c0 5.433756-2.148943 10.632151-5.986341 14.46955-3.847631 3.837398-9.046027 5.996574-14.479783 5.996574l-183.64253-0.020466c-11.307533 0-20.466124-9.168824-20.466124-20.466124 0-11.307533 9.168824-20.466124 20.466124-20.466124l132.293025 0.020466c-3.960195-4.952802-8.063653-9.782807-12.289907-14.479783-30.320563-33.605376-66.514903-60.098773-107.549481-78.753645-42.467207-19.289322-87.850837-29.072129-134.902456-29.072129-87.195921 0-169.172981 33.9533-230.816946 95.597265-61.654198 61.654198-95.597265 143.621025-95.597265 230.816946s33.943067 169.172981 95.597265 230.816946c61.643965 61.654198 143.621025 95.607498 230.816946 95.607498s169.172981-33.9533 230.816946-95.607498c61.654198-61.643965 95.597265-143.621025 95.597265-230.816946 0-11.2973 9.168824-20.466124 20.466124-20.466124C885.322567 485.261009 894.481158 494.429833 894.481158 505.727133z"
              p-id="4866"></path>
          </svg>
        </button>
      </div>
    </div>
    <div class="divider"></div>
    <UploadCard class="bg-base-200" :upload-event="eqps.jtagUploadBitstream"
      :download-event="eqps.jtagDownloadBitstream" :bitstream-file="eqps.jtagBitstream"
      @update:bitstream-file="handleBitstreamChange">
    </UploadCard>
    <div class="divider"></div>
    <div class="w-full">
      <legend class="fieldset-legend text-sm mb-0.3">Jtag运行频率</legend>
      <select class="select w-full" @change="handleSelectJtagSpeed" :value="props.jtagFreq">
        <option v-for="option in selectJtagSpeedOptions" :value="option.id">
          {{ option.text }}
        </option>
      </select>
    </div>
    <div class="flex flex-row items-center">
      <fieldset class="fieldset w-70">
        <legend class="fieldset-legend text-sm">边界扫描刷新率 / Hz</legend>
        <input type="number" class="input validator" required placeholder="Type a number between 1 to 1000" min="1"
          max="1000" v-model="jtagBoundaryScanFreq" title="Type a number between 1 to 1000" />
        <p class="validator-hint">输入一个1 ~ 1000的数</p>
      </fieldset>
      <button class="btn btn-primary grow mx-4" :class="eqps.enableJtagBoundaryScan ? '' : 'btn-soft'"
        :onclick="toggleJtagBoundaryScan">
        {{ eqps.enableJtagBoundaryScan ? "关闭边界扫描" : "启动边界扫描" }}
      </button>
    </div>
    <div class="divider"></div>
    <h1 class="font-bold text-center text-2xl">外设</h1>
    <div class="flex flex-row">
      <input type="checkbox" class="checkbox" :checked="eqps.enableMatrixKey" @change="handleMatrixkeyCheckboxChange" />
      <p class="mx-2">启用矩阵键盘</p>
    </div>
  </div>
</template>

<script lang="ts" setup>
import z from "zod";
import UploadCard from "@/components/UploadCard.vue";
import { useDialogStore } from "@/stores/dialog";
import { useEquipments } from "@/stores/equipments";
import { computed, ref, watchEffect, watchPostEffect } from "vue";

interface CapsProps {
  jtagAddr?: string;
  jtagPort?: number;
  jtagFreq?: string;
}

const emits = defineEmits<{
  changeJtagFreq: [text: string];
}>();
const props = withDefaults(defineProps<CapsProps>(), {});

const selectJtagSpeedOptions = ref([
  { text: "25 MHz", id: 1 },
  { text: "12.5 MHz", id: 2 },
  { text: "6.25 MHz", id: 3 },
  { text: "3.125 MHz", id: 4 },
  { text: "1562.5 KHz", id: 5 },
  { text: "781.25 KHz", id: 6 },
  { text: "390.625 KHz", id: 7 },
]);

// Global Stores
const dialog = useDialogStore();
const eqps = useEquipments();

const jtagBoundaryScanFreq = computed({
  get: () => eqps.jtagBoundaryScanFreq,
  set: (val) => {
    if (z.number().positive().max(1000).safeParse(val).success)
      eqps.jtagBoundaryScanFreq = val;
  },
});

// 使用传入的属性或默认值
const jtagIDCode = ref(0xffff_ffff);

function handleBitstreamChange(file: File | undefined) {
  eqps.jtagBitstream = file;
}

function handleSelectJtagSpeed(event: Event) {
  const target = event.target as HTMLSelectElement;
  eqps.jtagSetSpeed(target.selectedIndex);
  emits("changeJtagFreq", target.value);
}

async function handleMatrixkeyCheckboxChange(event: Event) {
  const target = event.target as HTMLInputElement;
  if (target.checked) {
    const ret = await eqps.matrixKeypadEnable(true);
    if (!ret) {
    }
  } else {
    const ret = await eqps.matrixKeypadEnable(false);
    if (!ret) {
    }
  }
}

async function toggleJtagBoundaryScan() {
  if (eqps.jtagClientMutex.isLocked()) {
    dialog.warn("Jtag正在被占用");
    return;
  }

  eqps.enableJtagBoundaryScan = !eqps.enableJtagBoundaryScan;
}

async function getIDCode(isQuiet: boolean = false) {
  jtagIDCode.value = await eqps.jtagGetIDCode(isQuiet);
}

watchEffect(async () => {
  if (eqps.setAddr(props.jtagAddr) && eqps.setPort(props.jtagPort))
    getIDCode(true);
});
</script>

<style scoped lang="postcss">
@import "@/assets/main.css";
</style>
