<?php

namespace App\Game;

use App\Game\Config\GameBasicConfig;
use App\Game\Services\RouteService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Redis;

class GameCard extends Model
{
    protected $connection='mysql';

    const TABLE = 'webgame.games';
    protected $table = self::TABLE;
    public $timestamps = false;

    protected $fillable = ['gid','img','img_pt','img_es','level_info', 'link', 'online', 'desc','brand','title','state'];

    public static $OuroGameInfo_KEY="OuroGameInfos";
    public static function setOuroGameInfo($gameInfo)
    {
        self::$_gameinfo=$gameInfo;
        Redis::set(self::$OuroGameInfo_KEY,json_encode($gameInfo));
    }
    private static $_gameinfo=null;
    public static function getOuroGameInfo(){
        if(self::$_gameinfo==null){
            if(Redis::exists(self::$OuroGameInfo_KEY)){
                self::$_gameinfo=json_decode(Redis::get(self::$OuroGameInfo_KEY),true);
            }
        }
        return self::$_gameinfo;

    }
    public static function GetGameByID($id){
        $card=GameCard::where('id',$id)->first();
        if($card){
            $card=self::formatGames([$card])[0];
        }
        return $card;
    }
    public static function formatGames($games)
    {

        $cdn_org=["cdn.moeda777.com"];
        $cdn_replace="24680.imgix.net";
        $img_add_param="?auto=format,compress&cs=srgb&dpr=2&w=140";
        $img_add_param="";

        //处理多语言
        $locale=GlobalUserInfo::getLocale();
        //处理房间信息
        $ouroGameInfos=self::getOuroGameInfo();
        foreach($games as $game){
            //不是http开头，补齐
            if(!strstr($game->link,"http")){
                $game->link=GameBasicConfig::$ApiServer.$game->link;
            }

            if($game->brand=="PG"&&RouteService::isTestSite()){
                $game->link=str_replace('pgpro','pg',$game->link);
            }

            if(isset($ouroGameInfos)&&$game->brand=="OURO777"&&!empty($game->gid)){
                if(GlobalUserInfo::$me) {
                    $score=GlobalUserInfo::getScoreByUserID(GlobalUserInfo::$me->UserID);
                    if($score>=0) {
                        if (isset($ouroGameInfos[$game->gid]) && !empty($ouroGameInfos[$game->gid])) {
                            $game->level_info = $ouroGameInfos[$game->gid];
                        }
                    }
                }
            }

//            if($locale=='en'){
//                unset($game->img_pt,$game->img_es);
//                continue;
//            }
            if($locale=="pt"&&!empty($game->img_pt)){
                $game->img=$game->img_pt;
            }elseif($locale=="es"){
                if(!empty($game->img_es)){
                    $game->img=$game->img_es;
                }elseif (!empty($game->img_pt)){
                    $game->img=$game->img_pt;
                }
            }
            unset($game->img_pt,$game->img_es);

            $game->img=str_replace($cdn_org,$cdn_replace,$game->img).$img_add_param;



        }
        return $games;

    }

    /**
     * 关闭state检查
     * @var bool
     */
    public static $enableStateCheck=true;
    protected static function boot()
    {
        parent::boot();

        if(self::$enableStateCheck) {
            static::addGlobalScope('where', function (Builder $builder) {
//            $builder->where('state', 1);
                $builder->whereRaw(RouteService::getStateToWhereRaw());
            });
        }
    }
}
