<div
    x-cloak
    x-data="dropzone({
        _this: @this,
        uuid: @js($uuid),
        multiple: @js($multiple),
    })"
    @dragenter.prevent="onDragenter($event)"
    @dragleave.prevent="onDragleave($event)"
    @dragover.prevent="onDragover($event)"
    @drop.prevent="onDrop($event,$event.target.closest('[id]').id)"
    class="w-full antialiased"
>
    <div class="flex flex-col items-start h-full w-full justify-center bg-white dark:bg-gray-800 dark:border-gray-600 dark:hover:border-gray-500">
        @if(! is_null($error))
            <div class="bg-red-50 p-4 w-full mb-4 rounded dark:bg-red-600">
                <div class="flex gap-3 items-start">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="w-5 h-5 text-red-400 dark:text-red-200">
                        <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.28 7.22a.75.75 0 00-1.06 1.06L8.94 10l-1.72 1.72a.75.75 0 101.06 1.06L10 11.06l1.72 1.72a.75.75 0 101.06-1.06L11.06 10l1.72-1.72a.75.75 0 00-1.06-1.06L10 8.94 8.28 7.22z" clip-rule="evenodd" />
                    </svg>
                    <h3 class="text-sm text-red-800 font-medium dark:text-red-100">{{ $error }}</h3>
                </div>
            </div>
        @endif
        <div class="flex justify-between w-full">
{{--            <label for="upload" class="font-medium text-lg mb-2 text-black dark:text-white">{{ __('Upload files') }} <span class="text-red-500">*</span></label>--}}
            <div x-show="isLoading" role="status">
                <svg aria-hidden="true" class="w-5 h-5 text-gray-200 animate-spin dark:text-gray-700 fill-blue-600" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/>
                    <path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"/>
                </svg>
                <span class="sr-only">Loading...</span>
            </div>
        </div>
        <div @click="$refs.input.click()" class="border-2 border-dashed border-gray-300 rounded w-full cursor-pointer bg-gray-50 dark:bg-gray-700 text-gray-500 dark:text-gray-400">
            <div>
                <div x-show="!isDragging" class="flex flex-col items-center justify-center gap-2 py-8 h-full ">
                    <div class="flex items-center justify-center gap-2">
{{--                        TODO change depending on the file type props etc--}}
                        <i class="@php
                                        echo match ($type){
                                            'pivot','span' => 'fg-geojson-file fg-3x',
                                            'harvest' => 'fg-map-stat fg-3x',
                                            default => 'fa-solid fa-file-arrow-down'
                                        }
                                  @endphp  fill-red-100"></i>
                        <p class="text-md ">Drop here or <span class="font-semibold text-black dark:text-white">Browse file </span>
                        </p>
                    </div>
                    <div class="flex items-center justify-center gap-2 text-gray-400 text-sm">
                        @php
                            $hasMaxFileSize = ! is_null($this->maxFileSize);
                            $hasMimes = ! empty($this->mimes);
                        @endphp

                        @if($hasMaxFileSize)
                            <p>{{ __('Up to :size', ['size' => \Illuminate\Support\Number::fileSize($this->maxFileSize * 1024)]) }}</p>
                        @endif

                        @if($hasMaxFileSize && $hasMimes)
                            <span class="w-1 h-1 text-gray-400">·</span>
                        @endif

                        @if($hasMimes)
                            <p>{{ Str::upper($this->mimes) }}</p>
                        @endif
                    </div>
                </div>
                <div x-show="isDragging" class="flex flex-col items-center bg-gray-100 justify-center py-8 h-full text-gray-600 dark:text-gray-400">
                    <div class="flex items-center justify-center gap-2">
                        <i class="fa-solid fa-download"></i>
                        <p class="text-md ">Drop here to upload</p>
                    </div>
                    <div class="flex items-center justify-center gap-2 text-gray-400 text-sm">
                        @php
                            $hasMaxFileSize = ! is_null($this->maxFileSize);
                            $hasMimes = ! empty($this->mimes);
                        @endphp

                        @if($hasMaxFileSize)
                            <p>{{ __('Up to :size', ['size' => \Illuminate\Support\Number::fileSize($this->maxFileSize * 1024)]) }}</p>
                        @endif

                        @if($hasMaxFileSize && $hasMimes)
                            <span class="w-1 h-1 text-gray-400">·</span>
                        @endif

                        @if($hasMimes)
                            <p>{{ Str::upper($this->mimes) }}</p>
                        @endif
                    </div>
                </div>
            </div>
            <input
                    x-ref="input"
                    wire:model="upload"
                    type="file"
                    class="hidden"
                    x-on:livewire-upload-start="isLoading = true"
                    x-on:livewire-upload-finish="isLoading = false"
                    x-on:livewire-upload-error="console.log('livewire-dropzone upload error', error)"
                    @if(! is_null($this->accept)) accept="{{ $this->accept }}" @endif
                    @if($multiple === true) multiple @endif
            >
        </div>
        @if(count($files) > 0)
            <div class="flex flex-wrap gap-x-10 gap-y-2 justify-start w-full mt-5">
                @foreach($files as $file)
                    <div class="flex items-center justify-between gap-2 border rounded border-gray-200 w-full h-auto overflow-hidden dark:border-gray-700">
                        <div class="flex items-center gap-3">
                            @if($this->isImageMime($file['extension']))
                                <div class="flex-none w-14 h-14">
                                    <img src="{{ $file['temporaryUrl'] }}" class="object-fill w-full h-full" alt="{{ $file['name'] }}">
                                </div>
                            @else
                                <div class="flex justify-center items-center w-14 h-14 bg-gray-100 dark:bg-gray-700">
                                    <i class="fa-regular fa-file fa-2xl text-gray-500"></i>
                                </div>
                            @endif
                            <div class="flex flex-col items-start gap-1">
                                <div class="text-center text-slate-900 text-sm font-medium dark:text-slate-100">{{ $file['name'] }}</div>
                                <div class="text-center text-gray-500 text-sm font-medium">{{ \Illuminate\Support\Number::fileSize($file['size']) }}</div>
                            </div>
                        </div>
                        <div class="flex items-center mr-3">
                            <button type="button" x-on:click="removeUpload('{{ $file['tmpFilename'] }}','{{$type}}');">
                                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="w-6 h-6 text-black dark:text-white">
                                    <path fill-rule="evenodd" d="M5.47 5.47a.75.75 0 011.06 0L12 10.94l5.47-5.47a.75.75 0 111.06 1.06L13.06 12l5.47 5.47a.75.75 0 11-1.06 1.06L12 13.06l-5.47 5.47a.75.75 0 01-1.06-1.06L10.94 12 5.47 6.53a.75.75 0 010-1.06z" clip-rule="evenodd" />
                                </svg>
                            </button>
                        </div>
                    </div>
                @endforeach
            </div>
        @endif
    </div>

    @script
    <script>
        Alpine.data('dropzone', ({ _this, uuid, multiple }) => {
            const process = {
                isDragging: false,
                isDropped: false,
                isLoading: false,

                onDrop(e,t) {
                    this.isDropped = true;
                    this.isDragging = false;

                    const file = multiple ? e.dataTransfer.files : e.dataTransfer.files[0];
                    const args = ['upload', file, () => {
                        // Upload completed
                        this.isLoading = false;
                        this.processFile(file,t);
                    }, (error) => {
                        // An error occurred while uploading
                        console.log('livewire-dropzone upload error', error);
                    }, () => {
                        // Uploading is in progress
                        this.isLoading = true
                    }];

                    // Upload file(s)
                    multiple ? _this.uploadMultiple(...args) : _this.upload(...args);
                },
                onDragenter() {
                    if(!this.isDragging){
                        this.isDragging = true;
                    }
                },
                onDragleave() {
                    if(this.isDragging) {
                        this.isDragging = false;
                    }
                },
                onDragover() {
                    if(!this.isDragging) {
                        this.isDragging = true;
                    }
                },
                removeUpload(tmpFilename,type) {
                    // Dispatch an event to remove the temporarily uploaded file
                    switch (type){
                        case 'pivot':
                            window.pivotLayer.clearLayers();
                            break;
                        case 'span':
                            window.spanLayer.clearLayers();
                            break;
                        default:
                            break;
                    }
                    window.geoJsonMap.fitBounds(window.groupLayer.getBounds());
                    window.map.focus();
                    _this.dispatch(uuid + ':fileRemoved', { tmpFilename })
                }
            };
            return (process);
        })
    </script>
    @endscript
</div>
