<?php
namespace Api\PhpServices\WeMedia;

use Api\PhpUtils\Http\Request;

class WeMediaService
{
    /**
     * 错误码
     */
    const ERROR_CODE = [
        1 => '参数有误',
        2 => '获取一点号作者信息失败',
        3 => '获取一点号作者文章流失败',
        4 => '获取一点号作者视频流失败'
    ];

    /**
     * 成功响应
     * @param null $data
     * @return array|int[]
     */
    private function success($data = null)
    {
        if (is_null($data)) {
            return ['code' => 0];
        }
        return ['code' => 0, 'data'=>$data];
    }

    /**
     * 异常响应
     * @param $code
     * @return array
     */
    private function error($code)
    {
        return ["code" => $code, "msg" => self::ERROR_CODE[$code]];
    }

    /**
     * 获取一点号作者信息
     * @param int $wm_id 比如m230600
     * @return array
     */
    public function getInfo($userid, $wm_id)
    {
        if (empty($wm_id)) {
            return $this->error(1);
        }
        $params = [
            'userid' => intval($userid),
            'wm_id' => $wm_id
        ];
        $request = new Request();
        $ret = $request->get(config('interface', 'service.wemedia.get_profile_info'), $params);
        if (!empty($ret['response']['code']) || !isset($ret['response']['result']) || empty($ret['response']['result'])) {
            return $this->error(2);
        }
        $ret = $ret['response']['result'];
        if(!isset($ret['profile_info']) || empty($ret['profile_info']) || !isset($ret['profile_navigation']['columns']) || empty($ret['profile_navigation']['columns'])){
            return $this->error(2);
        }
        //填充数据
        $profile_info = $ret['profile_info'];
        $profile_navigation = $ret['profile_navigation']['columns'];
        $result = [
            'wm_id' => 'm' . ltrim($profile_info['id'], 'm'),
            'uid' => $profile_info['uid'],
            'media_name' => $profile_info['media_name'],
            'media_pic' => $profile_info['media_pic'],
            'authentication' => $profile_info['authentication'],
            //发文数
            'postcount' => $profile_info['postcount'],
            //阅读量
            'account_click' => $profile_info['account_click'],
            //粉丝数
            'follower_count' => $profile_info['follower_count'],
            //订阅数
            'following_count' => $profile_info['following_count'],
            //该wm_id是否已被userid订阅
            'is_subscribed' => $profile_info['is_subscribed'],
            'subscribe_id' => $profile_info['subscribe_id'],
            'navigation' => $profile_navigation
        ];
        return $this->success($result);
    }

    /**
     * 获取订阅关系
     * @param int $userid
     * @param array $wm_ids ['m10001','m10002']
     * @return array
     */
    public function getSubscribedStatus($userid, $wm_ids){
        if (empty($userid) || !is_array($wm_ids) || empty($wm_ids)) {
            return $this->error(1);
        }
        $url = config('interface', 'service.subscribe.get_list');
        $params = [
            'userid' => $userid,
            'start' => 0,
            'count' => 10000
        ];
        $request = new Request();
        $ret = $request->get($url, $params);
        if (!isset($ret['response']) || empty($ret['response'])) {
            return $this->error(2);
        }
        $ret = $ret['response'];
        if (!isset($ret['code']) || !empty($ret['code']) || !isset($ret['channels'])) {
            return $this->error(2);
        }
        $result = [];
        //设置默认值false
        foreach ($wm_ids as $k => $wm_id) {
            $result[$wm_id]['is_subscribed'] = false;
            $result[$wm_id]['subscribe_id'] = '';
        }
        //匹配是否订阅
        foreach ($ret['channels'] as $k => $v) {
            if(isset($v['fromId']) && in_array($v['fromId'], $wm_ids)){
                $result[$v['fromId']]['is_subscribed'] = true;
                $result[$v['fromId']]['subscribe_id'] = $v['fromId'];
            }
        }
        return $this->success($result);
    }

    public function getDocFlow($wm_id, $page)
    {
        if (empty($wm_id)) {
            return $this->error(1);
        }
        $params = [
            'wm_id' => $wm_id,
            'cstart' => ($page - 1) * 30,
        ];
        $request = new Request();
        $ret = $request->get(config('interface', 'service.wemedia.get_news_list'), $params);
        if (!empty($ret['response']['code']) || !isset($ret['response']['result']) || empty($ret['response']['result'])) {
            return $this->error(3);
        }
        return $this->success($ret['response']['result']);
    }

    public function getVideoFlow($wm_id, $page)
    {
        if (empty($wm_id)) {
            return $this->error(1);
        }
        $params = [
            'wm_id' => $wm_id,
            'cstart' => ($page - 1) * 30,
        ];
        $request = new Request();
        $ret = $request->get(config('interface', 'service.wemedia.get_video_list'), $params);
        if (!empty($ret['response']['code']) || !isset($ret['response']['result']) || empty($ret['response']['result'])) {
            return $this->error(4);
        }
        return $this->success($ret['response']['result']);
    }
}
