<?php

namespace App\Http\Controllers\Game;

use App\Facade\TableName;
use App\Game\GameCard;
use App\Game\Config\PPGameIDs;
use App\Game\GlobalUserInfo;
use App\Game\LogGamecardClick;
use App\Game\Services\OuroGameService;
use App\Game\Services\PgSoftService;
use App\Game\Services\PlatformService;
use App\Game\Services\ServerService;
use App\Http\helper\ApiSign;
use App\Models\AccountsInfo;
use App\Notification\TelegramBot;
use App\Util;
use App\Utility\SetNXLock;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class PPSimController extends Controller
{

    public function __construct()
    {

    }


    public function gameLunch(Request $request)
    {

        $gid = $request->input('gid');
        $user = $request->user();

        if (!$user) {

            $userid = $request->UserID;


            $GlobalUID = 'a1b1c1-b53b-'.ServerService::GetLocalSign().'-' . $userid;


            $user=GlobalUserInfo::getGameUserInfo('UserID',$userid);
            if(!$user){
                $user=AccountsInfo::find($userid);
            }

        }else{
            $GlobalUID = $user->GlobalUID;
            $userid = $user->UserID;

        }
        $lang = GlobalUserInfo::getLocaleByUserID($userid,$request->input('language', env('DEFAULT_LOCALE', 'en')));


        $gamecard = GameCard::where('gid', $gid)->where('brand', 'PP')->first();
        if($gamecard){
            $gamecard->increment('play_num', 1);
            LogGamecardClick::recordClick($gamecard->id, $userid);

            GameCard::$enableStateCheck = false;
        }

        $gameName=$gamecard->title;


        $data = ['UserID' => ServerService::GlobalToUserID($GlobalUID)];


        $Currency = env("CONFIG_24680_CURRENCY", "USD");
        $CurrencySymbol = env("CONFIG_24680_DOLLAR", "$");



        $data['currency'] = $Currency;
        $data['dollar'] = $CurrencySymbol;


        if(!is_array($user))$user=$user->toArray();

        $data = array_merge($data, $user);

        $session = $data;


        $token = md5($GlobalUID.'|'.microtime());
        Redis::setex($token, 7200, json_encode($session));
        $gamecode=$gid;
        $short2=substr($gamecode, 0, 2);

        $configurls=json_decode(env('CONFIG_GAMES'),true);
        $configurl=$configurls['pp'];

        $host=$configurl['api'];
        $api_url='https://'.$host;
        $cdn_url='https://'.$configurl['source'];
        $web_url=env('APP_URL');

        if($userid==80000140){
            $host='test.pgn-nmu2nd.com';
            $api_url='https://test.pgn-nmu2nd.com';
            $cdn_url='https://test.pgn-nmu2nd.com';
        }
        $hide_logo=PPGameIDs::$ids[$gamecode]['hide_logo']||false;
        return view('game.ppsim.html5', compact('host', 'data','token','gamecode','short2','lang','gameName','api_url','cdn_url','hide_logo','web_url'));

    }

    public function leaveOrg(Request $request){

        $UserID = $request->UserID;
        try {
            DB::connection('write')->table('QPTreasureDB.dbo.GameScoreLocker')
                ->where('UserID', $UserID)
                ->delete();
        } catch (\Throwable $e) {
        }
        return '<script>
            if(window.parent&&window.parent!=window){
                window.parent.postMessage("backhome","*")
            }else {
                document.location = "game://a=1&b=2";
            }
        </script>';
    }
    public function gameLunchOrg(Request $request)
    {

    }

    private function convertString($input)
    {
        return strrev($input);

    }


}
