<?php
/**
 *------Create thems Controller------
 *------SCWPHP  Version 1.0.0------
 *------Dev Model Jions------
 *------Create Time 2017-06-23 15:06:45------
 */
namespace App\InApi\Controller;


use BaseController\Controller;
use Biz\Game\GameLogic;
use App\Commons\Model\Wagent;


class Index extends Controller {
    //用户上传参数数组;
    private $paras = [] ;
    private $parasString = '';
    private $userKey = '' ;
    private $model = null ;
    private $logic = null ;

    //默认返回数据格式
    private $defformat = 'json' ;

    //所有请求入口
	public function dobusiness() {
	   $this->doLogic();
	}

	public function   getdata(){
        $this->doLogic();
    }


    private function  doLogic(){
        $this->ParasCheck();
        $this->AgentKeyCheck();

        $method = $this->ActionMap()[$this->paras['method']];
        $this->logic = new GameLogic($this->paras,$this->userKey,$this->model);

        if ( (new  \ReflectionClass($this->logic))->hasMethod($method)){
            $ret = $this->logic->$method();
            Render($ret['datas'],$ret['status'],$ret['msg']);
        }
        Render('',10009,lang('Errors','Sports')->get('error-10009'));

    }

	private  function ActionMap(){
	    $maps = [
	        'caie' => 'CheckAccountIsExist',
            'caca' => 'CheckAndCreateAccount',
            'gb' => 'GetBalance',
            'ptc' => 'TransferCredit',
            'ctc' => 'ConfirmTransferCredit',
            'tg' => 'TransferGame',
            'gr' => 'GetReport',
            'gct' => 'GetCashTrade',
            'gbrbv' => 'GetBettingRecordByVendor',
            'gsbrbv' => 'GetSportsBettingRecordByVendor',
            'gebrbv' => 'GetEleBettingRecord',
            'gfbrbv' => 'GetFishBettingRecord',
            'glbrbv' => 'GetLotteryBettingRecord',
            'ggr' => 'GetGameResult',
            'ua' => 'UpdateAccount',
            'gvvi' => 'GetVideoVendorId',
            'gsvi' => 'GetSportVendorId',
            'atin' => 'AutoLogin',
        ];
	    return $maps;
    }


    //输入参数校验
	private function  ParasCheck(){
        $this->parasString = $params = $_REQUEST['params'];
        $this->userKey = $_REQUEST['Key'];

        $arr = explode('&',base64_decode($params));

        if  (is_array($arr)){
            foreach ($arr as $substring){
                $tmp = explode("=",$substring);
                if (isset($tmp['0'])){
                    $tmpkey = strtolower(trim($tmp['0']));
                    $val = isset($tmp['1']) ?  trim($tmp['1']) : '' ;
                    $this->paras[$tmpkey] = $val;
                }
            }
        }else{
            Render('',10010,lang('Errors','Sports')->get('error-10009'));
        }

        $allmap = $this->ActionMap();
        $method = isset($this->paras['method']) ? $this->paras['method'] : '';

        if (!isset( $allmap[$method] )){
            Render('',10011,lang('Errors','Sports')->get('error-10009'));
        }

        $format = isset($this->paras['format']  ) ?   strtolower(trim($this->paras['format'])) : '';
        if ($format =='' || !in_array($format,['json','xml'])){ $format = $this->defformat;   }
        S('CUR_RETURN_FORMAT',$format);
    }

    //Key验证
    private  function   AgentKeyCheck(){
        $agent = isset($this->paras['agent']) ?  trim($this->paras['agent']) : '' ;
        if (empty($agent)){
            Render('key_error',0,'key_error');
        }

        $model = (new Wagent)->getByName($agent);
        if (empty($model)){
            Render('10',0,'The agent not exist');
        }
        $ret = $model->CheckKey($model,$this->parasString,$this->userKey) ;
        if ( $ret!=1 ){
            if ($ret == -1){
                Render('10',2,'Token overTime,please Refresh!');
            }
            Render('10',0,'key check error!-'.$ret);
        }

        $this->model = $model ;
        return  true;
    }


    //调试地址生成用
    public function  makeurl(){

        $remotip = $_SERVER['REMOTE_ADDR'];
        if (!($remotip =='127.0.0.1' || substr($remotip,0,7) == '192.168')){
            //只能在内网调试使用
            exit;
        }

	    $paras = $_REQUEST;
	    unset($paras['_url']);
	    if (empty($paras)){
	        exit ;
        }

	    $strArr = array() ;
        foreach ($paras as $key=>$val){
            //if ($key=='agent'){   $val = 'test1agent' ;   }
            array_push($strArr,$key.'='.$val);
        }
        $string = implode ('&',$strArr);
	    $key = md5(base64_encode($string).'58f306c11e6a9d4cc74723bb76b17500');

	    if (isset($_REQUEST['debug'])){
            $host = $_SERVER['HTTP_HOST'];
            $url = 'http://' . $host . '/InApi-index/dobusiness?params='.base64_encode($string).'&Key='.$key;
        }else{
            $url = 'http://sports.5gogo.com/InApi-index/dobusiness?params='.base64_encode($string).'&Key='.$key;
        }

        Render(['url'=>$url],1,'success');

    }


    //客户获取token用于 Key 的生成使用
    public function  getToken(){

            $agengname = $_POST['wagent_name'] ;
            $key = $_POST['wagent_key'] ;
            $secret = $_POST['wagent_secret'] ;

            $model  = (new Wagent)->TokenGetAndUpdate($agengname,$key,$secret);
            if (!$model){
                    Render('',3,'get Token false!');
            }

            Render(['token'=>$model->auth_token],1,'success');

    }




}
