<script setup lang="ts">
const { onRender } = useScreen();

const store = useSettingsStore();
const { assets } = useAssets();

const renderNotification = (
  ctx: CanvasRenderingContext2D,
  image: AtlasImage,
  title: string,
  description: string,
) => {
  assets.images.settings.topScreen.notification.draw(ctx, 0, 0);
  image.draw(ctx, 2, 2);

  ctx.font = "10px NDS10";
  ctx.fillStyle = "#282828";
  ctx.fillText(title, 52, 13);

  ctx.fillStyle = "#fbfbfb";
  const lines = description.split("\n");
  const textY = lines.length === 1 ? 36 : 28;
  for (let i = 0; i < lines.length; i += 1) {
    ctx.fillText(lines[i]!, 52, textY + 14 * i);
  }
};

const mainNotification = computed(() => ({
  image: assets.images.settings.topScreen.settings,
  title: $t("settings.title"),
  description: $t("settings.description"),
}));

const IMAGES_MAP: Record<string, AtlasImage> = {
  options: assets.images.settings.topScreen.options.options,
  optionsStartUp: assets.images.settings.topScreen.options.startUp,
  optionsLanguage: assets.images.settings.topScreen.options.language,
  optionsGbaMode: assets.images.settings.topScreen.options.gbaMode,

  clock: assets.images.settings.topScreen.clock.clock,
  clockTime: assets.images.settings.topScreen.clock.time,
  clockDate: assets.images.settings.topScreen.clock.date,
  clockAlarm: assets.images.settings.topScreen.clock.alarm,

  user: assets.images.settings.topScreen.user.user,
  userUserName: assets.images.settings.topScreen.user.userName,
  userBirthday: assets.images.settings.topScreen.user.birthday,
  userMessage: assets.images.settings.topScreen.user.message,
  userColor: assets.images.settings.topScreen.user.color,

  touchScreen: assets.images.settings.topScreen.touchScreen.touchScreen,
};

const menuNotification = computed(() => {
  if (!store.currentMenu || !store.menuExpanded) return null;

  return {
    image: IMAGES_MAP[store.currentMenu]!,
    title: $t(`settings.${store.currentMenu}.title`),
    description: $t(`settings.${store.currentMenu}.description`),
  };
});

const submenuNotification = computed(() => {
  if (!store.currentSubMenu) return null;

  const image = IMAGES_MAP[store.currentSubMenu];
  if (!image) return null;

  const menuMatch = store.currentSubMenu.match(
    /^(options|clock|user|touchScreen)(.+)$/,
  );
  if (!menuMatch) return null;

  const [, menu, submenu] = menuMatch;
  const submenuKey = submenu!.charAt(0).toLowerCase() + submenu!.slice(1);

  return {
    image,
    title: $t(`settings.${menu}.${submenuKey}.title`),
    description: $t(`settings.${menu}.${submenuKey}.description`),
  };
});

onRender((ctx) => {
  let count = 1;
  if (menuNotification.value) count++;
  if (submenuNotification.value) count++;

  ctx.translate(0, 144 - (count - 1) * 16);

  renderNotification(
    ctx,
    mainNotification.value.image,
    mainNotification.value.title,
    mainNotification.value.description,
  );

  if (menuNotification.value) {
    ctx.translate(0, 16);
    renderNotification(
      ctx,
      menuNotification.value.image,
      menuNotification.value.title,
      menuNotification.value.description,
    );
  }

  if (submenuNotification.value) {
    ctx.translate(0, 16);
    renderNotification(
      ctx,
      submenuNotification.value.image,
      submenuNotification.value.title,
      submenuNotification.value.description,
    );
  }
});

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