<script setup lang="ts">
const props = withDefaults(
  defineProps<{
    x: number;
    y: number;
    opacity?: number;
    pressed: string | null;
    isAnyOtherMenuOpen: boolean;
    submenuExtraOffsetY: number;
  }>(),
  {
    opacity: 1,
  },
);

const { onRender } = useScreen();

const store = useSettingsStore();
const { assets } = useAssets((a) => a.images.settings.topScreen.user);

const isOpen = computed(
  () => store.currentMenu === "user" && store.menuExpanded,
);

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

const submenuExtraOffset = (submenu: string) =>
  store.selectedButton === submenu ? props.submenuExtraOffsetY : 0;

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

  if (isOpen.value || animation.playing) {
    (props.pressed === "userBirthday"
      ? assets.birthdayPressed
      : assets.birthday
    ).draw(
      ctx,
      -48 + animation.stage2Offset,
      -48 + animation.stage1Offset + submenuExtraOffset("userBirthday"),
    );
    (props.pressed === "userSnake" ? assets.snakePressed : assets.snake).draw(
      ctx,
      48 - animation.stage2Offset,
      -48 + animation.stage1Offset + submenuExtraOffset("userSnake"),
    );
    (props.pressed === "userColor" ? assets.colorPressed : assets.color).draw(
      ctx,
      0,
      -96 +
        animation.stage2Offset +
        animation.stage1Offset +
        submenuExtraOffset("userColor"),
    );
    (props.pressed === "userUserName"
      ? assets.userNamePressed
      : assets.userName
    ).draw(
      ctx,
      0,
      -48 + animation.stage1Offset + submenuExtraOffset("userUserName"),
    );

    (props.pressed === "user" ? assets.userPressed : assets.userActive).draw(
      ctx,
      0,
      0,
    );
  } else if (props.pressed === "user") {
    assets.userPressed.draw(ctx, 0, 0);
  } else if (props.isAnyOtherMenuOpen) {
    assets.userDisabled.draw(ctx, 0, 0);
  } else {
    assets.user.draw(ctx, 0, 0);
  }
});

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