<?php

namespace Push\Lib;

use Dever;

class Data
{
    public function get($key, $name, $total = 10, $page = false, $limit = false)
    {
        $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'];
        }

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

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

            
            if (checkVersion()->video == 2) {
                $where['type_no'] = 2;
            }

            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->getOne($v);
                }
            }
        }

        return $data;
    }

    private function getOne($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'];
            } elseif ($info['type'] == 2) {
                $id = $info['vod_id'];
            } elseif ($info['type'] == 3) {
                $id = $info['live_id'];
            } elseif ($info['type'] == 4) {
                $id = $info['journal_id'];
                $method = 'getOne';
            } elseif ($info['type'] == 6) {
                $id = $info['feature_id'];
            } elseif ($info['type'] == 7) {
                $id = $info['applet_id'];
            } elseif ($info['type'] == 13) {
                $id = $info['journal_id'];
            }

            unset($info['article_id']);
            unset($info['vod_id']);
            unset($info['live_id']);
            unset($info['journal_id']);
            unset($info['feature_id']);
            unset($info['applet_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['pic_cover_169'])) {
                        $info['pic_169'] = $data['pic_cover_169'];
                    }
                    if (isset($data['pic_view_cover']) && $data['pic_view_cover'] && !$info['pic']) {
                        $info['pic'] = $data['pic_view_cover'];
                    }

                    if (isset($data['num_ding'])) {
                        $info['num_ding'] = $data['num_ding'];
                    }

                    if (isset($data['buy'])) {
                        $info['buy'] = $data['buy'];
                    }

                    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'];
                    }

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

                    if (isset($data['link']) && $data['link']) {
                        $info['link'] = $data['link'];
                    }
                }
            }
        } else {
            unset($info['article_id']);
            unset($info['vod_id']);
            unset($info['live_id']);
            unset($info['journal_id']);
            unset($info['feature_id']);
            unset($info['applet_id']);
        }

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

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

        if (isset($info['pic']) && $info['pic']) {
            $info['pic'] = Dever::load('main/lib/core')->replacePic($info['pic']);
        }

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

        return $info;
    }
}