<template>
  <transition-group
    tag="ul"
    :class="[
      'ps-upload-list',
      'ps-upload-list--' + listType,
      { 'is-disabled': disabled }
    ]"
    name="ps-list"
  >
    <li
      v-for="file in files"
      :key="file"
      :class="['ps-upload-list__item', 'is-' + file.status, focusing ? 'focusing' : '']"
      tabindex="0"
      @keydown.delete="!disabled && handleRemove($event, file)"
      @focus="focusing = true"
      @blur="focusing = false"
      @click="onFileClicked"
    >
      <slot :file="file">
        <img
          v-if="file.status !== 'uploading' && ['picture-card', 'picture'].includes(listType)"
          class="ps-upload-list__item-thumbnail"
          :src="file.url"
          alt=""
        >
        <a class="ps-upload-list__item-name" @click="handleClick(file)">
          <i class="ps-icon-document"></i>{{ file.name }}
        </a>
        <label class="ps-upload-list__item-status-label">
          <i
            :class="{
              'ps-icon-upload-success': true,
              'ps-icon-circle-check': listType === 'text',
              'ps-icon-check': ['picture-card', 'picture'].includes(listType)
            }"
          ></i>
        </label>
        <i v-if="!disabled" class="ps-icon-close" @click="handleRemove($event, file)"></i>
        <!-- Due to close btn only appears when li gets focused disappears after li gets blurred, thus keyboard navigation can never reach close btn-->
        <!-- This is a bug which needs to be fixed -->
        <!-- TODO: Fix the incorrect navigation interaction -->
        <i v-if="!disabled" class="ps-icon-close-tip">{{ t('ps.upload.deleteTip') }}</i>
        <ps-progress
          v-if="file.status === 'uploading'"
          :type="listType === 'picture-card' ? 'circle' : 'line'"
          :stroke-width="listType === 'picture-card' ? 6 : 2"
          :percentage="parsePercentage(file.percentage)"
        />
        <span v-if="listType === 'picture-card'" class="ps-upload-list__item-actions">
          <span
            class="ps-upload-list__item-preview"
            @click="handlePreview(file)"
          >
            <i class="ps-icon-zoom-in"></i>
          </span>
          <span
            v-if="!disabled"
            class="ps-upload-list__item-delete"
            @click="handleRemove($event, file)"
          >
            <i class="ps-icon-delete"></i>
          </span>
        </span>
      </slot>
    </li>
  </transition-group>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue'
import { NOOP } from '@vue/shared'

import { t } from '@present-ui/locale'
import PsProgress from '@present-ui/progress'

import type { PropType } from 'vue'

export default defineComponent({
  name: 'PsUploadList',
  components: { PsProgress },
  props: {
    files: {
      type: Array as PropType<File[]>,
      default: () => [] as File[],
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    handlePreview: {
      type: Function as PropType<(file: File) => void>,
      default: () => NOOP,
    },
    listType: {
      type: String as PropType<'picture' | 'picture-card' | 'text'>,
      default: 'text',
    },
  },
  emits: ['remove'],
  setup(props, { emit }) {
    const parsePercentage = (val: string) => {
      return parseInt(val, 10)
    }

    const handleClick = (file: File) => {
      props.handlePreview(file)
    }

    const onFileClicked = (e: Event) => {
      (e.target as HTMLElement).focus()
    }

    const handleRemove = (e: Event, file: File) => {
      emit('remove', file)
    }
    return {
      focusing: ref(false),
      parsePercentage,
      handleClick,
      handleRemove,
      onFileClicked,
      t,
    }
  },
})
</script>
