<script>
  import { link } from 'svelte-spa-router';
  import { onMount } from 'svelte';
  import Layout from '$/client/Layout.svelte';
  import api from "$/client/api.js";
  import { user } from "$/client/stores.js";

  let agree = false;
  let invite_sent = false;

  const request_invite = async () => {
    const [status, data] = await api.post("/api/discord");

    if(status === 200) {
      console.log("STATUS", status, "DATA", data);
      invite_sent = true;
      $user.discord_sent = true; // just enough update to change the CourseSidebar
    } else {
      console.error("Invalid response", status, data);
    }
  }
</script>

<style>
  p {
    font-size: 1.3em;
  }

  h1 {
    margin-top: 2rem;
  }
</style>

<Layout centered={ true } authenticated={ true }>

  {#if invite_sent}
    <h1>Discord Sent</h1>
    <p><b>Invite Sent!</b> Check your email for the invite code, and move quickly because you have a limited amount of time to accept the invite before it's invalid.  If you miss it, then you won't receive another one.  <b>See you soon!</b>
    </p>

    <button type="button"><a href="/" use:link>Back to the Course</a></button>
  {:else if $user.discord_sent}
    <h1>Discord Already Sent</h1>
    <p>Your discord access code was already sent to your registered email address.  Please check your spam folder.
    </p>

    <button type="button"><a href="/" use:link>Back to the Course</a></button>
  {:else}
    <h1>Discord Access</h1>

    <p>You can request an invite to the discord server.  You'll receive an email to your registered email address with a <b>one time use invite link</b>.  Be sure to use it promptly as it is only available for a limited time, and you only get one.</p>

    <h1>Covenant of Fair and Equal Enforcement</h1>

    <p>These values are the promises to you as a participant in the community.  Anyone running the server promises:</p>

    <callout>
    <ol>
      <li><b>Mutual Respect</b> -- I hope to earn your respect by showing you respect.</li>
      <li><b>Rules Apply to All</b> -- The Code of Conduct applies to everyone equally, especially anyone in a position of authority.</li>
      <li><b>No Outside Authority</b> -- Nobody has a right to control your life outside of the Server.</li>
      <li><b>No Secret Police</b> -- Anyone with control over the server will be known to you, and they won't make secret actions against you.</li>
      <li><b>Forgiving of Mistakes</b> -- Nobody will treat you like an enemy, and everyone has bad days, so if you get in trouble you'll be given a chance to change your behavior.  You'll first receive a warning, then a 7 day time out, then you'll finally be removed.</li>
      <li><b>Education Continues</b> -- Being removed from the server does not make you a bad person.  If you're removed from the server you can still ask for help and continue the course.</li>
      <li><b>No Authority without Consent</b> -- You can request an admin be removed if you feel they're doing a bad job.  If Zed is not managing things well you can tell him and he'll change his management style. No, you can't remove Zed.</li>
      <li><b>No Public Shaming</b> -- You will not be publicly shamed.  If you're not following the code of conduct you will be <b>privately</b> reminded of the rules, and told exactly how your behavior is impacting others.  If you block the admins though then, well, I guess we have to remove you until you're willing to talk privately.
    </ol>
    </callout>

    <h1>Code of Conduct</h1>

    <p>Please be respectful by following these four simple rules of behavior:</p>

    <callout>
    <ol>
      <li><b>No Drama</b> -- Don't cause drama with others on the server. That includes racism, sexism, harrassing, stalking, or anything that causes someone...drama.</li>
      <li><b>No Hacking</b> -- Don't hack, exploit, or access anything you don't own.  If you find an exploit in this server tell Zed immediately.</li>
      <li><b>No Free Therapy</b> -- There is no unpaid emotional labor allowed on the server. You paid for a programming course, not a therapy session.  It's unfair to everyone else to make them responsible for your mental health. If you're having strong emotions find someone you trust and talk to them about it, or just keep working on the code.  Usually if you just keep trying to solve it you'll find the solution and feel better.</li>
      <li><b>No Enforcing The Rules</b> -- You paid to take a class, not be an admin.  Only Zed (or a <b>well known admin</b>) is allowed to remind people of these rules.  If someone is causing drama, hacking, or demanding free therapy then simply DM Zed and block them.</li>
    </ol>
    </callout>

    <h1>Not Essential to the Course</h1>

    <p>Finally, this important warning about access to Discord:</p>

    <callout class="warning">
      <p><b>Access to the Discord server is not part of the product you purchased.</b>  If you are removed from the server you will still have access to the content you paid for, email help, and other services.  It's entirely voluntary and not essential to finish the course.  </p>
    </callout>

    <p>
      <label for="conduct-agree">
        <input type="checkbox" id="conduct-agree" bind:checked={ agree }>
        I Agree to the Server's Rules
      </label>
    </p>

    {#if agree}
      <button type="button" on:click={ request_invite }>Request an Invite Link</button>
    {:else}
      <button type="button" disabled>Check Agree to Continue</button>
    {/if}
  {/if}
</Layout>
