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

const { onRender } = useScreen();

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

const { assets } = useAssets();

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

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

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

  if (isOpen.value || animation.playing) {
    assets.images.settings.topScreen.clock.time.draw(
      ctx,
      48 - animation.stage2Offset,
      -48 + animation.stage1Offset,
    );
    assets.images.settings.topScreen.clock.date.draw(
      ctx,
      0,
      -96 + animation.stage2Offset + animation.stage1Offset,
    );
    assets.images.settings.topScreen.clock.achievements.draw(
      ctx,
      0,
      -48 + animation.stage1Offset,
    );

    assets.images.settings.topScreen.clock.clockActive.draw(ctx, 0, 0);
  } else if (isAnyOtherMenuOpen.value) {
    assets.images.settings.topScreen.clock.clockDisabled.draw(ctx, 0, 0);
  } else {
    assets.images.settings.topScreen.clock.clock.draw(ctx, 0, 0);
  }
});

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