<?php


namespace App\Http\Controllers\Api;


use App\Http\helper\HttpCurl;
use App\Jobs\ClearCache;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class GetVersionController
{
    public function index(Request $request)
    {

        $login_ip = $request->ip();
        $inIp = config('inIp');
        $inner = 0;
        if(in_array($login_ip,$inIp)){
            $inner = 1;
        }


        $data=self::getVersion($inner);
        $result = apiReturnSuc($data);
        return $result;
    }
    public function indexRuby(Request $request)
    {

        $login_ip = $request->ip();
        $inIp = config('inIp');
        $inner = 0;
        if(in_array($login_ip,$inIp)){
            $inner = 1;
        }
        $data=self::getVersionRuby($inner);
        $result = apiReturnSuc($data);
        return $result;
    }

    public static function getVersionRuby($inner=false){
        $key="getVersionRuby";

        if (Redis::exists($key) ) {
            $data = json_decode(Redis::get($key));
        }else{
            $GameNames = [
                'RsROMAN', 'RsDZPK', 'RsCACHETA','RsTRUCO','RsFRUIT9X','RsFOOTBALL', 'RsOLYMPUS','RsTREASURE','RsRAIO','RsHALLOWEEN',"RsCRASH","RsMINES","RsBCBM","RsSGYYL","RsJOKER","RsHIVE","RsATENA","RsGOLDTIGER","RsOX","RsBIGBASS"
            ];
            $requestUrl = 'http://static.ruby777.gold/br/update/';

            $data=self::fetchDatas($GameNames,$requestUrl);

            $ret = json_encode($data);
            Redis::set($key, $ret);
        }

        return $data;
    }
    public static function getVersion($inner=false){

        if (Redis::exists('getVersion') && !$inner) {
            $data = json_decode(Redis::get('getVersion'));
        } else {

            $GameNames = [
                'AVIATOR2','AVIATOR','ROMAN', 'DZPK', 'CACHETA','TRUCO','FRUIT9X','FOOTBALL','NEWOLYMPUS','AZTEC', 'OLYMPUS','TREASURE','RAIO','HALLOWEEN',"CRASH","MINES","BCBM","SGYYL","JOKER5x1","HIVE","ATENA","GOLDTIGER","OX","BIGBASS","LHD"
            ];

            $requestUrl = env('OURO_GAME_CDN','https://cdn.moeda777.com/glo/mexico/update/');

            $data=self::fetchDatas($GameNames,$requestUrl);
            if(!$inner){
                $ret = json_encode($data);
                Redis::set('getVersion', $ret);
            }

        }
        return $data;
    }
    private static function fetchDatas($GameNames,$requestUrl){
        $data = [];
        $http = new HttpCurl();
        foreach ($GameNames as $gameName) {

            if ($gameName == 'assets') {
                $url = $requestUrl . 'assets/version.manifest';
            } else {
                $url = $requestUrl . $gameName . "/" . $gameName . "_ver.manifest";
            }

            $res = $http->curl_get($url);

            try {
                $toArr = \GuzzleHttp\json_decode($res, true);
            }catch (\Exception $exception){
                $toArr['version'] =0;
            }

            $data[$gameName] = $toArr['version'] ?? 0;
        }
        return $data;
    }

    // 清空版本信息
    public function delVersion()
    {

        Redis::del('getVersion');
        Redis::del('getVersionRuby');
//        ClearCache::dispatch();
        return apiReturnSuc();
    }
}
