<script setup lang="ts">
const props = defineProps<{
  x: number;
  y: number;
}>();

const settingsStore = useSettingsStore();
const menusContext = inject<{
  isSubmenuSelected: ComputedRef<boolean>;
  selectedSubmenuParent: ComputedRef<string | null>;
}>("menusContext")!;

const { assets } = useAssets();

const isOpen = computed(
  () => settingsStore.currentMenu === "options" && settingsStore.menuExpanded,
);
const isAnyOtherMenuOpen = computed(() => {
  if (settingsStore.currentSubMenu) {
    return !settingsStore.currentSubMenu.startsWith("options");
  }
  if (menusContext.isSubmenuSelected.value) {
    return menusContext.selectedSubmenuParent.value !== "options";
  }
  return false;
});

const animation = useMenuAnimation("options", isOpen);

useRender((ctx) => {
  ctx.translate(props.x, props.y);

  if (isOpen.value || animation.playing) {
    ctx.drawImage(
      assets.settings.topScreen.options.language,
      0,
      -48 + animation.stage1Offset,
    );
    ctx.drawImage(
      assets.settings.topScreen.options.gbaMode,
      48 - animation.stage2Offset,
      -48 + animation.stage1Offset,
    );
    ctx.drawImage(
      assets.settings.topScreen.options.startUp,
      0,
      -96 + animation.stage2Offset + animation.stage1Offset,
    );

    ctx.drawImage(assets.settings.topScreen.options.optionsActive, 0, 0);
  } else if (isAnyOtherMenuOpen.value) {
    ctx.drawImage(assets.settings.topScreen.options.optionsDisabled, 0, 0);
  } else {
    ctx.drawImage(assets.settings.topScreen.options.options, 0, 0);
  }
});

defineOptions({
  render: () => null,
});
</script>
