<?php

namespace App\Services\WeChat;

class Tags extends Base
{


    /**
     * 创建标签
     * @param string $name
     * @return array
     * @throws WeChatException
     */
    public function create($name)
    {
        $access_token = $this->accessToken;
        $post = [
            'tag' => ['name' => $name],
        ];
        $url = "https://api.weixin.qq.com/cgi-bin/tags/create?access_token={$access_token}";
        $response = \Curl::to($url)
            ->withData(json_encode($post, JSON_UNESCAPED_UNICODE))
            ->asJsonResponse(true)
            ->post();
        if (isset($response['errcode']) && $response['errcode'] != 0) {
            throw new WeChatException($response['errmsg'], $response['errcode']);
        }

        return $response;
    }


    /**
     * @param int $id
     * @param string $name
     * @return bool
     * @throws WeChatException
     */
    public function update($id, $name)
    {
        $post = [
            'tag' => [
                'id' => $id,
                'name' => $name,
            ],
        ];
        $access_token = $this->accessToken;
        $url = "https://api.weixin.qq.com/cgi-bin/tags/update?access_token={$access_token}";
        $response = \Curl::to($url)
            ->withData(json_encode($post, JSON_UNESCAPED_UNICODE))
            ->asJsonResponse(true)
            ->post();
        if (isset($response['errcode']) && $response['errcode'] != 0) {
            throw new WeChatException($response['errmsg'], $response['errcode']);
        }

        return true;
    }


    /**
     * 删除标签
     * @param int $id
     * @return bool
     * @throws WeChatException
     */
    public function delete($id)
    {
        $access_token = $this->accessToken;
        $url = "https://api.weixin.qq.com/cgi-bin/tags/delete?access_token={$access_token}";
        $response = \Curl::to($url)->withData([
            'tag' => ['id' => $id],
        ])->asJson(true)->post();
        if (isset($response['errcode']) && $response['errcode'] != 0) {
            throw new WeChatException($response['errmsg'], $response['errcode']);
        }

        return true;
    }


    /**
     * 获取公众号已创建的标签
     * @param string|null $name
     * @return array|null
     * @throws WeChatException
     */
    public function get($name = null)
    {
        $access_token = $this->accessToken;
        $url = "https://api.weixin.qq.com/cgi-bin/tags/get?access_token={$access_token}";
        $response = \Curl::to($url)->asJson(true)->get();

        if (isset($response['errcode']) && $response['errcode'] != 0) {
            throw new WeChatException($response['errmsg'], $response['errcode']);
        }
        $tags = $response['tags'];
        if ($name) {
            $found_key = array_search($name, array_column($tags, 'name'));

            return is_int($found_key)
                ? array_column($tags, 'id')[$found_key]
                : null;
        }

        return $tags;
    }


    /**
     * 批量为用户打标签
     * @param array|string $openid_list
     * @param  int  $tag_id
     * @return bool
     * @throws WeChatException
     */
    public function batchTagging($openid_list, int $tag_id)
    {
        if (is_string($openid_list)) {
            $openid_list = [$openid_list];
        }
        $access_token = $this->accessToken;
        $url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token={$access_token}";
        $response = \Curl::to($url)->withData([
            'openid_list' => $openid_list,
            'tagid' => $tag_id,
        ])->asJson(true)->post();
        if (isset($response['errcode']) && $response['errcode'] != 0) {
            throw new WeChatException($response['errmsg'], $response['errcode']);
        }

        return true;
    }


    /**
     * 批量取消标签
     * @param string|array $openid_list
     * @param  int  $tag_id
     * @return bool
     * @throws WeChatException
     */
    public function batchUnTagging($openid_list, int $tag_id)
    {
        if (is_string($openid_list)) {
            $openid_list = [$openid_list];
        }
        $access_token = $this->accessToken;
        $url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging?access_token={$access_token}";
        $response = \Curl::to($url)->withData([
            'openid_list' => $openid_list,
            'tagid' => $tag_id,
        ])->asJson(true)->post();
        if (isset($response['errcode']) && $response['errcode'] != 0) {
            throw new WeChatException($response['errmsg'], $response['errcode']);
        }

        return true;
    }


    /**
     * 获取用户身上的标签列表
     * @param string $openid
     * @return array
     * @throws WeChatException
     */
    public function userTag($openid)
    {
        $access_token = $this->accessToken;
        $url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token={$access_token}";
        $response = \Curl::to($url)->withData([
            'openid' => $openid,
        ])->asJson(true)->post();
        if (isset($response['errcode']) && $response['errcode'] != 0) {
            throw new WeChatException($response['errmsg'], $response['errcode']);
        }

        return $response['tagid_list'];
    }
}
