package org.fouram.core.plugin.weixin.cp.util;

import static org.fouram.core.plugin.weixin.cp.core.WXCpBase.userService;

import java.util.List;
import java.util.Set;

import org.fouram.constants.WebConstants;
import org.fouram.core.util.Tools;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.bean.Gender;
import me.chanjar.weixin.cp.bean.WxCpDepart;
import me.chanjar.weixin.cp.bean.WxCpUser;
import me.chanjar.weixin.cp.bean.WxCpUser.ExternalAttribute;

/**
 * 企业微信用户工具类
 * 
 * @author Liup
 */
public class WXCpUserUtil {

	public static String createOrUpdateForTest(String userId, String name, String[] subCompanyList,
			String[] departmentList, String position, String mobile, String gender, String email) {
		WxCpUser user = getByIdOrEmail(userId, email);
		try {
			if (user == null) {
				user = getUserForTest(userId, name, subCompanyList, departmentList, position, mobile, gender, email);
				userService.create(user);
			} else {
				user = getUserForTest(user.getUserId(), name, subCompanyList, departmentList, position, mobile, gender,
						email);
				userService.update(user);
			}
			return WebConstants.SUCCESS;
		} catch (WxErrorException e) {
			return e.getMessage();
		}
	}

	public static String createOrUpdate(String userId, String name, Long[] firstOrgWxDepartIdList,
			Long[] secondOrgWxDepartIdList, Long[] thirdOrgWxDepartIdList, Long[] fourthOrgWxDepartIdList,
			String position, String mobile, String gender, String email, String telephone, String firstOrgName) {
		WxCpUser user = getByIdOrEmail(userId, email);
		try {
			if (user == null) {
				user = getUser(userId, name, firstOrgWxDepartIdList, secondOrgWxDepartIdList, thirdOrgWxDepartIdList,
						fourthOrgWxDepartIdList, position, mobile, gender, email, telephone, firstOrgName);
				userService.create(user);
			} else {
				user = getUser(user.getUserId(), name, firstOrgWxDepartIdList, secondOrgWxDepartIdList,
						thirdOrgWxDepartIdList, fourthOrgWxDepartIdList, position, mobile, gender, email, telephone,
						firstOrgName);
				userService.update(user);
			}
			return "SUCCESS";
		} catch (WxErrorException e) {
			return e.getMessage();
		}
	}

	private static WxCpUser getUser(String userId, String name, Long[] firstOrgWxDepartIdList,
			Long[] secondOrgWxDepartIdList, Long[] thirdOrgWxDepartIdList, Long[] fourthOrgWxDepartIdList,
			String position, String mobile, String gender, String email, String telephone, String firstOrgName)
			throws WxErrorException {
		WxCpUser user = new WxCpUser();
		if (Tools.notEmpty(userId)) {
			user.setUserId(userId);
		}
		if (Tools.notEmpty(name)) {
			user.setName(name);
		}
		Long[] departIds = new Long[firstOrgWxDepartIdList.length];
		for (int i = 0; i < firstOrgWxDepartIdList.length; i++) {
			departIds[i] = WXCpDepartUtil.create(firstOrgWxDepartIdList[i], secondOrgWxDepartIdList[i],
					thirdOrgWxDepartIdList[i], fourthOrgWxDepartIdList[i]);
		}
		user.setDepartIds(departIds);
		if (Tools.notEmpty(position)) {
			user.setPosition(position);
		}
		if (Tools.notEmpty(mobile)) {
			user.setMobile(mobile);
		}
		if (Tools.notEmpty(gender)) {
			user.setGender(Gender.MALE.getGenderName().equals(gender) ? Gender.MALE : Gender.FEMALE);
		}
		if (Tools.notEmpty(email)) {
			user.setEmail(email);
		}
		if (Tools.notEmpty(telephone)) {
			user.setTelephone(telephone);
		}
		user.setToInvite(false);
		ExternalAttribute attribute = ExternalAttribute.builder().name("所属公司").type(0).value(firstOrgName).build();
		user.addExternalAttr(attribute);
		return user;
	}

	public static void updateUser(WxCpUser user) throws WxErrorException {
		userService.update(user);
	}

	private static WxCpUser getUserForTest(String userId, String name, String[] subCompanyList, String[] departmentList,
			String position, String mobile, String gender, String email) throws WxErrorException {
		WxCpUser user = new WxCpUser();
		if (Tools.notEmpty(userId)) {
			user.setUserId(userId);
		}
		if (Tools.notEmpty(name)) {
			user.setName(name);
		}
		Long[] departIds = new Long[subCompanyList.length];
		for (int i = 0; i < subCompanyList.length; i++) {
			departIds[i] = WXCpDepartUtil.create(subCompanyList[i], departmentList[i]);
		}
		user.setDepartIds(departIds);
		if (Tools.notEmpty(position)) {
			user.setPosition(position);
		}
		if (Tools.notEmpty(mobile)) {
			user.setMobile(mobile);
		}
		if (Tools.notEmpty(gender)) {
			user.setGender(Gender.MALE.getGenderName().equals(gender) ? Gender.MALE : Gender.FEMALE);
		}
		if (Tools.notEmpty(email)) {
			user.setEmail(email);
		}
		user.setToInvite(false);
		ExternalAttribute attribute = ExternalAttribute.builder().name("所属公司").type(0).value(subCompanyList[0]).build();
		user.addExternalAttr(attribute);
		return user;
	}

	public static WxCpUser getByIdOrEmail(String userId, String email) {
		try {
			WxCpUser user = userService.getById(userId);
			if (user != null) {
				return user;
			}
		} catch (WxErrorException e) {
		}
		try {
			WxCpUser user = userService.getById(email);
			if (user != null) {
				return user;
			}
		} catch (WxErrorException e) {
		}
		return null;
	}

	public static void delete(String userId) {
		try {
			userService.delete(userId);
		} catch (WxErrorException e) {
		}
	}

	public static List<WxCpUser> listByDepartment(Long departId) throws WxErrorException {
		return userService.listByDepartment(departId, true, 0);
	}

	public static List<WxCpUser> listAllUser() throws WxErrorException {
		List<WxCpUser> result = Lists.newArrayList();
		Set<String> set = Sets.newHashSet();
		List<WxCpDepart> departs = WXCpDepartUtil.getAllChildDeparts(1L);
		for (WxCpDepart depart : departs) {
			List<WxCpUser> users = WXCpUserUtil.listByDepartment(depart.getId());
			for (WxCpUser user : users) {
				if(!set.contains(user.getUserId())) {
					result.add(user);
					set.add(user.getUserId());
				}
			}
		}
		return result;
	}

	public static void main(String[] args) throws WxErrorException {
		System.out.println(listAllUser().size());
	}
}
