<?php

namespace App\Http\Livewire\Traits;

use App\Models\AllegatiGalleryType;
use App\Models\ReportAllegatiGallery;
use Livewire\WithFileUploads;

trait HasAllegato
{
    public $allegatoId = 0;
    public $allegatoType = 0;
    public $allegatoGallery = 0;
    public $allegatoName = '';
    public $allegatoVisible = false;
    public $allegatoFiles = '';
    public $allegatiFiles = [];
    public $allegatiImmagini;
    public $allegatiDocumenti;
    public $allegati = [];

    protected $imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg', 'webp'];

    public function getAllegatoType($type)
    {
        if ($type > 0) {
            $ret = AllegatiGalleryType::findOrFail($type);
            return $ret->name;
        }
        return "";
    }

    public function addAllegato($type)
    {
        $this->allegatoId = 0;
        $this->allegatoType = $type;
        $this->allegatoGallery = 0;
        $this->allegatoName = '';
        $this->allegatoVisible = false;
        $this->allegatoFiles = '';
        $this->allegatiFiles = [];
        $this->emit('attachments', "");
    }

    public function saveAllegato()
    {
        if ($this->allegatoGallery == 1) {
            foreach ($this->allegatiFiles as $file) {
                $extension = pathinfo($file, PATHINFO_EXTENSION);
                if (!in_array(strtolower($extension), $this->imageExtensions)) {
                    session()->flash('error', 'I File devono essere immagini per Tipologia: foto sinistro');
                    return;
                }
            }
        }

        $files = implode("|", $this->allegatiFiles);

        if ($this->allegatoId > 0) {
            ReportAllegatiGallery::where('id', $this->allegatoId)->update([
                'file_type' => $this->allegatoType,
                'gallery_type' => $this->allegatoGallery,
                'name' => $this->allegatoName,
                'is_visible' => $this->allegatoVisible,
                'files' => $files,
                'state' => 0
            ]);
        } else {
            ReportAllegatiGallery::create([
                'report_id' => $this->dataId,
                'file_type' => $this->allegatoType,
                'gallery_type' => $this->allegatoGallery,
                'name' => $this->allegatoName,
                'is_visible' => $this->allegatoVisible,
                'files' => $files,
                'state' => 0,
                'created' => date("Y-m-d H:i:s"),
                'created_by' => 0
            ]);
        }

        $this->resetAllegatoFields();
        $this->refreshAllegatiCollections();
        $this->emit('close-modal');
    }

    private function resetAllegatoFields()
    {
        $this->allegatoId = 0;
        $this->allegatoGallery = 0;
        $this->allegatoName = '';
        $this->allegatoVisible = false;
        $this->allegatoFiles = '';
        $this->allegatiFiles = [];
        $this->allegatoType = 0;
    }

    private function refreshAllegatiCollections()
    {
        $this->allegatiImmagini = ReportAllegatiGallery::where('report_id', $this->dataId)
            ->where('file_type', 0)
            ->orderBy('name')
            ->get();

        $this->allegatiDocumenti = ReportAllegatiGallery::where('report_id', $this->dataId)
            ->where('file_type', 1)
            ->orderBy('name')
            ->get();
    }

    public function editAllegato($id)
    {
        $this->emit('attachments', "");
        $a = ReportAllegatiGallery::where('id', $id)->first();

        if ($a != null) {
            $this->allegatoId = $id;
            $this->allegatoType = $a->file_type;
            $this->allegatoGallery = $a->gallery_type;
            $this->allegatoName = $a->name;
            $this->allegatoFiles = $a->files;
            $this->allegatiFiles = explode("|", $this->allegatoFiles);
            $this->emit('attachments', $this->allegatoFiles);
        }
    }

    public function removeAllegato($id)
    {
        ReportAllegatiGallery::findOrFail($id)->delete();
        $this->refreshAllegatiCollections();
    }

    public function updatedAllegati()
    {
        foreach ($this->allegati as $allegato) {
            $name = $allegato->getClientOriginalName();
            $allegato->storeAs('', $name, 'public');
            $this->allegatiFiles[] = $name;
        }
        $this->emit('attachments', implode("|", $this->allegatiFiles));
        $this->allegati = [];
    }
}
