<?php

use App\Base\Base;
use Helpers\Aes;
use Api\PhpUtils\Http\Request;
use Api\PhpServices\Session\SessionManager;
use App\Exception\BaseException;

class SessionController extends Base
{
    public function StartAction()
    {
        // $key = 'asdfasdfasdfasdf';
        // $iv = 'fdsafdsafdsafdsa';
        // $salt = "12341/5";
        // var_dump(base64_encode($salt));
        // var_dump(strtolower(str_replace(array('+','/','='), array(''), base64_encode($salt))));
        // var_dump($data = openssl_encrypt($salt, "AES-128-CBC", $key, 0, $iv));
        // var_dump(openssl_decrypt($data, "AES-128-CBC", $key, 0, $iv));
        // exit;
        // $result = (new Request())->get('http://10.126.171.30:18080/topom', [], 1);
        // // $proxy = $result['stats']['proxy'];
        // $server = $result['stats']['group']['models'];
        // $arr = [];
        // foreach ($server as $item) {
        //     $arr[] = $item['servers'][1]['server'];
        // }
        // print_r($arr);
        // exit;
        // print_r(\Yaconf::get('interface.test.service'));exit;
        // echo $s = '{'.str_replace(array('+','/','='), array('-','_',''), base64_encode('12345')).'}';
        // exit;
        // $s = json_decode(json_encode(['a' =>'{MTIzNDU}e3eec597a5a3f9420b445c89d38a6fbc7c']),true);
        // print_r($s);exit;
        // phpinfo();exit;
        // $key = 'interface.test.service.session.get';
        // echo  \Yaconf::get($key);
        // exit;

        $ret = SessionManager::start();
        // print_r($_SERVER);
        // print_r($_COOKIE);
        var_dump($ret);
        print_r($_SESSION);

        // $ret1 = SessionManager::destroy();
        // // print_r($_SERVER);
        // // print_r($_COOKIE);
        // var_dump($ret1);
        // print_r($_SESSION);
        // var_dump($ret);
    }

    public function CreateAction()
    {
        $ret = SessionManager::set('metro', '12345', true, ['a' => 'aaa', 'b' => 'bbb', 'c' => 'ccc', 'd' => 'ddd'], 1);
        var_dump($ret);
        print_r($_SESSION);
        exit;
    }

    public function DestroyAction()
    {
        $ret = SessionManager::destroy();
        // print_r($_SERVER);
        // print_r($_COOKIE);
        var_dump($ret);
        print_r($_SESSION);
    }

    public function GetAction() 
    {
        print_r($request = $this->getRequest()->getRequest());
        $sessionid = $request['sid'] ?? 'aedcc37ea232aade90037716ddab1debe6';
        $url = $this->getBaseUri() . '/get-session';
        $query = ['sessionid' => $sessionid];
        // $query = ['sessionid' => 'jssionidtest'];
        $result = (new Request())->get($url, $query, 3000);
        print_r($result);
        exit;
        if (isset($result['code']) && $result['code'] == 0 ) {
            return true;
        }
        return false;
    }

    public function SetAction() 
    {
        $url = $this->getBaseUri() . '/set-session';
        $sessionData = ['id' => 123, 'name' => 'test'];
        $params = ['sessionid' => 'jssionidtest11', 'data' => ($sessionData), 'expire' => 600, 'key' => 'c071f6e5923a648a5ef88f32b6f738e1'];
        $result = (new Request())->post($url, $params, 1000, 'json');
        print_r($result);
    }

    public function DelAction() 
    {
        $url = $this->getBaseUri() . '/delete-session';
        $query = ['sessionid' => ['jssionidtest1', 'jssionidtest'], 'key' =>'c071f6e5923a648a5ef88f32b6f738e1'];
        $result = (new Request())->get($url, $query, 1000);
        print_r($result);
    }

    public function MgetAction() 
    {
        
        $url = $this->getBaseUri() . '/get-many-session';
        $query = ['sessionid' =>['YPZQISU1Tjxg3wJLFWJmVA', 'E4vo3R5EfaPS-pnyDzia3Q']];
        $result = (new Request())->get($url, $query, 2000);
        print_r($result);
        
        exit;

        $url = $this->getBaseUri() . '/get-many-session';
        $query = ['sessionid' =>['jssionidtest2', 'jssionidtest1']];
        $result = (new Request())->get($url, $query, 2000);
        print_r($result);

        exit;

        // $validSessionIds[] = 'a';
        // $validSessionIds[] = 'b';
        // $validSessionIds[] = 'c';
        // echo $validSessionIds[rand(0, count($validSessionIds) - 1)];
        // exit;


        // // todo 请求接口读redis 
        // echo $url = $this->getBaseUri() . '/get-session1';
        // $query = ['sessionid' => 'jssionidtest'];
        // $result = (new Request())->get($url, $query, 1000);
        // if (isset($result['code']) && $result['code'] == 0 && isset($result['result'])) {
        //     $data = $result['result'];
        //     print_r($data);
        // }else {
        //     print_r($result);
        // }
        // exit;

        // $r = \Api\PhpUtils\Common\IP::find('114.114.114.114');
        // print_r($r);

        // exit;

        // $_SESSION['abc'] = 'ttt';
        // echo $_SESSION['abc'];
        // echo $_SESSION['abc'];
        // echo $_SESSION['abc'];
        // echo $_SESSION['abc'];
        // exit;
        
        // $abc = '123'. "\x7F" . 456;
        // var_dump($abc);exit;//string(7) "123456" 不可见字任作为分隔符
        // $a = base64_encode('6543211111dasfsdfasdf1111asdfasdfs1111');
        // var_dump($a);
        // var_dump(sha1($a));
        // var_dump(substr(sha1($a), -2));
        // $a = $a . substr(sha1($a), -2);
        // var_dump($a);
        // var_dump(substr($a, 0, -2));
        // $a = substr($a, 0, -2);
        // var_dump($a);
        // var_dump(sha1($a));
        // var_dump(substr(sha1($a), -2));
        // exit;
        // var_dump(strlen($a));
        // var_dump($a);exit;
        
        
        // var_dump($this->response);
        // var_dump(Yaf\Dispatcher::getInstance()->getResponse());
        // exit;
        
        // $data = ['appid'=> 'appid', 'verison'=>'023200', 'reqid'=>'11adddddddfasdfasdfasdfasdfdsafwerqwersfasfasdvasdfsdfffs1', 'platform'=>'1', 'ts' => ceil(microtime(true) * 1000)];
        // echo $sign = Aes::createSign($data);
        // exit;
        
        // $data = ['appid'=> 'appid', 'verison'=>'023200', 'reqid'=>'111', 'platform'=>'1'];
        // $data['sign'] = 'l4lBCE3B_v-ODFeuoxFksl0IUY9ehBnjN_FtU3ESdrn_bMPDsEIxA8IUw0pDJ_kX06P6OOw23in46acBucgceSBE2Vx3Ew52w3FfmRn8IhE';
        // $sign = Aes::checkSign($data);
        // var_dump($sign);
        // exit;
        
        // $obj = new App\Models\User();
        // print_r($obj->getUserData());//Array ( [name] => zhangsan [age] => 18 )
        // exit;
        // $http = $this->getRequest()->getRequest();
        // var_dump($http);
        // var_dump(file_get_contents("php://input"));
        // var_dump($this->requestAll());yaf
        // exit;
        $res = [
            "userid" => 'user',
            "code" => 0
        ];
        // print_r($this->failed(-1,'reason'));
        $this->success($res);
    }

    /**
     * get uri
     * @return string
     */
    public function getBaseUri()
    {
        $env = \Yaf\Application::app()->environ() ?? "dev1";
        if ($env == 'product') {
            $this->baseUri = 'http://a4.go2yd.com/Website/session';
        }elseif ($env == 'dev') { 
            $this->baseUri = 'http://127.0.0.1/Website/session';
        }else {
            $this->baseUri = 'http://a4-1.go2yd.com/Website/session';
        }
        return $this->baseUri;
    }
}