<template>
  <div
    class="left-panel"
    id="leftPanel"
    :class="{ closed: !isOpen, mobile: isMobileDevice }"
    :style="{ height: isHeaderDisabled || isMobileDevice ? '100%' : 'calc(100% - 44px)'}"
    >
    <div class="close-button" v-if="isMobileDevice"><Close @click="closePanel" /></div>
    <div class="sidebar-tabs-header">
      <LeftPanelTabs :activePanelTab="activePanelTab" />
    </div>
    <div class="sidebar-content">
      <div class="thumbnail-container" :class="{ hidden: activePanelTab !== 'THUMBS' }">
        <h2>{{ $t('leftPanel.thumbnails') }}</h2>
        <div class="thumbnail-view"></div>
      </div>
      <div class="annotation-container" :class="{ hidden: activePanelTab !== 'ANNOTATION' }">
        <AnnotationHeader />
        <AnnotationContent />
      </div>
      <OutlinesPanel v-show="activePanelTab === 'OUTLINE'" />
      <div class="layer-view-container" :class="{ hidden: activePanelTab !== 'LAYER' }">
        <h2>{{ $t('leftPanel.layers') }}</h2>
        <div class="layer-view"></div>
      </div>
      <div class="compare-container" :class="{ hidden: activePanelTab !== 'COMPARISON' }">
        <Compare />
      </div>
      <div class="signature-container" :class="{ hidden: activePanelTab !== 'SIGNATURE' }">
        <SignatureContainer />
      </div>
    </div>
    <div class="findbar-container" :class="{ hidden: activePanelTab !== 'SEARCH' }">
      <SearchContainer />
    </div>
  </div>
</template>

<script setup>
  import { computed, watch } from 'vue'
  import { useViewerStore } from '@/stores/modules/viewer'
  import { useDocumentStore } from '@/stores/modules/document'
  import { isMobileDevice } from '@/helpers/device'

  const { isHeaderDisabled } = defineProps(['isHeaderDisabled'])
  const useViewer = useViewerStore()
  const useDocument = useDocumentStore()

  const activePanelTab = computed(() => useViewer.getActiveElementTab('leftPanel'))
  const isOpen = computed(() => useViewer.isElementOpen('leftPanel'))
  const activeTool = computed(() => useDocument.getActiveTool)

  watch(isOpen, (newValue, oldValue) => {
    if (newValue && activeTool.value === 'cropPage') {
      useDocument.setToolState('')
    }
  })

  const closePanel = () => {
    useViewer.closeElement('leftPanel')
  }
</script>

<style lang="scss">
  .left-panel {
    position: fixed;
    left: 0;
    z-index: 4;
    display: flex;
    flex-direction: column;
    height: calc(100% - 44px);
    overflow: hidden;
    transition: transform .3s ease-in-out;
    background-color: var(--c-side-bg);
    border-right: 1px solid var(--c-side-header-border);
    &.closed {
      transform: translateX(-100%);
    }
    a {
      text-decoration: none;
    }
    .sidebar-tabs-header {
      display: flex;
      align-items: center;
      background-color: var(--c-side-header-bg);
      border-bottom: 1px solid var(--c-side-header-border);
      .button {
        flex: 1;
        margin: 0;
        padding: 12px 0;
        border-radius: initial;
        color: var(--c-side-header-text);
        &:not(.disabled):hover {
          background-color: var(--c-side-header-bg);
        }
        &:not(.disabled).active {
          background-color: var(--c-side-header-active);
        }
      }
    }
    .compare-container {
      padding: 16px 16px 0;
    }
    .sidebar-content {
      width: 100%;
      flex: 1;
      overflow: hidden;
      .hidden {
        display: none;
      }
    }
    h2 {
      margin: 0;
      padding: 9px 16px;
      line-height: 17px;
      font-size: 14px;
      color: var(--c-side-title);
    }
    .thumbnail-container {
      position: relative;
      width: 100%;
      height: 100%;
    }
    .thumbnail-view {
      position: relative;
      padding: 4px 4px 0;
      overflow: auto;
      width: 100%;
      height: calc(100% - 35px);
      -webkit-user-select: none;
      -moz-user-select: none;
      -ms-user-select: none;
      user-select: none;
    }
    .thumbnail  {
      display: flex;
      flex-direction: column;
      align-items: center;
      margin: 20px 0 40px;
      .box-thumbnail {
        margin-top: 8px;
        font-size: 12px;
        line-height: 19px;
        width: 24px;
        height: 18px;
        color: var(--c-side-text);
        text-align: center;
      }
      &.selected {
        .thumbnailSelectionRing {
          border: 1px solid var(--c-side-thumbnails-active);
        }
        .box-thumbnail {
          color: rgb(255, 255, 255);
          background-color: var(--c-side-thumbnails-active);
        }
      }
    }
    .thumbnailSelectionRing {
      margin: 0 auto;
      border: 1px solid var(--c-black-5);
    }
    .no-outline, .no-layer {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, 50%);
      text-align: center;
      font-weight: 700;
      color: var(--c-side-text);
      font-size: 14px;
      line-height: 16px;
    }
    .outline-view-container, .annotation-container, .layer-view-container, .findbar-container {
      height: calc(100% - 41px);
    }
    .outline-view, .layer-view {
      position: relative;
      height: calc(100% - 35px);
      overflow: auto;
      .treeItem > .treeItems {
        margin-left: 24px;
      }
      .treeItem {
        .title {
          display: flex;
          align-items: center;
          padding: 8px 16px;
          color: var(--c-side-outline-text);
          font-size: 14px;
          line-height: 16px;
          .treeItemToggler {
            display: flex;
            align-items: center;
            margin-right: 8px;
            transition: transform .1s ease;
          }
          &.treeItemsHidden > .treeItemToggler {
            transform: rotate(0deg);
          }
          > .treeItemToggler {
            transform: rotate(90deg);
          }
          &.treeItemsHidden ~ .treeItems {
            display: none;
          }
          a {
            color: var(--c-side-outline-text);
          }
          &.selected {
            background-color: var(--c-side-outline-bg-active);
          }
        }
        & > a {
          display: block;
          width: 100%;
          padding: 8px 16px;
          color: var(--c-side-outline-text);
          font-size: 14px;
          line-height: 16px;
          & > label {
            display: flex;
            align-items: center;
            > input {
              margin: 3px 4px 0;
            }
          }
        }
      }
    }
    .layer-view {
      .toggle {
        display: flex;
        align-items: center;
        padding: 8px 16px;
        font-size: 14px;
        line-height: 16px;
        color: var(--c-side-outline-text);
        cursor: pointer;
        svg {
          display: none;
          margin-right: 8px;
        }
        .not-checked {
          display: block;
          color: var(--c-side-layers-not-checked-border);
          fill: var(--c-side-layers-not-checked-bg);
        }
        &.selected {
          .not-checked {
            display: none;
          }
          .checked {
            display: block;
            color: var(--c-side-layers-checked-bg);
          }
        }
      }
      .treeItem {
        margin-left: 8px;
      }
      .treeItem > a {
        display: flex;
        align-items: center;
        cursor: pointer;
        > label > input {
          opacity: 0;
          display: none;
        }
        svg {
          display: none;
          margin-right: 8px;
        }
        .not-checked {
          display: block;
          color: var(--c-side-layers-not-checked-border);
          fill: var(--c-side-layers-not-checked-bg);
        }
        &.checked {
          .checked {
            display: block;
            color: var(--c-side-layers-checked-bg);
          }
          .not-checked {
            display: none;
          }
        }
      }
    }
  }

  .left-panel.mobile {
    background-color: var(--c-side-header-bg);
    border-right: none;
    top: 0;
    height: 100%;

    &.closed {
      transform: translateY(100%);
    }

    .close-button {
      padding: 14px 14px 0 0;
      display: flex;
      align-items: center;
      justify-content: flex-end;
    }

    .sidebar-tabs-header {
      margin: 18px 16px 16px;
      border: 1px solid var(--c-side-header-border);
      border-radius: 4px;

      .button {
        padding: 6px 0;

        & + .button {
          border-left: 1px solid var(--c-side-header-border);
        }
      }
    }
  }

  @media screen and (max-width: 640px) {
    .left-panel {
      width: 100vw;
    }
  }
  @media screen and (min-width: 641px) {
    .left-panel {
      width: 260px;
      .thumbnail-view {
        padding: 0 16px;
      }
    }
  }
</style>
