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

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

import java.util.List;
import java.util.Map;

import org.fouram.core.util.Tools;

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

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

/**
 * 企业微信科室工具类
 * 
 * @author Liup
 */
public class WXCpDepartUtil {

	public static void main(String[] args) throws WxErrorException {
		List<WxCpDepart> departs = getAllChildDeparts(1L);
		for (WxCpDepart depart : departs) {
			System.out.println(depart);
		}
	}

	public static List<WxCpDepart> getChildDeparts(Long id) throws WxErrorException {
		List<WxCpDepart> departs = Lists.newArrayList();
		for (WxCpDepart depart : departService.list(id)) {
			if (id.equals(depart.getParentId())) {
				departs.add(depart);
			}
		}
		return departs;
	}

	public static Map<String, Long> getChildDepartMap(Long id) throws WxErrorException {
		Map<String, Long> map = Maps.newHashMap();
		for (WxCpDepart depart : departService.list(id)) {
			if (id.equals(depart.getParentId())) {
				map.put(depart.getName(), depart.getId());
			}
		}
		return map;
	}

	public static Long create(String subCompany, String depatment) throws WxErrorException {
		Long subCompanyId = create(1L, subCompany);
		if (Tools.isEmpty(depatment)) {
			return subCompanyId;
		}
		return create(subCompanyId, depatment);
	}

	public static Long create(String firstOrg, String secondOrg, String thirdOrg, String fourthOrg)
			throws WxErrorException {
		Long firstOrgId = create(1L, firstOrg);
		if (Tools.isEmpty(secondOrg)) {
			return firstOrgId;
		}
		Long secondOrgId = create(firstOrgId, secondOrg);
		if (Tools.isEmpty(thirdOrg)) {
			return secondOrgId;
		}
		Long thirdOrgId = create(secondOrgId, thirdOrg);
		if (Tools.isEmpty(fourthOrg)) {
			return thirdOrgId;
		}
		return create(thirdOrgId, fourthOrg);
	}

	public static Long create(Long firstOrgWxDepartId, Long secondOrgWxDepartId, Long thirdOrgWxDepartId,
			Long fourthOrgWxDepartId) throws WxErrorException {
		if (secondOrgWxDepartId == null) {
			return firstOrgWxDepartId;
		}
		if (thirdOrgWxDepartId == null) {
			return secondOrgWxDepartId;
		}
		if (fourthOrgWxDepartId == null) {
			return thirdOrgWxDepartId;
		}
		return fourthOrgWxDepartId;
	}

	private static Long create(Long parentId, String depatment) throws WxErrorException {
		Map<String, Long> map = getChildDepartMap(parentId);
		Long depatmentId = map.get(depatment);
		if (depatmentId == null) {
			WxCpDepart depart = new WxCpDepart();
			depart.setName(depatment);
			depart.setParentId(parentId);
			depatmentId = departService.create(depart);
		}
		return depatmentId;
	}
	
	public static Long createOrUpdate(Long id, Long parentId, String name) throws WxErrorException {
		if(id == null) {
			Map<String, Long> map = getChildDepartMap(parentId);
			Long depatmentId = map.get(name);
			if (depatmentId == null) {
				WxCpDepart depart = new WxCpDepart();
				depart.setName(name);
				depart.setParentId(parentId);
				depatmentId = departService.create(depart);
			}
			return depatmentId;
		} else {
			Map<String, Long> map = getChildDepartMap(parentId);
			Long depatmentId = map.get(name);
			if(!id.equals(depatmentId)) {
				WxCpDepart depart = new WxCpDepart();
				depart.setId(id);
				depart.setName(name);
				depart.setParentId(parentId);
				departService.update(depart);
			}
			return id;
		}
	}

	public static List<WxCpDepart> getAllChildDeparts(Long id) throws WxErrorException {
		return departService.list(id);
	}

	public static void delete(Long id) throws WxErrorException {
		departService.delete(id);
	}
}
