<?php

namespace App\Http\Livewire\Posts;

use App\Models\Bank;
use App\Models\Organization;
use App\Models\User;
use Illuminate\Support\Facades\Storage;
use Livewire\Component;

class SelectOrganizer extends Component
{
    public $search;
    public $searchResults = [];
    public $showDropdown = false;
    public $selectedId;
    public $selected = [];

    protected $listeners = [
        'resetForm',
        'organizerExists'
    ];


    public function inputBlur()
    {
        $this->dispatch('toAccountValidation');        
        $this->showDropdown = false;
        $this->search = '';
    }

    public function resetForm()
    {
        $this->reset();
    }
    
    /**
     * Populate dropdown when already meeting data exists
     *
     * @param  mixed $value
     * @return void
     */
    public function organizerExists($value)
    {
        $this->selectedId = $value['meetingable_id'];
        $this->selected['id'] = $value['meetingable_id'];
        $this->selected['type'] = $value['meetingable_type'];
        if ($value['meetingable_type'] == User::class) {
            $organizer = User::where('id', $value['meetingable_id'])->select('name', 'profile_photo_path')->firstOrFail();
            $description = '';
        } elseif ($value['meetingable_type'] == Bank::class) {
            $organizer = Bank::where('id', $value['meetingable_id'])->select('name', 'profile_photo_path')->firstOrFail();
            $description = __('Bank');
        } else {
            $organizer = Organization::where('id', $value['meetingable_id'])->select('name', 'profile_photo_path')->firstOrFail();
            $description = __('Organization');
        }
        $this->selected['name'] = $organizer->name;
        $this->selected['profile_photo_path'] = url(Storage::url($organizer->profile_photo_path));
        $this->selected['description'] = $description;
    }


    public function orgSelected($value)
    {
        $this->selectedId = $value;
        $this->selected = collect($this->searchResults)->where('id', '=', $value)->first();
        $this->showDropdown = false;
        $this->search = '';
        $this->dispatch('organizerSelected', $this->selected);
    }


    /**
     * updatedSearch: Search query users and organizations
     *
     * @param  mixed $newValue
     * @return void
     */
    public function updatedSearch($newValue)
    {
        $this->showDropdown = true;
        $search = $this->search;
        $users = User::where('name', 'like', '%' . $search . '%')
            ->where('id', '!=', '1')        // Exclude Super-Admin user  //TODO: exclude all admin users by role
            ->select('id', 'name', 'profile_photo_path')
            ->get()
            ->map(function ($item) {
                return [
                    'id' => $item['id'],
                    'type' => User::class,
                    'name' => $item['name'],
                    'description' => '',
                    'profile_photo_path' => url('/storage/' . $item['profile_photo_path'])
                ];
            });
        $organizations = Organization::where('name', 'like', '%' . $search . '%')
            ->select('id', 'name', 'profile_photo_path')
            ->get()
            ->map(function ($item) {
                return [
                    'id' => $item['id'],
                    'type' => Organization::class,
                    'name' => $item['name'],
                    'description' => __('Organization'),
                    'profile_photo_path' => url(Storage::url($item['profile_photo_path']))
                ];
            });
        $banks = Bank::where('name', 'like', '%' . $search . '%')
            ->select('id', 'name', 'profile_photo_path')
            ->get()
            ->map(function ($item) {
                return [
                    'id' => $item['id'],
                    'type' => Bank::class,
                    'name' => $item['name'],
                    'description' => __('Bank'),
                    'profile_photo_path' => url(Storage::url($item['profile_photo_path']))
                ];
            });
        $merged = $users->concat($organizations)->concat($banks);
        $response = $merged->take(6);
        $this->searchResults = $response->toArray();
    }

    public function removeSelectedProfile()
    {            
        $this->selectedId = null;
        $this->selected = [];
        $this->dispatch('organizerSelected', $this->selected);
    }


    public function render()
    {
        return view('livewire.posts.select-organizer');
    }
}
