<?php

namespace App\Http\Livewire\Pages\User;

use App\Models\User;
use App\Models\ManualResponse\UsersConnectSpn;
use App\Http\Controllers\ManualResponse\SpnConfigController;
use Livewire\Component;

class ConfigManualResponse extends Component
{
    public $editUserId;
    public $editUsername;
    public $usernameSPN;
    public $passwordSPN;
    public $linkSPN;
    public $action;
 
    public function mount($editUserId)
    {
        $this->editUserId = $editUserId;
        $currentUser = User::find($editUserId);
        $this->editUsername = $currentUser->first_name;
        $userConnectSpn = UsersConnectSpn::where('uid', $editUserId)->first();

        if($userConnectSpn){
            $this->usernameSPN = $userConnectSpn->username;
            $this->passwordSPN = "";
            $this->linkSPN = $userConnectSpn->link;
        }
    }

    public function updateConfig()
    {
        if(empty($this->usernameSPN)){
            $this->dispatchBrowserEvent('open-modal', [
                'name' => 'alert-modal',
                'message' => 'Username must not be empty!',
                'status' => 'warning'
            ]);
            return;
        }

        $userConnectSpn = UsersConnectSpn::where('uid', $this->editUserId)->first();
        if($userConnectSpn){
            if(!empty($this->passwordSPN)){
                $userConnectSpn->password = password_hash($this->passwordSPN, PASSWORD_BCRYPT);
            }
            $userConnectSpn->username = $this->usernameSPN;
            $userConnectSpn->link = $this->linkSPN;
            $userConnectSpn->save();
        }else{
            UsersConnectSpn::create([
                'uid' => $this->editUserId,
                'username' => $this->usernameSPN,
                'password' => password_hash($this->passwordSPN, PASSWORD_BCRYPT),
                'link' => $this->linkSPN
            ]);
        }

        $this->dispatchBrowserEvent('open-modal', [
            'name' => 'alert-modal',
            'message' => 'Update config successfully',
            'status' => 'success'
        ]);
    }

    public function render()
    {
        return view('livewire.pages.user.config-manual-response');
    }

    public function backToMainList()
    {
        //Use emitTo() to call function on specific component, (emit() will call function on every loaded compoment)
        $this->emitTo('pages.user.user-index', 'showUserList', '');
    }

    public function testConnection()
    {
        //Use emitTo() to call function on specific component, (emit() will call function on every loaded compoment)
        $userConnect = UsersConnectSpn::where('uid', $this->editUserId)->first();
        if(!isset($userConnect)){
            $this->dispatchBrowserEvent('open-modal', [
                'name' => 'alert-modal',
                'message' => 'SPN config is not found!',
                'status' => 'failed'
            ]);
            return;
        }

        $spnConfigController = new SpnConfigController();
        $spnConfig = $spnConfigController->init_connection($userConnect);
        if($spnConfig != null && $spnConfig['success'] == 'true'){
            $this->dispatchBrowserEvent('open-modal', [
                'name' => 'alert-modal',
                'message' => 'Connect to SPN successfully',
                'status' => 'success'
            ]);
        }else if($spnConfig != null && $spnConfig['success'] == 'false'){
            $this->dispatchBrowserEvent('open-modal', [
                'name' => 'alert-modal',
                'message' => $spnConfig['message'],
                'status' => 'failed'
            ]);
        }else{
            //Cannot get UID/UCODE from SPN
            $this->dispatchBrowserEvent('open-modal', [
                'name' => 'alert-modal',
                'message' => 'Cannot connect to SPN',
                'status' => 'failed'
            ]);
        }
    }

}
