package cn.hmsoft.weixin.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.hmsoft.frame.data.model.FrameOptr;
import cn.hmsoft.frame.data.model.FrameThread;
import cn.hmsoft.frame.service.FrameService;
import cn.hmsoft.frame.thread.FrameThreadHelper;
import cn.hmsoft.frame.util.FrameAssertUtil;
import cn.hmsoft.helper.StringHelper;
import cn.hmsoft.weixin.data.dao.WxUserDao;
import cn.hmsoft.weixin.data.model.WxUser;
import cn.hmsoft.weixin.data.model.WxUserTag;
import cn.hmsoft.weixin.entity.response.UserGetResponseJson;
import cn.hmsoft.weixin.request.MemberBatchTaggingRequest;
import cn.hmsoft.weixin.request.MemberBatchUnTaggingRequest;
import cn.hmsoft.weixin.request.UserGetRequest;
import cn.hmsoft.weixin.request.UserRemarkRequest;
import cn.hmsoft.weixin.request.UserinfoRequest;

@Service
public class WxUserService extends FrameService {

	@Autowired
	private WxUserDao daoUser;

	public void modifyUserTags(int wuser_id, Integer[] tag_ids, FrameOptr optr) {
		WxUser user = this.daoUser.find(wuser_id);
		FrameAssertUtil.isNotNull(user, "未找到您操作的微信用户");
		// 去服务器请求
		List<WxUserTag> list = this.daoUser.list(WxUserTag.class, "open_id", user.getOpen_id());
		// 比较增加还是删除
		for (WxUserTag tag : list) {
			boolean remove = true;
			for (int tag_id : tag_ids) {
				if (tag.getTag_id() - tag_id == 0) {
					remove = false;
					break;
				}
			}
			if (remove) {
				MemberBatchUnTaggingRequest tagReq = new MemberBatchUnTaggingRequest(tag.getTag_id(),
						user.getOpen_id());
				tagReq.setWxDefine(user.getWx_id());
				tagReq.request();
			}
		}

		for (int tag_id : tag_ids) {
			boolean add = true;
			for (WxUserTag tag : list) {
				if (tag.getTag_id() - tag_id == 0) {
					add = false;
					break;
				}
			}
			if (add) {
				MemberBatchTaggingRequest tagReq = new MemberBatchTaggingRequest(tag_id, user.getOpen_id());
				tagReq.setWxDefine(user.getWx_id());
				tagReq.request();

			}
		}
	}

	public void modifyUserRemark(int wx_id, String open_id, String remark, FrameOptr optr) {
		FrameAssertUtil.checkParam(open_id);
		UserRemarkRequest rq = new UserRemarkRequest(open_id, remark);
		rq.setWxDefine(wx_id);
		rq.request();

	}

	public void syncUser(int wx_id, String open_id, FrameOptr optr) {
		FrameAssertUtil.checkParam(open_id);
		UserinfoRequest rq = new UserinfoRequest(open_id);
		rq.setWxDefine(wx_id);
		rq.request();

	}

	public void syncUserAll(FrameThread thread, int wx_id, FrameOptr optr) {
		UserGetRequest rq = new UserGetRequest("");
		rq.setWxDefine(wx_id);
		UserGetResponseJson json = rq.request();

		FrameThreadHelper.updateThread(thread, json.getCount() * 100 / (json.getTotal() * 2),
				"一共【" + json.getTotal() + "】用户，已经拉取了【" + (json.getCount()) + "】个用户");
		// 同步所有用户
		int fetchCount = json.getCount();
		while (StringHelper.isNotEmpty(json.getNext_openid())) {
			rq = new UserGetRequest(json.getNext_openid());
			rq.setWxDefine(wx_id);
			json = rq.request();
			fetchCount += json.getCount();
			FrameThreadHelper.updateThread(thread, json.getCount() * 100 / (json.getTotal() * 2),
					"一共【" + json.getTotal() + "】用户，已经拉取了【" + (fetchCount) + "】个用户");

		}
		// 获取用户详细信息
		int total = 1;
		for (WxUser user : this.daoUser.all()) {
			UserinfoRequest ur = new UserinfoRequest(user.getOpen_id());
			ur.setWxDefine(wx_id);
			ur.request();
			total++;
			if (total % 500 == 0) {
				// 每100条记录下
				FrameThreadHelper.updateThread(thread, 50 + total * 100 / (fetchCount * 2),
						"一共【" + json.getTotal() + "】用户，已经同步【" + (total) + "】个用户");
			}
		}

		FrameThreadHelper.completeThread(thread, "同步成功：共同步了【" + fetchCount + "】个关注用户");
	}

	public void logMessage(FrameOptr optr, String open_id, String content) {
		this.log(optr, "SendTextMsg", open_id, content);
	}
}
