<!--
// Copyright © 2020 Anticrm Platform Contributors.
// 
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// 
// See the License for the specific language governing permissions and
// limitations under the License.
-->
<script lang="ts">
  import { createEventDispatcher } from 'svelte'

  export let checked: boolean = false
  export let symbol: 'check' | 'minus' = 'check'
  export let circle: boolean = false
  export let primary: boolean = false

  const dispatch = createEventDispatcher()

  const handleValueChanged = (event: Event) => {
    const eventTarget = event.target as HTMLInputElement
    const isChecked = eventTarget.checked

    dispatch('value', isChecked)
  }
</script>

<label class="checkbox" class:circle class:primary class:checked>
  <input class="chBox" type="checkbox" bind:checked on:change={handleValueChanged} />
  <svg class="checkSVG" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
    {#if checked}
      {#if symbol === 'minus'}
        <rect class="check" class:primary x="4" y="7.4" width="8" height="1.2" />
      {:else}
        <polygon class="check" class:primary points="7.3,11.5 4,8.3 5,7.4 7.3,9.7 11.8,5.1 12.7,6.1 " />
      {/if}
    {/if}
  </svg>
</label>

<style lang="scss">
  .checkbox {
    flex-shrink: 0;
    display: inline-flex;
    justify-content: center;
    align-items: center;
    width: 0.875rem;
    height: 0.875rem;
    border: 1px solid var(--dark-color);
    border-radius: 0.25rem;

    &.circle {
      width: 1rem;
      height: 1rem;
      border-radius: 50%;
    }
    &.checked {
      background-color: var(--accent-color);
      border-color: transparent;
    }
    &.primary.checked {
      background-color: var(--primary-bg-color);
      border-color: transparent;
    }

    .chBox {
      position: absolute;
      width: 1px;
      height: 1px;
      margin: -1px;
      border: 0;
      padding: 0;
      clip: rect(0 0 0 0);
      overflow: hidden;

      &:checked + .checkSVG {
        & .check {
          visibility: visible;
          fill: var(--theme-button-bg-enabled);
          &.primary {
            fill: var(--primary-button-color);
          }
        }
      }
      &:not(:disabled) + .checkSVG {
        cursor: pointer;
      }
      &:disabled + .checkSVG {
        filter: grayscale(70%);
      }
    }
    .checkSVG {
      width: 0.875rem;
      height: 0.875rem;

      .check {
        visibility: hidden;
        fill: var(--theme-button-bg-enabled);
      }
    }
  }
</style>
