<?php
namespace Api\PhpServices\Subscribe;

use Api\PhpUtils\Http\Request;

class WeMediaSubscribe extends Subscribe
{
    /**
     * @param int $userid
     * @param string $subscribe_id
     * @return array
     */
    public function add($userid, $subscribe_id)
    {
        if (empty($userid) || empty($subscribe_id)) {
            return $this->error(1);
        }
        $params = [
            "created_channels" => [
                [
                    "channel_id" => $subscribe_id,
                    "create_status" => 0,
                    "insert_at" => 0
                ]
            ]
        ];
        $url = config('interface', 'service.subscribe.add') . '?userid=' . $userid;
        $request = new Request();
        $ret = $request->post($url, $params);
        if (!isset($ret['response']['code']) || !empty($ret['response']['code']) || !isset($ret['response']['created_channels'])) {
            return $this->error(2);
        }
        if (isset($ret['response']['failed_channels']) && !empty($ret['response']['failed_channels'])) {
            return $this->error(2);
        }
        if (isset($ret['response']['repeat_channels']) && !empty($ret['response']['repeat_channels'])) {
            return $this->error(3);
        }
        return $this->success();
    }

    /**
     * @param int $userid
     * @param array $subscribe_ids 支持批量
     * @return array
     */
    public function delete($userid, $subscribe_ids)
    {
        if (empty($userid) || !is_array($subscribe_ids) || empty($subscribe_ids)) {
            return $this->error(1);
        }
        $params = [];
        foreach ($subscribe_ids as $k => $subscribe_id) {
            $params['deleted_channels'][$k]['channel_id'] = $subscribe_id;
        }
        $url = config('interface', 'service.subscribe.delete') . '?userid=' . $userid;
        $request = new Request();
        $ret = $request->post($url, $params);
        if (!isset($ret['response']['code']) || !empty($ret['response']['code'])) {
            return $this->error(4);
        }
        return $this->success();
    }

    /**
     * @param int $userid
     * @param int $page
     * @param int $page_size
     * @return array
     */
    public function getList($userid, $page, $page_size = 100)
    {
        if (empty($userid) || empty($page)) {
            return $this->error(1);
        }
        $url = config('interface', 'service.subscribe.get_list');
        $params = [
            'userid' => $userid,
            'start' => ($page - 1) * $page_size,
            'count' => $page_size
        ];
        $request = new Request();
        $ret = $request->get($url, $params);
        if (!isset($ret['response']['code']) || !empty($ret['response']['code']) || !isset($ret['response']['channels'])) {
            return $this->error(5);
        }
        $ret = $ret['response'];
        //提取所需字段
        $list = [
            'list' => [],
            'has_more' => $ret['hasmore']
        ];
        foreach ($ret['channels'] as $k => $v) {
            if (!isset($v['id']) || empty($v['id']) || !isset($v['name']) || empty($v['name'])) {
                continue;
            }
            $list['list'][$k]['subscribe_id'] = $v['fromId'] ?? '';
            $list['list'][$k]['name'] = $v['name'];
            $list['list'][$k]['wm_id'] = $v['fromId'] ?? '';
            $list['list'][$k]['image'] = $v['image'] ?? '';
            $list['list'][$k]['summary'] = $v['summary'] ?? '';
            $list['list'][$k]['authentication'] = $v['authentication'] ?? '';
        }
        return $this->success($list);
    }
}
