import { applet, reactive } from "@scripts/applet/common";

////////////////////////////////////////////
// 🗂️ Applets
////////////////////////////////////////////
import type * as AudioEngine from "@applets/engine/audio/types.d.ts";
import type * as QueueEngine from "@applets/engine/queue/types.d.ts";

import type * as AudioUI from "@applets/theme/pilot/audio/types";

const engine = {
  audio: await applet<AudioEngine.State>("/engine/audio"),
  queue: await applet<QueueEngine.State>("/engine/queue"),
};

const _orchestrator = {
  input: await applet("/orchestrator/input-cache"),
  queueAudio: await applet("/orchestrator/queue-audio"),
  queueTracks: await applet("/orchestrator/queue-tracks"),
};

const ui = {
  audio: await applet<AudioUI.State>("/theme/pilot/audio/", { setHeight: true }),
};

////////////////////////////////////////////
// ⚙️ [Connections → Engines]
// 🔉 AUDIO
////////////////////////////////////////////

// NOTE:
// These could probably be optimised, but it works.

reactive(
  engine.audio,
  (data) =>
    data.isPlaying && (data.items[engine.queue.data.now?.id ?? Infinity]?.isPlaying ?? false),
  (isPlaying) => ui.audio.sendAction("modifyIsPlaying", isPlaying),
);

reactive(
  engine.audio,
  (data) => data.items[engine.queue.data.now?.id ?? Infinity]?.progress ?? 0,
  (progress: number) => ui.audio.sendAction("modifyProgress", progress),
);

////////////////////////////////////////////
// 🌅 [Connections → UI]
// 🔉 AUDIO
////////////////////////////////////////////

reactive(
  ui.audio,
  (data) => data.isPlaying,
  async (isPlaying, setter) => {
    const audioId = engine.queue.data.now?.id;

    // Automatically start playing something if nothing is playing yet.
    if (!audioId) {
      if (isPlaying) {
        const now = await engine.queue.sendAction("shift");
        if (!now) {
          console.warn("No tracks available yet, try again later.");
          await ui.audio.sendAction("modifyIsPlaying", false);
          setter(false);
        }
      }
      return;
    }

    // Otherwise just control the audio
    if (isPlaying) {
      engine.audio.sendAction("play", { audioId });
    } else {
      engine.audio.sendAction("pause", { audioId });
    }
  },
);

reactive(
  ui.audio,
  (data: AudioUI.State) => data.seekPosition,
  (seekPosition) => {
    if (seekPosition !== undefined && engine.queue.data.now?.id) {
      engine.audio.sendAction("seek", {
        percentage: seekPosition,
        audioId: engine.queue.data.now.id,
      });
    }
  },
);
