<?php namespace Api\Video\Remote;
use Dever;
class Base
{
    protected $type = '';
    protected $local = false;
    protected $site = array();
    protected $video = array();

    protected function getVideo($txt, $local)
    {
        $this->local = $local;
        if (!$this->type) {
            Dever::error('不支持的站点类型');
        }
        $this->site = Dever::db('site', 'api')->find(array('key' => $this->type));
        if (!$this->site) {
            Dever::error('不支持的站点类型');
        }
        if ($this->site['status'] == 2) {
            Dever::error('不支持的站点类型');
        }
        if (is_string($txt) && strstr($txt, 'http')) {
            $url = \Dever\Helper\Str::getLink($txt);
            if (!$url || !strstr($url, 'http')) {
                Dever::error('链接解析失败');
            }
            $headers = get_headers($url, 1);
            $redirect_url = $headers['Location'] ?? $url;
            if ($redirect_url) {
                $url = $redirect_url;
            }
            // 获取视频id
            preg_match('/(\d+)/', $url, $matches);
            if (empty($matches)) {
                Dever::error('视频id获取失败');
            }
            $id = $matches[1];
        } else {
            $id = $txt;
        }
        if ($this->local) {
            $data['site_id'] = $this->site['id'];
            $data['fid'] = $id;
            $this->video = Dever::db('video', 'api')->find($data);
            if ($this->video && $this->video['file']) {
                $extend = Dever::db('video_extend', 'api')->find(array('vid' => $this->video['id']));
                if ($extend && $extend['remote']) {
                    $remote = Dever::json_decode($extend['remote']);
                    $remote['video']['url'] = $this->video['file'];
                    return $remote;
                }
            }
        }
        return $id;
    }

    protected function saveVideo($info)
    {
        if ($this->local && !$this->video) {
            $data['site_id'] = $this->site['id'];
            $data['fid'] = $info['id'];
            $data['name'] = $info['title'] ? $info['title'] : $info['desc'];
            $data['desc'] = $info['desc'];
            $upload = Dever::load('save', 'upload')->act(3, $info['video']['url'], '', false, md5($data['site_id'] . '_' . $data['fid']));
            $info['video']['url'] = $upload['url'];
            $data['file'] = $info['video']['url'];
            $data['format'] = $info['video']['format'];
            $data['ratio'] = $info['video']['ratio'];
            $data['height'] = $info['video']['height'];
            $data['width'] = $info['video']['width'];
            $data['duration'] = $info['video']['duration'];
            $data['author'] = $info['author']['nickname'];
            $data['pdate'] = $info['cdate'];
            $info['vid'] = Dever::db('video', 'api')->insert($data);
            if ($info['vid']) {
                Dever::db('video_extend', 'api')->insert(array('vid' => $info['vid'], 'remote' => Dever::json_encode($info)));
            }
        }
        return $info;
    }
}