<script>
  import { _ } from "svelte-i18n";
  import {
    PermissionService,
    CreatePermission,
    UserGroupService,
  } from "@odit/lfk-client-js";
  import toast from 'svelte-french-toast'

  import PromiseError from "../base/PromiseError.svelte";
  export let params;
  let [
    grantedPermissions_initial,
    grantedPermissions,
    to_add,
    to_delete,
    allpermissions,
    promises,
  ] = [[], [], [], [], [], []];
  $: original_data = {};
  $: save_enabled =
    JSON.stringify(grantedPermissions) ===
    JSON.stringify(grantedPermissions_initial);
  const group_promise = UserGroupService.userGroupControllerGetOne(
    params.groupid
  );
  group_promise.then((data) => {
    original_data = Object.assign(original_data, data);
  });
  function submit() {
    toast.loading($_("updating-permissions"));
    to_delete.forEach((d) => {
      promises = promises.concat([
        PermissionService.permissionControllerRemove(d, true),
      ]);
    });
    to_add.forEach((a) => {
      promises = promises.concat([
        PermissionService.permissionControllerPost(a),
      ]);
    });
    Promise.all(promises).then((values) => {
      promises = [];
      to_delete.forEach((d) => {
        to_delete = to_delete.filter((o) => o !== d);
      });
      to_add.forEach((a) => {
        to_add = to_add.filter(
          (o) => o.target + ":" + o.action !== a.target + ":" + a.action
        );
      });
      grantedPermissions_initial = grantedPermissions;
      toast.success($_("permissions-updated"));
    });
  }
  Object.values(CreatePermission.target).forEach((t) => {
    Object.values(CreatePermission.action).forEach((a) => {
      allpermissions = allpermissions.concat([{ target: t, action: a }]);
    });
  });
  UserGroupService.userGroupControllerGetPermissions(params.groupid).then(
    (val) => {
      val.directlyGranted.forEach((p) => {
        delete p.responseType;
        grantedPermissions = grantedPermissions.concat([p]);
      });
      grantedPermissions_initial = grantedPermissions;
    }
  );
</script>

{#await group_promise}
  <!--  -->
{:then user}
  <section class="container p-5 select-none">
    <div class="flex flex-row mb-4">
      <div class="w-full">
        <nav class="w-full flex">
          <ol class="list-none flex flex-row items-center justify-start">
            <li class="flex items-center">
              <svg
                class="flex-shrink-0 w-5 h-5 mr-2"
                fill="currentColor"
                width="24"
                height="24"
                xmlns="http://www.w3.org/2000/svg"
                viewBox="0 0 640 512"
                ><path
                  fill="currentColor"
                  d="M610.5 341.3c2.6-14.1 2.6-28.5 0-42.6l25.8-14.9c3-1.7 4.3-5.2 3.3-8.5-6.7-21.6-18.2-41.2-33.2-57.4-2.3-2.5-6-3.1-9-1.4l-25.8 14.9c-10.9-9.3-23.4-16.5-36.9-21.3v-29.8c0-3.4-2.4-6.4-5.7-7.1-22.3-5-45-4.8-66.2 0-3.3.7-5.7 3.7-5.7 7.1v29.8c-13.5 4.8-26 12-36.9 21.3l-25.8-14.9c-2.9-1.7-6.7-1.1-9 1.4-15 16.2-26.5 35.8-33.2 57.4-1 3.3.4 6.8 3.3 8.5l25.8 14.9c-2.6 14.1-2.6 28.5 0 42.6l-25.8 14.9c-3 1.7-4.3 5.2-3.3 8.5 6.7 21.6 18.2 41.1 33.2 57.4 2.3 2.5 6 3.1 9 1.4l25.8-14.9c10.9 9.3 23.4 16.5 36.9 21.3v29.8c0 3.4 2.4 6.4 5.7 7.1 22.3 5 45 4.8 66.2 0 3.3-.7 5.7-3.7 5.7-7.1v-29.8c13.5-4.8 26-12 36.9-21.3l25.8 14.9c2.9 1.7 6.7 1.1 9-1.4 15-16.2 26.5-35.8 33.2-57.4 1-3.3-.4-6.8-3.3-8.5l-25.8-14.9zM496 368.5c-26.8 0-48.5-21.8-48.5-48.5s21.8-48.5 48.5-48.5 48.5 21.8 48.5 48.5-21.7 48.5-48.5 48.5zM96 224c35.3 0 64-28.7 64-64s-28.7-64-64-64-64 28.7-64 64 28.7 64 64 64zm224 32c1.9 0 3.7-.5 5.6-.6 8.3-21.7 20.5-42.1 36.3-59.2 7.4-8 17.9-12.6 28.9-12.6 6.9 0 13.7 1.8 19.6 5.3l7.9 4.6c.8-.5 1.6-.9 2.4-1.4 7-14.6 11.2-30.8 11.2-48 0-61.9-50.1-112-112-112S208 82.1 208 144c0 61.9 50.1 112 112 112zm105.2 194.5c-2.3-1.2-4.6-2.6-6.8-3.9-8.2 4.8-15.3 9.8-27.5 9.8-10.9 0-21.4-4.6-28.9-12.6-18.3-19.8-32.3-43.9-40.2-69.6-10.7-34.5 24.9-49.7 25.8-50.3-.1-2.6-.1-5.2 0-7.8l-7.9-4.6c-3.8-2.2-7-5-9.8-8.1-3.3.2-6.5.6-9.8.6-24.6 0-47.6-6-68.5-16h-8.3C179.6 288 128 339.6 128 403.2V432c0 26.5 21.5 48 48 48h255.4c-3.7-6-6.2-12.8-6.2-20.3v-9.2zM173.1 274.6C161.5 263.1 145.6 256 128 256H64c-35.3 0-64 28.7-64 64v32c0 17.7 14.3 32 32 32h65.9c6.3-47.4 34.9-87.3 75.2-109.4z"
                /></svg
              >
            </li>
            <li class="flex items-center">
              <a class="mr-2" href="../../">{$_("user-groups")}</a><svg
                stroke="currentColor"
                fill="none"
                stroke-width="2"
                viewBox="0 0 24 24"
                stroke-linecap="round"
                stroke-linejoin="round"
                class="h-3 w-3 mr-2 stroke-current"
                height="1em"
                width="1em"
                xmlns="http://www.w3.org/2000/svg"
                ><line x1="5" y1="12" x2="19" y2="12" />
                <polyline points="12 5 19 12 12 19" /></svg
              >
            </li>
            <li class="flex items-center">
              <span class="mr-2"><a href="../">{original_data.name}</a></span>
            </li>
            <li class="flex items-center">
              <svg
                stroke="currentColor"
                fill="none"
                stroke-width="2"
                viewBox="0 0 24 24"
                stroke-linecap="round"
                stroke-linejoin="round"
                class="h-3 w-3 mr-2 stroke-current"
                height="1em"
                width="1em"
                xmlns="http://www.w3.org/2000/svg"
                ><line x1="5" y1="12" x2="19" y2="12" />
                <polyline points="12 5 19 12 12 19" /></svg
              >
            </li>
            <li class="flex items-center">
              <span class="mr-2">{$_("permissions")}</span>
            </li>
          </ol>
        </nav>
      </div>
    </div>
    <div class="mb-4 text-3xl font-extrabold">
      <div>
        {#if promises.length === 0}
          <button
            disabled={save_enabled}
            class:opacity-50={save_enabled}
            type="button"
            on:click={submit}
            class="w-full justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-blue-600 text-base font-medium text-white hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 sm:w-auto sm:text-sm mb-1 lg:mb-0"
            >{$_("save-changes")}</button
          >
        {:else}
          <button
            type="button"
            class="w-full justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-yellow-600 text-base font-medium text-white hover:bg-yellow-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-yellow-500 sm:w-auto sm:text-sm"
            >{$_("applying-changes")}</button
          >
        {/if}
      </div>
    </div>
    <!--  -->
    <div class="flex flex-wrap -mx-1 overflow-hidden">
      <div class="my-1 px-1 w-full overflow-hidden sm:w-1/2">
        {$_("available-permissions")}
      </div>
      <div class="my-1 px-1 w-full overflow-hidden sm:w-1/2">
        {$_("granted")}
      </div>
    </div>
    <!--  -->
    <div class="flex flex-wrap -mx-1 overflow-hidden">
      {#if allpermissions.length > 0}
        <div class="my-1 px-1 w-full overflow-hidden sm:w-1/2">
          <div
            class="border-4 border-dashed rounded mb-4 p-5 text-lg text-center"
          >
            {#each allpermissions as p}
              {#if !(grantedPermissions.filter((o) => p.target == o.target && p.action == o.action).length > 0)}
                <p
                  class="block w-full mt-1 text-sm bg-gray-200 p-2 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple form-input"
                >
                  {p.target + ":" + p.action}
                  <button
                    on:click={() => {
                      grantedPermissions = grantedPermissions.concat([p]);
                      if (to_delete.some((o) => o === p.id)) {
                        to_delete = to_delete.filter((o) => o !== p.id);
                      } else {
                        to_add = to_add.concat([
                          {
                            action: p.action,
                            target: p.target,
                            principal: original_data.id,
                          },
                        ]);
                      }
                    }}
                    type="button"
                    class="w-full justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-green-200 text-base font-medium text-black hover:bg-green-700 hover:text-white focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-green-500 sm:w-auto sm:text-sm"
                    >+</button
                  >
                </p>
              {/if}
            {/each}
          </div>
        </div>
        <div class="my-1 px-1 w-full overflow-hidden sm:w-1/2">
          <div
            class="border-4 border-dashed rounded mb-4 p-5 text-lg text-center"
          >
            {#each grantedPermissions as p}
              <p
                class="block w-full mt-1 text-sm bg-gray-200 p-2 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple form-input"
              >
                {p.target + ":" + p.action}
                <button
                  on:click={() => {
                    grantedPermissions = grantedPermissions.filter(
                      (o) =>
                        o.target + ":" + o.action !== p.target + ":" + p.action
                    );
                    if (
                      to_add.some(
                        (o) =>
                          o.target + ":" + o.action ===
                          p.target + ":" + p.action
                      )
                    ) {
                      to_add = to_add.filter(
                        (o) =>
                          o.target + ":" + o.action !==
                          p.target + ":" + p.action
                      );
                    } else {
                      to_delete = to_delete.concat([p.id]);
                    }
                  }}
                  type="button"
                  class="w-full justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-red-300 text-base font-medium text-black hover:bg-red-700 hover:text-white focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-gray-500 sm:w-auto sm:text-sm"
                  >-</button
                >
              </p>
            {/each}
          </div>
        </div>
      {/if}
    </div>
  </section>
{:catch error}
  <PromiseError {error} />
{/await}
