<?php

namespace Main\Lib;

use Dever;

class Core
{
    protected $checkUser = false;
    protected $data;
    public function __construct()
    {
        # 获取用户信息
        if ($this->checkUser) {
            $this->data['uid'] = Dever::load('passport/applet')->check();
        } else {
            $this->data['uid'] = Dever::load('passport/applet')->check(false);
        }
        if ($this->data['uid'] < 0) {
            $this->data['uid'] = 0;
        }
        //$this->data['uid'] = 1;

        # 获取基本配置
        $key = 'config';
        $this->data['config'] = Dever::cache($key);
        if (!$this->data['config']) {
            $this->data['config'] = Dever::db('main/config')->one();
            Dever::cache($key, $this->data['config']);
        }
        
        $this->data['system'] = Dever::input('system', 1);

        $this->source_type = Dever::input('source_type');
        $this->app = false;
        if ($this->source_type == 'ios' || $this->source_type == 'android') {
            $this->app = true;
        }

        $this->data['ios_state'] = 2;
        $version_code = Dever::input('version_code');
        if ($version_code && $this->app) {
            $version_code = str_replace("\n", "", $version_code);
            $version_where['source_type'] = $this->source_type;
            $version_where['name'] = $version_code;
            $version = Dever::db('main/version')->getOne($version_where);
            if ($version && $version['vip'] > 0) {
               $this->data['ios_state'] = $version['vip'];
            }
        }
    }

    # 图片处理
    public function pic($data, $type = 't1')
    {
        //?imageView2/1/w/100/h/100/q/75
        $config = array
        (
            'pic','pic_cover', 'pic_cover_169', 'content', 'share_pic'
        );

        foreach ($config as $k => $v) {
            if (isset($data[$v]) && $data[$v]) {
                $data[$v] = $this->replacePic($data[$v], $type);
            }
        }

        return $data;
    }

    public function replacePic($pic, $type = 't1')
    {
        //$pic = Dever::pic($pic, $type);

        if (strstr($pic, 'http://')) {
            $pic = str_replace('http://', 'https://', $pic);
        }

        return $pic;
    }

    # 列表页里的数据 根据类型，解析内容
    protected function content($data, $content = false)
    {
        $type = $id = false;
        if (isset($data['type']) && isset($data['data_id'])) {
            $type = $data['type'];
            $id = $data['data_id'];
        }

        if ($type && $id) {
            $table = Dever::config('base')->type_table[$type];
            //$info = Dever::db($table)->getOne($id);
            $info = Dever::db($table)->one($id);
            if ($info) {
                return $this->getInfo($type, $info, $content);
            } else {
                return array();
            }
            
        }
        return array();
    }

    protected function getInfo($type, $info, $content = false)
    {
        $info = $this->pic($info);
        $info['type'] = $type;
        $info['data_id'] = $info['id'];
        if (isset($info['content']) && !$content) {
            unset($info['content']);
        }
        if (isset($info['pdate']) && $info['pdate'] > 0) {
        } else {
            $info['pdate'] = time();
        }
        $info['cdate'] = $info['pdate'] = date('Y-m-d', $info['pdate']);

        //$info['cdate'] = date('Y-m-d', $info['cdate']);

        if (isset($info['cate_id']) && $info['cate_id']) {
            $cate = Dever::db('content/cate')->one($info['cate_id']);
            $info['cate_name'] = $cate['name'];
        }

        if (isset($info['author_id']) && $info['author_id']) {
            $author = Dever::db('content/author')->one($info['author_id']);
            $info['author_name'] = $author['name'];
        }

        if (isset($info['num_add_view']) && isset($info['num_view'])) {
            $info['num_view'] = $info['num_add_view'] + $info['num_view'];
            unset($info['num_add_view']);
        }
        if (isset($info['num_add_up']) && isset($info['num_up'])) {
            $info['num_up'] = $info['num_add_up'] + $info['num_up'];
            unset($info['num_add_up']);
        }
        if (isset($info['num_add_user']) && isset($info['num_user'])) {
            $info['num_user'] = $info['num_add_user'] + $info['num_user'];
            unset($info['num_add_user']);
        }
        if (isset($info['num_add_user']) && isset($info['num_user'])) {
            $info['num_user'] = $info['num_add_user'] + $info['num_user'];
            unset($info['num_add_user']);
        }
        if (isset($info['num_add_ding']) && isset($info['num_ding'])) {
            $info['num_ding'] = $info['num_add_ding'] + $info['num_ding'];
            unset($info['num_add_ding']);
        }

        if (isset($info['name']) && $info['name']) {
            $info['name'] = Dever::cut($info['name'], 20, '...');
        }
        return $info;
    }

    protected function getSourceUid($error = true)
    {
        return Dever::load('invite/api')->getSourceUid($this->data['uid'], $error);
    }

    protected function log()
    {
        $input = Dever::json_encode(Dever::input());
        $url = Dever::url();
        Dever::log($url . '||' . $input, 'request');
    }
}