<script>
  import { store } from "builderStore"
  import { tables } from "stores/backend"
  import { ModalContent, Body, Detail, Layout, Icon } from "@budibase/bbui"
  import getTemplates from "builderStore/store/screenTemplates"

  export let screenNameModal
  export let navigationSelectionModal
  export let selectedScreens = []

  const blankScreen = "createFromScratch"
  $: blankSelected = selectedScreens.find(x => x.id === blankScreen)
  $: autoSelected = selectedScreens.length > 0 && !blankSelected

  $: templates = getTemplates($store, $tables.list)
  const toggleScreenSelection = table => {
    if (selectedScreens.find(s => s.name.includes(table.name))) {
      selectedScreens = selectedScreens.filter(
        screen => !screen.name.includes(table.name)
      )
    } else {
      const templates = getTemplates($store, $tables.list).filter(template =>
        template.name.includes(table.name)
      )
      selectedScreens = [...templates, ...selectedScreens]
    }
  }
</script>

<ModalContent
  title="Add screens"
  confirmText="Add Screens"
  cancelText="Cancel"
  onConfirm={() =>
    autoSelected ? navigationSelectionModal.show() : screenNameModal.show()}
  disabled={!selectedScreens.length}
  size="L"
>
  <Body size="XS"
    >Please select the screens you would like to add to your application.
    Autogenerated screens come with CRUD functionality.</Body
  >

  <Layout noPadding>
    <Detail size="S">Blank screen</Detail>
    <div
      class="item"
      class:selected={selectedScreens.find(x => x.id.includes(blankScreen))}
      on:click={() =>
        toggleScreenSelection(templates.find(t => t.id === blankScreen))}
      class:disabled={autoSelected}
    >
      <div class="content">
        <Body size="S">Blank</Body>
      </div>
      <div style="color: var(--spectrum-global-color-green-600); float: right">
        {#if selectedScreens.find(x => x.id === blankScreen)}
          <Icon size="S" name="CheckmarkCircleOutline" />
        {/if}
      </div>
    </div>
    <Detail size="S">Autogenerated Screens</Detail>

    {#each $tables.list.filter(table => table.type !== "external") as table}
      <div
        class:disabled={blankSelected}
        class:selected={selectedScreens.find(x => x.name.includes(table.name))}
        on:click={() => toggleScreenSelection(table)}
        class="item"
      >
        <div class="content">
          {table.name}
        </div>
        <div
          style="color: var(--spectrum-global-color-green-600); float: right"
        >
          {#if selectedScreens.find(x => x.name.includes(table.name))}
            <Icon size="S" name="CheckmarkCircleOutline" />
          {/if}
        </div>
      </div>
    {/each}
  </Layout>
</ModalContent>

<style>
  .disabled {
    opacity: 0.3;
    pointer-events: none;
  }

  .content {
    padding-left: 10px;
    letter-spacing: 0px;
    color: #2c2c2c;
  }
  .item {
    cursor: pointer;
    grid-gap: var(--spectrum-alias-grid-margin-xsmall);
    padding: var(--spectrum-alias-item-padding-s);
    background: var(--background);
    transition: 0.3s all;
    border: solid var(--spectrum-alias-border-color);
    border-radius: 2px;
    box-sizing: border-box;
    border-width: 1px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 60px;
  }

  .item:hover,
  .selected {
    background: var(--spectrum-alias-background-color-tertiary);
  }
</style>
