import {getStandeeSet, type AvatarDefinition} from "../../standee-sets";
import {
  defineVQTimeline,
  say,
  type VQCustomTimelineEvent,
  type VQSpeechEvent,
  type VQTimelineEvent,
  type VQTimelineInputEvent,
} from "../../lib/VQRemotionLib/timeline";

export const compositionTitle = "__TITLE__";

export type VoicevoxVoice = Readonly<{
  speakerName: string;
  styleName: string;
}>;

export type CharacterDefinition = Readonly<{
  displayName: string;
  voicevox: VoicevoxVoice;
  avatar: AvatarDefinition;
}>;

export const characters = {
  zundamon: {
    displayName: "ずんだもん",
    voicevox: {
      speakerName: "ずんだもん",
      styleName: "ノーマル",
    },
    avatar: {
      ...getStandeeSet("zundamon_ohnegus_ai"),
      accentColor: "#79d36f",
      nameplatePosition: "none",
      idleAnimationType: "none",
      speakingAnimationType: "rhubarbLipSync",
    },
  },
  sayo: {
    displayName: "小夜",
    voicevox: {
      speakerName: "小夜/SAYO",
      styleName: "ノーマル",
    },
    avatar: {
      ...getStandeeSet("sayo_ohnegus_ai"),
      accentColor: "#6b5f83",
      nameplatePosition: "none",
      idleAnimationType: "none",
      speakingAnimationType: "rhubarbLipSync",
    },
  },
} as const satisfies Record<string, CharacterDefinition>;

export type CharacterId = keyof typeof characters;

export const initialVisibleCharacters: CharacterId[] = ["zundamon"];

export type ShowOptions = Readonly<{
  caption?: string;
  durationSeconds?: number;
}>;

export type SpeechEvent = VQSpeechEvent<CharacterId, VoicevoxVoice>;

export type ShowEvent = VQCustomTimelineEvent<
  "show",
  {
    character: CharacterId;
    caption?: string;
    durationSeconds?: number;
  }
>;

export type TimelineEvent = VQTimelineEvent<
  CharacterId,
  VoicevoxVoice,
  ShowEvent
>;
export type TimelineInputEvent = VQTimelineInputEvent<
  CharacterId,
  VoicevoxVoice,
  ShowEvent
>;

// 用途: キャラクターを画面に登場させ、任意の説明字幕を表示する。
// 使用方法: timeline 内で show("sayo", {caption: "小夜が登場！"}) のように呼び出す。
// オプションや引数詳細: durationSeconds を省略すると timing.ts の既定秒数で表示する。
export const show = (
  character: CharacterId,
  options: ShowOptions = {}
): ShowEvent => ({
  type: "show",
  character,
  ...options,
});

export const timeline = defineVQTimeline([
  say("__SLUG__-zunda-001", "zundamon", "みなさんこんにちは、ずんだもんなのだ！"),
  say("__SLUG__-zunda-002", "zundamon", "今日は新しい動画の下書きを作っていくのだ。"),
  show("sayo", {
    caption: "小夜が登場！",
  }),
  say("__SLUG__-sayo-001", "sayo", "小夜です。ここから脚本を書き換えて、動画を育てていきましょう。"),
  say("__SLUG__-zunda-003", "zundamon", "音声を作ったら、口パクも忘れずに生成するのだ。"),
] satisfies readonly TimelineInputEvent[]);
