<script>
  import { useNavigate } from "svelte-navigator";
  const navigate = useNavigate();

  let url = "";
  let video = true;
  let audio = true;

  $: disabled =
    (!audio && !video) ||
    (!url.startsWith("https://youtu.be") &&
      !url.startsWith("https://www.youtube.com"));

  async function add() {
    const result = await fetch("grid.php", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({ url, audio, video }),
    });
    if (!result.ok) {
      alert(result.status);
      return;
    }

    const lastId = await result.text();

    const currentResult = await fetch("grid.php");
    const current = await currentResult.json();

    let choosing;
    if (audio && video) {
      choosing = Math.random() > 0.5 ? "audio" : "video";
    } else if (audio) {
      choosing = "video";
    } else if (video) {
      choosing = "audio";
    }

    const viable = current.filter((link) => {
      if (link.id == lastId) {
        return false;
      }
      if (choosing == "audio") {
        return link.audio;
      } else if (choosing == "video") {
        return link.video;
      }
    });

    if (!viable.length) {
      alert(`Looks like there's no ${choosing} yet. Come back later!`);
      return;
    }

    const rndId = viable[Math.floor(Math.random() * viable.length)].id;

    if (choosing == "audio") {
      navigate(`/x/${lastId}/${rndId}`);
    } else if (choosing == "video") {
      navigate(`/x/${rndId}/${lastId}`);
    }
  }

  async function random() {
    const currentResult = await fetch("grid.php");
    const current = await currentResult.json();

    const audios = current.filter((link) => link.audio);
    if (!audios.length) {
      alert(`Looks like there's no audios yet. Come back later!`);
    }
    const videos = current.filter((link) => link.video);
    if (!videos.length) {
      alert(`Looks like there's no videos yet. Come back later!`);
    }

    const videoId = videos[Math.floor(Math.random() * videos.length)].id;
    const audioId = audios[Math.floor(Math.random() * audios.length)].id;

    navigate(`/x/${videoId}/${audioId}`);
  }
</script>

<main>
  <h1>XAOS TUBE</h1>

  <hr />

  <div class="input">
    <input type="text" placeholder="YouTube URL..." bind:value={url} />
    <div class="attrs">
      USE:
      <label><input type="checkbox" bind:checked={audio} />Audio</label>
      <label>
        <input type="checkbox" bind:checked={video} />
        Video</label
      >
    </div>

    <button {disabled} on:click={add}>ADD</button>
  </div>
  <hr />
  <button on:click={random}>JUST GIMME</button>
  <hr />
  <a href="https://kunsaxan.sdbs.cz">ksx</a>
</main>

<div class="fullscreen-bg">
  <video loop muted autoplay id="bg-video">
    <source src="assets/bg.mp4" />
  </video>
</div>

<style>
  main {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 4rem;
    gap: 5rem;
  }

  h1 {
    color: white;
    text-transform: uppercase;
    font-size: 4rem;
    font-weight: 100;
    line-height: 1.1;
    margin: 0;
  }

  :global(*:focus) {
    outline: none;
  }

  input {
    font-size: 20px;
    padding: 0.5em;
    border-radius: 5px;
  }

  button {
    font-family: inherit;
    font-size: inherit;
    padding: 1em 2em;
    color: white;
    background-color: rgba(255, 255, 255, 0.25);
    border-radius: 2em;
    border: 2px solid rgba(255, 255, 255, 0);
    outline: none;
    width: 200px;
    font-variant-numeric: tabular-nums;
    cursor: pointer;
  }

  button:focus {
    border: 2px solid white;
  }

  button:active {
    background-color: rgba(255, 255, 255, 0.2);
  }

  button:disabled {
    color: gray;
    pointer-events: none;
  }

  hr {
    height: 2px;
    background-color: white;
    border: none;
    width: 420px;
  }

  .input {
    text-align: center;
  }

  .input > * {
    margin: 0.5em;
  }

  .attrs {
    display: flex;
    justify-content: space-between;
  }

  #bg-video {
    position: absolute;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    object-fit: cover;
    z-index: -999;
    /* filter: brightness(.2) contrast(1.1); */
  }

  a,
  a:visited {
    color: white;
    text-decoration: none;
  }

  a:hover {
    text-decoration: underline;
  }
</style>
