<?php namespace Api\Lib\Platform;
use Dever;
class Field
{
    private $data = array();
    public $ssl;
    public function __construct($data)
    {
        $this->ssl = new Ssl($this);
        $this->set('time', time());
        $this->set('timestamp', \Dever\Helper\Secure::timestamp());
        $this->set('nonce', \Dever\Helper\Secure::nonce());
        $this->data = $data;
    }

    public function add($index, $value, $key = 'body')
    {
        $this->data[$key][$index] = $value;
    }

    public function set($key, $value)
    {
        $this->data[$key] = $value;
    }

    public function __set($key, $value)
    {
        $this->data[$key] = $value;
    }

    public function __get($key)
    {
        if (array_key_exists($key, $this->data)) {
            return $this->data[$key];
        } else {
            return null;
        }
    }

    public function createRequestId()
    {
        $value = Dever::db('api_log', 'api')->insert([]);
        $this->set('request_id', $value);
    }

    public function setPlatformId($value)
    {
        $this->set('platform_id', $value);
    }

    public function setApid($value)
    {
        $this->set('api_id', $value);
    }

    public function setHost($value)
    {
        $this->set('body', $value);
    }

    public function setUri($value)
    {
        $this->set('uri', $value);
    }

    public function setPath($value)
    {
        $this->set('path', $value);
    }

    public function setQuery($value)
    {
        $this->set('query', $value);
    }

    public function setUrl($value)
    {
        $this->set('url', $value);
    }

    public function setNotify($value)
    {
        $this->set('notify', $value);
    }

    public function setMethod($value)
    {
        $this->set('method', $value);
    }

    public function setBody($value)
    {
        $this->set('body', $value);
    }

    public function setBodyJson($value)
    {
        $this->set('body_json', $value);
    }

    public function setHeader($value)
    {
        $this->set('header', $value);
    }

    public function setHeaderJson($value)
    {
        $this->set('header_json', $value);
    }

    public function setNumber($value)
    {
        $this->set('number', $value);
    }

    public function value($source, $type = '', $state = true)
    {
        $value = trim($source);
        if ($this->data && isset($this->data[$value])) {
            $value = $this->data[$value];
        } elseif (strstr($value, 'key=') && strstr($value, '&')) {
            $value = $this->parse($value);
        } elseif ($a = strstr($value, '{') || strstr($value, '(')) {
            $value = $this->eval($value);
        }
        if (!$type && $source == $value) {
            /*
            $field = Dever::db('platform_field', 'api')->find(array('platform_id' => $this->data['platform_id'], 'key' => $value));
            if ($field) {
                $value = $field['value'];
                $type = $field['type'];
            } else {
                $sign = Dever::db('platform_sign', 'api')->find(array('platform_id' => $this->data['platform_id'], 'name' => $value));
                if ($sign) {
                    $value = '';
                    $type = '3,' . $sign['id'];
                }
            }*/
            $sign = Dever::db('platform_sign', 'api')->find(array('platform_id' => $this->data['platform_id'], 'name' => $value));
            if ($sign) {
                $value = '';
                $type = '3,' . $sign['id'];
            }
        }
        return $this->handle($type, $value, $state);
    }

    protected function parse($value)
    {
        parse_str($value, $temp);
        $k = $temp['key'];
        unset($temp['key']);
        if (isset($this->data[$k]) && isset($temp[$this->data[$k]])) {
            $value = $temp[$this->data[$k]];
        }
        return $value;
    }

    protected function eval($value)
    {
        $func = function ($r) {
            return $this->value($r[1]);
        };
        $value = preg_replace_callback('/{(.*?)}/', $func, $value);
        $value = '$value = '.$value.';';
        eval($value);
        return $value;
    }

    protected function handle($type, $value, $state)
    {
        if (strpos($type, ',')) {
            list($type, $type_id) = explode(',', $type);
            if ($type == 1) {
                $info = Dever::db('format', 'api')->find($type_id);
                if ($info) {
                    $value = \Dever\Helper\Str::val($info['method'], array('value' => $value));
                }
            } elseif ($type == 2) {
                # state == true 是编码 == false 是解码
                if ($state) {
                    $value = $this->ssl->encrypt($type_id, $value);
                } else {
                    $value = $this->ssl->decrypt($type_id, $value);
                }
            } elseif ($type == 3) {
                $info = Dever::db('platform_sign', 'api')->find($type_id);
                if ($info) {
                    if ($value) {
                        $info['arg'] = $value;
                    }
                    $value = Sign::load($this, $info)->get();
                }
            }
        }
        return $value;
    }
}