<?php

namespace App\Http\Livewire\Profile;

use App\Models\Account;
use App\Models\Tag;
use App\Models\User;
use App\Traits\ActiveStatesTrait;
use App\Traits\LocationTrait;
use App\Traits\ProfileTrait;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Livewire\Component;
use Spatie\Activitylog\Models\Activity;

class Show extends Component
{
    use LocationTrait;
    use ActiveStatesTrait;
    use ProfileTrait;

    public $profile;
    public $type;
    public $inactive = false;
    public bool $hidden = false;
    public bool $inactiveLabel = false;
    public string $inactiveSince;
    public bool $emailUnverifiedLabel = false;
    public bool $isIncomplete = false;
    public bool $incompleteLabel = false;
    public bool $noExchangesYetLabel = false;
    public string $removedSince;
    public $showAboutFullText = false;
    public $age;
    public $location = [];
    public $friend;
    public $pendingFriend;
    public $phone;
    public $languagesWithCompetences = [];
    public $skills;
    public $lastLoginAt;
    public $lastExchangeAt;
    public $registeredSince;
    public $onlineStatus;
    public $accountsTotals;
    public $socials;

    /**
     * The mount method is called when the component is mounted.
     *
     */
    public function mount()
    {
        $this->type = strtolower(class_basename($this->profile));
        // $this->onlineStatus = $this->getOnlineStatus();
        $this->location = $this->getLocation($this->profile);
        $this->age = $this->getAge($this->profile) ? ' (' .  $this->getAge($this->profile) . ')' : '';
        $this->phone = $this->getPhone($this->profile);
        $this->lastLoginAt = $this->getLastLogin($this->profile);
        $this->accountsTotals = $this->getAccountsTotals($this->profile);
        $this->lastExchangeAt = $this->getLastExchangeAt($this->accountsTotals, $this->profile);
        $this->registeredSince = $this->getRegisteredSince($this->profile);
        $this->profile->languages = $this->getLanguages($this->profile);
        $this->profile->lang_preference = $this->getLangPreference($this->profile);
        $this->skills = $this->getSkills($this->profile);
        $this->socials = $this->getSocials($this->profile);
    }


    public function toggleAboutText()
    {
        $this->showAboutFullText = !$this->showAboutFullText;
    }


    /**
     * Redirects to the payment page to this user.
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function payButton()
    {
        if ($this->profile->isRemoved()) {
            return;
        }
        return redirect()->route('pay-to-name', ['name' => $this->profile->name]);
    }


    /**
     *  Start a conversation with this user.
     */
    public function createConversation()
    {
        if ($this->profile->isRemoved()) {
            return ;
        }

        $recipient = $this->profile;
        $conversation = getActiveProfile()->createConversationWith($recipient);

        return redirect()->route('chat', ['conversation' => $conversation->id]);
    }


    /**
     * Render the view for the ProfileUser Show component.
     *
     * @return \Illuminate\Contracts\View\View
     */
    public function render()
    {
        return view('livewire.profile.show');
    }
}
