<?php

namespace Push\Lib;

use Dever;

class Data
{
    public function get($key, $name, $total = 10, $page = false, $limit = false)
    {
        $info = $this->getInfo($key, $name, $total);

        $data = array();
        if ($info) {
            if (!$info['num']) {
                $info['num'] = 20;
            }
            if ($limit > 0) {
                $info['num'] = $limit;
            }

            $where = array();
            $where['info_id'] = $info['id'];

            if ($page) {
                $where['page'] = array($info['num'], 'list');
                $data = Dever::db('push/data')->getAllPage($where);
            } else {
                $where['limit'] = '0,' . $info['num'];
                $data = Dever::db('push/data')->getAll($where);
            }

            if ($data) {
                foreach ($data as $k => $v) {
                    $data[$k] = $this->getData($v);
                }
            }
        }

        return $data;
    }

    public function getOne($key, $name, $total = 10, $data_name)
    {
        $info = $this->getInfo($key, $name, $total);
        $data = array();
        if ($info) {
            $where = array();
            $where['info_id'] = $info['id'];
            $where['name'] = $data_name;
            $data = Dever::db('push/data')->one($where);
            if ($data) {
                $data = $this->getData($data);
            } else {
                $where['type'] = 10;
                Dever::db('push/data')->insert($where);
            }
        }

        return $data;
    }

    private function getInfo($key, $name, $total)
    {
        $info = Dever::db('push/info')->one(array('key' => $key));

        if (!$info) {
            $insert['key'] = $key;
            $insert['name'] = $name;
            $insert['num'] = $total;
            $insert['col'] = '1,3';
            $info['id'] = Dever::db('push/info')->insert($insert);
            $info['num'] = $insert['num'];
        }

        return $info;
    }

    private function getData($info)
    {
        $info['id'] = 0;
        if ($info['type'] < 10 || $info['type'] == 13) {
            $table = Dever::config('base')->type_table[$info['type']];
            $method = 'one';
            if ($info['type'] == 1) {
                $id = $info['article_id'];
            }

            unset($info['article_id']);
            if ($id > 0) {
                $info['id'] = $id;
                $data = Dever::db($table)->$method($id);
                if (isset($data) && $data) {
                    if (isset($data['name']) && $data['name'] && !$info['name']) {
                        $info['name'] = $data['name'];
                    }

                    if (isset($data['pic_cover']) && $data['pic_cover'] && !$info['pic']) {
                        $info['pic'] = $data['pic_cover'];
                    }

                    if (isset($data['pdate']) && $data['pdate']) {
                        $info['cdate'] = $data['pdate'];
                    }

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

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

                    $info['link'] = Dever::load('main/content')->link($data);

                    if (isset($data['template']) && $data['template'] == 2) {
                        $info['name'] = '<i class="ico-video"></i>' . $info['name'];
                    }
                }
            }
        } else {
            unset($info['article_id']);
            if (isset($info['olink']) && $info['olink']) {
                $info['link'] = $info['olink'];
            }
        }

        if ($info['link']) {
            if (!strstr($info['link'], 'http')) {
                $info['link'] = 'http://' . $info['link'];
            }
        }

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

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

        return $info;
    }
}