<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Login\AccountInfo;
use App\Models\Login\GameVersion;
use Response;
use GuzzleHttp\Client;

class AuthController extends Controller
{
    public function LoginAccountActionPost(Request $request)
    {
        $validator = \Validator::make($request->all(), [
            'Username' => 'required',
            'Password' => 'required'
        ], [], [
            'Username' => '',
            'Password' => ''
        ]);

        if(!$validator->passes())
            return response()->json(['code' => -1, 'msg' => $validator->errors()->first() ], 400);

        $AccountInfo = AccountInfo::LoginAccount($request);

        return response()->json($AccountInfo, 200);
    }

    public function RegisterAccountActionPost(Request $request)
    {

        $request['UUID'] = vsprintf('%s%s-%s-4000-8%.3s-%s%s%s0',str_split(dechex( microtime(true) * 1000 ) . bin2hex( random_bytes(8) ),4));
	     
        $validator = \Validator::make($request->all(), [
            'Username' => 'required|min:5|max:30|unique:Login.AccountInfo,Username',
            'Password' => 'required|min:5|max:30'
        ], [], [
            'Username' => 'Nome de usuário',
            'Password' => 'Senha'
        ]);
		

        if(!$validator->passes())
            return response()->json(['code' => -1, 'msg' => $validator->errors() ], 400);
        
        $saveAcc = AccountInfo::CreateAccount($request);

        if ($saveAcc == 0)
            return Response::json( ['code' => 0, 'msg' => 'Sucesso!' ] ,200 );
        dd($request);
        return Response::json( ['code' => -3, 'msg' => 'Falha ao criar a conta!' ] ,400 );
    }

    public function LoginAccountCheck(Request $request)
    {
		$client = new Client([
			'verify' => false
		]);
		
		return $client->request('get', env('GAME_SERVER_API_URL') . '/api/login', ['query' => $request->all()])->getBody()->getContents();

        $xml = 'null';

        if(isset($request['keyVal1']))
                $Account = AccountInfo::where('UUID', $request['keyVal1'])->first();

                if($Account){
                    if($Account->Real_CERPass != $request['keyVal2'])
                        return response()->xml($xml, $status = 400, $headers = [], $xmlRoot = 'root', $encoding = null);

                    if($Account->IsBlocked == 1)
                        return response()->xml($xml, $status = 400, $headers = [], $xmlRoot = 'root', $encoding = null);

                    $xml = '<root>' .
                    '<getParamInfo>' .
                    '<keyVal1 value="' . $Account->UUID . '"/>' .
                    '<keyVal2 value="'. $Account->Real_CERPass .'"/>' .
                    '<m value="P"/>' .
                    '</getParamInfo>' .
                    '<getResultInfo>' .
                    '<result value="101"/>' .
                    '<resultMsg value="e"/>' .
                    '<LoginID value="' . $Account->Username . '"/>' .
                    '<Permission value="' . $Account->Permission . '"/>' .
                    '<UserAge value="20"/>' .
                    '<MakeCodeNo value="' . $Account->MakeCodeNo . '"/>' .
                    '<UserType value="0"/>' .
                    '<StateDetail value="0"/>' .
                    '<PCRoomType value="1"/>' .
                    '<PCRoomID value="1"/>' .
                    '<UIDSEQ value="' . $Account->AccountDBID . '"/>' .
                    '</getResultInfo>' .
                    '</root>';

                    //AccountInfo::UpdateUUID($Account->UUID);
                }

        return response()->xml($xml, $status = 200, $headers = [], $xmlRoot = '', $encoding = null);

    }

}
