<script setup lang="ts">
import Buttons from "./Buttons.vue";

const { onRender } = useScreen();

const { assets } = useAssets();
const confirmationModal = useConfirmationModal();

const BG_WIDTH = assets.images.common.confirmationModal.rect.width;
const BG_HEIGHT = assets.images.common.confirmationModal.rect.height;
const BG_X = Math.floor((LOGICAL_WIDTH - BG_WIDTH) / 2);
const BG_Y = Math.floor((LOGICAL_HEIGHT - BG_HEIGHT) / 2);

const TEXT_Y = BG_Y + Math.floor(BG_HEIGHT / 2) - 8 - 16 + 2 + 2;

const BOTTOM_BAR_HEIGHT = 24;
const CLIP_HEIGHT = LOGICAL_HEIGHT - BOTTOM_BAR_HEIGHT;

const handleActivateA = () => {
  confirmationModal.onConfirm?.();
  confirmationModal.close();
};

const handleActivateB = () => {
  confirmationModal.close();
};

onRender((ctx) => {
  if (!confirmationModal.isVisible) return;

  ctx.beginPath();
  ctx.rect(0, 0, LOGICAL_WIDTH, CLIP_HEIGHT);
  ctx.clip();

  ctx.translate(0, confirmationModal.offsetY);

  assets.images.common.confirmationModal.draw(ctx, BG_X, BG_Y);

  ctx.font = "16px Pokemon DP Pro";
  ctx.textBaseline = "top";
  ctx.fillStyle = "#ffffff";

  fillTextCentered(ctx, confirmationModal.text, BG_X, TEXT_Y, BG_WIDTH);
}, 100);

onUnmounted(() => {
  confirmationModal.close();
});
</script>

<template>
  <Buttons
    v-if="confirmationModal.onConfirm"
    :y-offset="confirmationModal.modalButtonsYOffset"
    b-label="Cancel"
    a-label="Confirm"
    @activate-a="handleActivateA"
    @activate-b="handleActivateB"
  />
</template>
