<!--
// Copyright © 2021, 2022 Hardcore Engineering Inc.
// 
// 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'
  import attachment from '@hcengineering/attachment'
  import { AnySvelteComponent, IconSize, showPopup } from '@hcengineering/ui'
  import { AvatarType } from '@hcengineering/contact'
  import { Asset, getResource } from '@hcengineering/platform'

  import AvatarComponent from './Avatar.svelte'
  import SelectAvatarPopup from './SelectAvatarPopup.svelte'

  export let avatar: string | null | undefined
  export let email: string | undefined = undefined
  export let id: string
  export let size: IconSize
  export let direct: Blob | undefined = undefined
  export let icon: Asset | AnySvelteComponent | undefined = undefined
  export let disabled: boolean = false

  const [schema, uri] = avatar?.split('://') || []

  let selectedAvatarType: AvatarType | undefined = avatar?.includes('://') ? (schema as AvatarType) : AvatarType.IMAGE
  let selectedAvatar: string | null | undefined = selectedAvatarType === AvatarType.IMAGE ? avatar : uri

  export async function createAvatar (): Promise<string | undefined> {
    if (selectedAvatarType === AvatarType.IMAGE && direct !== undefined) {
      const uploadFile = await getResource(attachment.helper.UploadFile)
      const file = new File([direct], 'avatar')

      return await uploadFile(file)
    }
    if (selectedAvatarType && selectedAvatar) {
      return `${selectedAvatarType}://${selectedAvatar}`
    }
  }

  export async function removeAvatar (avatar: string) {
    if (!avatar.includes('://')) {
      const deleteFile = await getResource(attachment.helper.DeleteFile)
      await deleteFile(avatar)
    }
  }

  function handlePopupSubmit (submittedAvatarType?: AvatarType, submittedAvatar?: string, submittedDirect?: Blob) {
    selectedAvatarType = submittedAvatarType
    selectedAvatar = submittedAvatar
    direct = submittedDirect
    dispatch('done')
  }
  const dispatch = createEventDispatcher()

  async function showSelectionPopup (e: MouseEvent) {
    if (!disabled) {
      showPopup(SelectAvatarPopup, { avatar, email, id, file: direct, icon, onSubmit: handlePopupSubmit })
    }
  }
</script>

<div class="cursor-pointer" on:click|self={showSelectionPopup}>
  <AvatarComponent
    avatar={selectedAvatarType === AvatarType.IMAGE ? selectedAvatar : `${selectedAvatarType}://${selectedAvatar}`}
    {direct}
    {size}
    {icon}
  />
</div>
