<?php

namespace App\Models\Login;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Helpers\Functions;
use App\Models\Login\GameVersion;

class AccountInfo extends Model
{
    protected $connection = "Login";
    protected $table = "AccountInfo";
    protected $primaryKey = "AccountDBID";
    protected $dates = [];
    protected $fillable = ["Username", "Password", "UUID","RegisterTime", "RegisterIP",
						   "LastLoginTime","LastLoginIP","IsBlocked","Real_Balance","Bonus_Balance",
						   "Permission","UserAge","MakeCodeNo","Email","Real_CERPass"
	
	];

    protected $hidden = [
        'Password',
        'UUID',
        'Permission',
        'MakeCodeNo'
    ];

    public $timestamps = false;

    public function antihacklogcontrol(){
        return $this->hasMany('App\Models\Web\AntihackLogControl','AccountDBID');
    }

    public static function LoginAccount($params)
    {

        $AccountData = self::where('Username', $params['Username'])->first();

        if(!$AccountData){
            return ['code' => -1, 'msg' => 'Ez a fiók nem létezik!' ];
        }else if($AccountData->Username == $params['Username'] && $AccountData->Password != md5($params['Password'])){
            return ['code' => -2, 'msg' => 'Hibás jelszó!' ];
        }

        if($AccountData->IsBlocked == 1)
            return ['code' => -3, 'msg' => 'Letiltott fiók!' ];

        //if($AccountData->IsActivated == 0)
            //return ['code' => -4, 'msg' => 'Conta não ativada!' ];

        //$GameVersion = GameVersion::first();
        //if($GameVersion->IsMaintenance == 1 && $AccountData->Permission == 0)
            //return ['code' => -5, 'msg' => 'Servidor em manutenção!' ];

        $UUID = vsprintf('%s%s-%s-4000-8%.3s-%s%s%s0',str_split(dechex( microtime(true) * 1000 ) . bin2hex( random_bytes(8) ),4));

        $AccountData->UUID = $UUID;
        $AccountData->LastLoginIP = Functions::getIp();
        $AccountData->LastLoginTime = now();
        $AccountData->save();

        //$AccountData->Version = $GameVersion->Version;

        return [
            'code' => 0,
            'msg' => 'Siker!',
            'token' => $UUID,
            'accountinfo' => $AccountData
        ];
    }

    public static function UpdateUUID($UUID)
    {
        $AccountData = self::where('UUID', $UUID)->first();
        $AccountData->UUID = "";
        $AccountData->save();
    }

    public static function GetBalance($Username)
    {
        $totalBalance = 0;

        $AccountBalance = self::where('Username', $Username)->first();
        if($AccountBalance){
            $totalBalance = $AccountBalance->Real_Balance + $AccountBalance->Bonus_Balance;
        }
        return $totalBalance;
    }

    public static function UpdateBalance($Username, $totalBalance)
    {
        $AccountBalance = self::where('Username', $Username)->first();
        if($AccountBalance)
            $AccountBalance->Real_Balance = $totalBalance;
            $AccountBalance->Bonus_Balance = 0;
            $AccountBalance->save();

    }

    public static function GETACCOUNTSPECIALCHARINNAME()
    {
        $accounts = self::get();
        $Data[] = [];
        foreach ($accounts as $key => $account) {
            if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $account->Username))
            {
                $Data[$key] = [
                    'Username' => $account->Username
                ];
            }
        }

        return $Data;
    }

    public static function CreateAccount($params)
    {
        $Account = new AccountInfo;
        $Account->Username = $params['Username'];
        $Account->Password = md5($params['Password']);
        $Account->UUID = $params['UUID'];
        $Account->RegisterIP = '127.0.0.1';
        $Account->RegisterTime = now();
        $Account->IsBlocked = 0;
        $Account->Real_Balance = 200000;
        $Account->Bonus_Balance = 0;
        //$Account->BirthDate = now();
        $Account->Email = 'teste@teste.com.br';
        //$Account->AccountType = 0;
        //$Account->AccountRecoverCode = '123456';
        //$Account->IsActivated = 0;
        //$Account->RegFont = 'quenio';
        $Account->Permission = 0;
        $Account->save();
    }
}
