package org.dromara.mp.service.impl;

import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
import me.chanjar.weixin.mp.bean.tag.WxTagListUser;
import me.chanjar.weixin.mp.bean.tag.WxUserTag;
import org.dromara.mp.service.WxMpUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 微信公众号用户管理服务实现类
 *
 * @author pengles
 */
@Service
public class WxMpUserServiceImpl implements WxMpUserService {

    private static final Logger logger = LoggerFactory.getLogger(WxMpUserServiceImpl.class);

    @Autowired
    private WxMpService wxMpService;

    @Override
    public WxMpUser getUserInfo(String openId) {
        try {
            WxMpUser user = wxMpService.getUserService().userInfo(openId);
            logger.info("获取用户信息成功: {}", openId);
            return user;
        } catch (WxErrorException e) {
            logger.error("获取用户信息失败: {}", e.getMessage(), e);
            return null;
        }
    }

    @Override
    public WxMpUserList getUserList(String nextOpenId) {
        try {
            WxMpUserList userList = wxMpService.getUserService().userList(nextOpenId);
            logger.info("获取用户列表成功，数量: {}", userList.getCount());
            return userList;
        } catch (WxErrorException e) {
            logger.error("获取用户列表失败: {}", e.getMessage(), e);
            return null;
        }
    }

    @Override
    public List<String> getAllUsers() {
        try {
            List<String> allUsers = wxMpService.getUserService().userList().getOpenids();
            logger.info("获取所有用户成功，数量: {}", allUsers.size());
            return allUsers;
        } catch (WxErrorException e) {
            logger.error("获取所有用户失败: {}", e.getMessage(), e);
            return null;
        }
    }

    @Override
    public WxUserTag createTag(String tagName) {
        try {
            WxUserTag tag = wxMpService.getUserTagService().tagCreate(tagName);
            logger.info("创建标签成功: {}", tagName);
            return tag;
        } catch (WxErrorException e) {
            logger.error("创建标签失败: {}", e.getMessage(), e);
            return null;
        }
    }

    @Override
    public List<WxUserTag> getTags() {
        try {
            List<WxUserTag> tags = wxMpService.getUserTagService().tagGet();
            logger.info("获取标签列表成功，数量: {}", tags.size());
            return tags;
        } catch (WxErrorException e) {
            logger.error("获取标签列表失败: {}", e.getMessage(), e);
            return null;
        }
    }

    @Override
    public boolean updateTag(Long tagId, String tagName) {
        try {
            boolean result = wxMpService.getUserTagService().tagUpdate(tagId, tagName);
            logger.info("更新标签成功: {} -> {}", tagId, tagName);
            return result;
        } catch (WxErrorException e) {
            logger.error("更新标签失败: {}", e.getMessage(), e);
            return false;
        }
    }

    @Override
    public boolean deleteTag(Long tagId) {
        try {
            boolean result = wxMpService.getUserTagService().tagDelete(tagId);
            logger.info("删除标签成功: {}", tagId);
            return result;
        } catch (WxErrorException e) {
            logger.error("删除标签失败: {}", e.getMessage(), e);
            return false;
        }
    }

    @Override
    public boolean tagUsers(Long tagId, String[] openIds) {
        try {
            boolean result = wxMpService.getUserTagService().batchTagging(tagId, openIds);
            logger.info("批量打标签成功: {} 用户数: {}", tagId, openIds.length);
            return result;
        } catch (WxErrorException e) {
            logger.error("批量打标签失败: {}", e.getMessage(), e);
            return false;
        }
    }

    @Override
    public boolean untagUsers(Long tagId, String[] openIds) {
        try {
            boolean result = wxMpService.getUserTagService().batchUntagging(tagId, openIds);
            logger.info("批量取消标签成功: {} 用户数: {}", tagId, openIds.length);
            return result;
        } catch (WxErrorException e) {
            logger.error("批量取消标签失败: {}", e.getMessage(), e);
            return false;
        }
    }

    @Override
    public List<Long> getUserTags(String openId) {
        try {
            List<Long> tags = wxMpService.getUserTagService().userTagList(openId);
            logger.info("获取用户标签成功: {} 标签数: {}", openId, tags.size());
            return tags;
        } catch (WxErrorException e) {
            logger.error("获取用户标签失败: {}", e.getMessage(), e);
            return null;
        }
    }

    @Override
    public WxTagListUser getUsersByTag(Long tagId, String nextOpenId) {
        try {
            WxTagListUser userList = wxMpService.getUserTagService().tagListUser(tagId, nextOpenId);
            logger.info("获取标签下用户列表成功: {} 用户数: {}", tagId, userList.getCount());
            return userList;
        } catch (WxErrorException e) {
            logger.error("获取标签下用户列表失败: {}", e.getMessage(), e);
            return null;
        }
    }

    @Override
    public List<String> getTagUsers(Long tagId, String nextOpenId) {
        try {
            WxTagListUser userList = wxMpService.getUserTagService().tagListUser(tagId, nextOpenId);
            logger.info("获取标签下用户openId列表成功: {} 用户数: {}", tagId, userList.getCount());
            return userList.getData().getOpenidList();
        } catch (WxErrorException e) {
            logger.error("获取标签下用户openId列表失败: {}", e.getMessage(), e);
            return null;
        }
    }

    @Override
    public boolean batchTagging(Long tagId, List<String> openidList) {
        try {
            String[] openIds = openidList.toArray(new String[0]);
            boolean result = wxMpService.getUserTagService().batchTagging(tagId, openIds);
            logger.info("批量打标签成功: {} 用户数: {}", tagId, openIds.length);
            return result;
        } catch (WxErrorException e) {
            logger.error("批量打标签失败: {}", e.getMessage(), e);
            return false;
        }
    }

    @Override
    public boolean batchUntagging(Long tagId, List<String> openidList) {
        try {
            String[] openIds = openidList.toArray(new String[0]);
            boolean result = wxMpService.getUserTagService().batchUntagging(tagId, openIds);
            logger.info("批量取消标签成功: {} 用户数: {}", tagId, openIds.length);
            return result;
        } catch (WxErrorException e) {
            logger.error("批量取消标签失败: {}", e.getMessage(), e);
            return false;
        }
    }
}
