<?php

namespace Api\PhpServices\Session;

use Api\PhpUtils\Http\Request;

/**
 * Customer Redis Session Handler
 */
class RedisSessionHandler implements \SessionHandlerInterface
{
    protected $key = 'c071f6e5923a648a5ef88f32b6f738e1';
    protected $expire = 0;

    /**
     * open session
     * @param string $savePath
     * @param string $sessionName
     * @return boolean
     */
    public function open($savePath, $sessionName)
    {
        return true;
    }

    /**
     * read session
     * @param string $sessionId
     * @return string
     */
    public function read($sessionId)
    {
        $url = $this->getUrl('get');
        $query = ['sessionid' => $sessionId];
        $result = (new Request())->get($url, $query);
        $data = [];
        if (isset($result['response']['code']) && $result['response']['code'] == 0 && isset($result['response']['result'])) {
            $data = $result['response']['result'];
        }
        return empty($data) ? '' : (is_array($data) ? serialize($data) : $data);
    }

    /**
     * write session
     * @param string $sessionId
     * @param string $sessionData
     * @return boolean
     */
    public function write($sessionId, $sessionData)
    {
        if (!$_SESSION) {
            return true;
        }
        $url = $this->getUrl('set');
        $params = ['sessionid' => $sessionId, 'data' => $_SESSION, 'expire' => $this->expire, 'key' => $this->key];
        $result = (new Request())->post($url, $params, 3000, 'json');
        if (isset($result['response']['code']) && $result['response']['code'] == 0 ) {
            return true;
        }
        return false;
    }

    /**
     * close session
     * @return boolean
     */
    public function close()
    {
        return true;
    }

    /**
     * destroy session
     * @param string $sessionId
     * @return boolean
     */
    public function destroy($sessionId)
    {
        $url = $this->getUrl('delete');
        $params = ['sessionid' => $sessionId, 'key' => $this->key];
        $result = (new Request())->post($url, $params, 3000, 'json');
        if (isset($result['response']['code']) && $result['response']['code'] == 0 ) {
            return true;
        }
        return false;
    }

    /**
     * garbage clean session
     * session in redis would be auto cleaned when expired
     * @param integer $maxlifetime
     * @return integer
     */
    public function gc($maxlifetime)
    {
        return 0;
    }

    /**
     * set session expire
     * @param integer $expire   second
     * @return RedisSessionHandler
     */
    public function setExpire($expire)
    {
        $this->expire = $expire;
        return $this;
    }

    /**
     * get session expire
     * @return integer
     */
    public function getExpire()
    {
        return $this->expire;
    }

    /**
     * get yaconf interface.url
     * @return string
     */
    public function getUrl($param = '')
    {
        if (empty($param)){
            return "";
        }
        $env = \Yaf\Application::app()->environ() ?? "test";
        $key = 'interface.' . $env . '.service.session.' . $param;
        return \Yaconf::get($key);
    }
}
