<?php

namespace App\Game;

use App\Game\Services\RouteService;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Redis;
define('SPECIAL_MODE_GUEST', 1);
define('SPECIAL_MODE_ZERO_MONEY', 2);//注册不送钱
define('SPECIAL_DISABLE_PROMOTE_INSTALL', 4);//提示安装
//define('SPECIAL_MODE_ENABLE_INSURE_SCORE', 8);//直接启用第二货币  GameScoreInfo InsureScore
define('SPECIAL_MODE_REG_TO_REALMONEY', 16);//默认注册送的钱是真钱不是第二货币
define('SPECIAL_MODE_MAIL_BONUS', 32);//MAIL验证送钱
define('SPECIAL_MODE_FIRSTPAY_OFF30', 64);//首冲打开30%bonus
define('SPECIAL_MODE_DEBUG_EVENT', 128);//是否开启事件日志上保
define('SPECIAL_MODE_REGION_UNIQUE', 256);//是否开启区域独立模式
define('SPECIAL_MODE_FB_JUMP_LATER', 512);//fb内浏览器弹出时机靠后
define('SPECIAL_MODE_FB_JUMP_LATER2', 1024);//fb内浏览器弹出时机靠后
class WebChannelConfig extends Model
{
    protected $table = 'webgame.WebChannelConfig';
    protected $primaryKey = 'ID';
    public $incrementing = true;
    protected $keyType = 'int';
    protected $connection = 'mysql';
    protected $fillable = [
        'Channel', 'PackageName', 'RegionID', 'Remarks', 'StateNo','SpecialMode', 'PlatformName', 'PlatformID', 'PlatformToken', 'LoginOpen','RegOpen','BonusArr','ShadowChannel','LightApk','FullApk'
    ];

    private static $key='web_channel_config:';

    public $timestamps = false;

    protected $shadow_channel=0;
    public function SHADOW_CHANNEL()
    {
        if($this->shadow_channel)return $this->shadow_channel;
        $this->shadow_channel=$this->Channel;
        if(!empty($this->ShadowChannel)){
            $channels=explode('|',$this->ShadowChannel);
            $allRate=0;
            $rates=[];
            foreach ($channels as $channel){
                $channel=explode('%',$channel);
                $allRate+=$channel[0];
                $rates[$allRate]=$channel[1];
            }
            $rand=mt_rand(0, 100);
            foreach($rates as $rate=>$channel){
                if($rand<$rate){
                    $this->shadow_channel=$channel;
                    return $channel;
                }
            }
        }
        return $this->shadow_channel;
    }
    public function BONUS_PWA()
    {
        return explode('|',$this->BonusArr)[3]??00;
    }
    public function BONUS_VERIFY_PHONE()
    {
        return explode('|',$this->BonusArr)[1]??00;
    }
    public function BONUS_REG()
    {
        return explode('|',$this->BonusArr)[0]??00;
    }
    public function BONUS_VERIFY_EMAIL()
    {
        return explode('|',$this->BonusArr)[2]??00;
    }
    public function isRealMoneyReg()
    {
        return ($this->SpecialMode&SPECIAL_MODE_REG_TO_REALMONEY)==SPECIAL_MODE_REG_TO_REALMONEY;
    }
    public static function GuestOpen($config)
    {
        return ($config->SpecialMode&SPECIAL_MODE_GUEST)==SPECIAL_MODE_GUEST;
    }
    public function isDisablePromote()
    {
        return ($this->SpecialMode&SPECIAL_DISABLE_PROMOTE_INSTALL)==SPECIAL_DISABLE_PROMOTE_INSTALL;
    }
    public function isDebugEvent()
    {
        return ($this->SpecialMode&SPECIAL_MODE_DEBUG_EVENT)==SPECIAL_MODE_DEBUG_EVENT;
    }
    public function isFbJumpLater(){
        if(($this->SpecialMode&SPECIAL_MODE_FB_JUMP_LATER2)==SPECIAL_MODE_FB_JUMP_LATER2)return 2;
        if(($this->SpecialMode&SPECIAL_MODE_FB_JUMP_LATER)==SPECIAL_MODE_FB_JUMP_LATER)return 1;
        return 0;
    }
    public function isRegionUnique()
    {
        if(($this->SpecialMode&SPECIAL_MODE_REGION_UNIQUE)==SPECIAL_MODE_REGION_UNIQUE&&!empty($this->RegionID)){
            return $this->RegionID;
        }else{
            $region_config=RouteService::getRegionConfig();
            if($region_config&&$region_config->isAllChannelsUnique()){
                return $region_config->RegionID;
            }
            return "";
        }
    }
    public function isGuestOpen()
    {
        return ($this->SpecialMode&SPECIAL_MODE_GUEST)==SPECIAL_MODE_GUEST;
    }
    public function isRegZeroMoneyOpen()
    {
        return ($this->SpecialMode&SPECIAL_MODE_ZERO_MONEY)==SPECIAL_MODE_ZERO_MONEY;
    }
    // 获取特定 Channel 的记录
    public static function getByChannel($channel)
    {
        $cacheKey = self::$key . $channel;

        $cachedConfig = Redis::get($cacheKey);

        if ($cachedConfig) {
            return new self(json_decode($cachedConfig,true));
        }

        $config = self::where('Channel', $channel)->first();

        if ($config) {
            Redis::setex($cacheKey, 300, $config->toJson());
        }

        return $config;
    }
    // 清除缓存
    public static function clearCache($channel)
    {
        $cacheKey = self::$key . $channel;
        Redis::del($cacheKey);
    }

    // 监听模型事件
    protected static function boot()
    {
        parent::boot();

        static::saved(function ($model) {
            self::clearCache($model->Channel);
        });

        static::deleted(function ($model) {
            self::clearCache($model->Channel);
        });
    }
}
