<?php
namespace Video\Lib;

use Dever;

class Live
{
	# 根据id 获取信息
    public function get($data, $uid = false)
    {
        if (!is_array($data)) {
            $data = Dever::db('video/live')->getOne($data);
        }
        
        if (!$data) {
            Dever::alert('错误的信息');
        }

        $data['content'] = Dever::filter($data['content']);
    
        //embed
        $time = time();
        $data['status'] = 1;
        $data['stream'] = '';
        if ($data['sdate'] <= $time && $time <= $data['edate']) {

            if ($data['stream_id'] && $data['stream_id'] > 0) {
                $stream = Dever::db('live/stream')->one($data['stream_id']);
                $data['stream'] = $stream['live'];
                $data['stream_rtmp'] = $stream['url_rtmp'];
                $data['stream_hls'] = $stream['url_hls'];
                $data['stream_hdl'] = $stream['url_hdl'];
                $data['stream_pic'] = $stream['url_pic'];
                $data['url'] = $data['stream_hls'];

                $source_type = Dever::input('source_type');
                if ($source_type != 'ios' && $source_type != 'android') {
                    //$data['stream_rtmp'] = $data['stream_hdl'];
                }
            } elseif (isset($data['stream_link']) && $data['stream_link']) {
                $data['stream'] = $data['stream_link'];
                $data['stream_rtmp'] = $data['stream_link'];
                $data['stream_hls'] = $data['stream_link'];
                $data['stream_hdl'] = $data['stream_link'];
                $data['stream_pic'] = $data['stream_link'];
                $data['url'] = $data['stream_hls'];
            }
            
            $data['status'] = 2;
        } elseif ($data['sdate'] > $time) {
            $data['status'] = 1;
        } elseif ($data['edate'] <= $time) {
            $data['status'] = 3;
            if (!$data['url']) {
                # 回放生成中
                $data['status'] = 3;
            }
        }

        if ($data['url'] && !isset($data['stream_rtmp'])) {
            $data = Dever::load('video/lib/vod')->avinfo($data, 'url', 'video/live');
        }

        if (Dever::input('test') == 1) {
            print_r($data);die;
        }

        $data['sdate_time'] = date('Y-m-d H:i:s', $data['sdate']);
        $data['edate_time'] = date('Y-m-d H:i:s', $data['edate']);
        
        return $data;
    }

    # 获取相关推荐
    public function getRelation($info)
    {
        $where['noid'] = $info['id'];
        $where['cate_id'] = $info['cate_id'];
        return Dever::db('video/live')->getRelation($where);
    }

    # 增加浏览量
    public function addView($id)
    {
        Dever::db('video/live')->addView($id);
    }
}
