<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 === "user" && settingsStore.menuExpanded,
);
const isAnyOtherMenuOpen = computed(() => {
  if (settingsStore.currentSubMenu) {
    return !settingsStore.currentSubMenu.startsWith("user");
  }
  if (menusContext.isSubmenuSelected.value) {
    return menusContext.selectedSubmenuParent.value !== "user";
  }
  return false;
});

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

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

  if (isOpen.value || animation.playing) {
    ctx.drawImage(
      assets.settings.topScreen.user.birthday,
      -48 + animation.stage2Offset,
      -48 + animation.stage1Offset,
    );
    ctx.drawImage(
      assets.settings.topScreen.user.userName,
      0,
      -48 + animation.stage1Offset,
    );
    ctx.drawImage(
      assets.settings.topScreen.user.message,
      48 - animation.stage2Offset,
      -48 + animation.stage1Offset,
    );
    ctx.drawImage(
      assets.settings.topScreen.user.color,
      0,
      -96 + animation.stage2Offset + animation.stage1Offset,
    );

    ctx.drawImage(assets.settings.topScreen.user.userActive, 0, 0);
  } else if (isAnyOtherMenuOpen.value) {
    ctx.drawImage(assets.settings.topScreen.user.userDisabled, 0, 0);
  } else {
    ctx.drawImage(assets.settings.topScreen.user.user, 0, 0);
  }
});

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