package net.wicp.tams.app.duckula.controller.service.cas.impl;

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

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.tapestry5.commons.util.CollectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import lombok.extern.slf4j.Slf4j;
import net.wicp.tams.app.duckula.controller.bean.constant.ResAuthType;
import net.wicp.tams.app.duckula.controller.bean.models.SysResource;
import net.wicp.tams.app.duckula.controller.bean.models.SysRole;
import net.wicp.tams.app.duckula.controller.dao.SysResourceMapper;
import net.wicp.tams.app.duckula.controller.service.cas.IMenuService;
import net.wicp.tams.common.apiext.CollectionUtil;
import net.wicp.tams.common.apiext.json.EasyUiAssist;
import net.wicp.tams.common.apiext.json.easyuibean.EasyUINode;
import net.wicp.tams.common.apiext.json.easyuibean.EasyUINodeConf;
import net.wicp.tams.common.callback.IConvertValue;
import net.wicp.tams.common.constant.dic.YesOrNo;

@Slf4j
@Service
public class MenuService implements IMenuService {

	@Autowired
	private SysResourceMapper sysResourceMapper;

	@Override
	public List<SysResource> findAllModule(List<SysRole> roles, boolean isRoot) {
		if (CollectionUtils.isEmpty(roles)) {
			return CollectionFactory.newList();
		}
		QueryWrapper<SysResource> queryWrapper = new QueryWrapper<SysResource>();
		if (isRoot) {
			queryWrapper.eq("res_level", 1);
		} else {
			queryWrapper.gt("res_level", 1);
		}
		List<SysResource> retlist = sysResourceMapper.selectList(queryWrapper);
		return retlist;
	}

	@Override
	public List<EasyUINode> findModuleMenuForRoles(final int moduleId, List<SysRole> roles, IConvertValue I18NConvert) {
		// 查询该用户的所有模块资源
		List<SysResource> resList = this.findAllModule(roles, false);
		EasyUINodeConf conf = new EasyUINodeConf("id", "resName", "caResource.id");
		conf.setAttrCols("resValue", "caResType.id");
		conf.setIsRoot(new Predicate() {
			@Override
			public boolean evaluate(Object object) {
				int parentId = Integer.parseInt(String.valueOf(object));
				return parentId == moduleId;
			}
		});
		conf.setTextConvert(I18NConvert);
		List<EasyUINode> ret = CollectionFactory.newList();
		try {
			ret = EasyUiAssist.getTreeRoot(resList, conf);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	@Override
	public List<EasyUINode> findAllModuleMenu(IConvertValue i18NConvert, boolean hasUnallot, boolean hasDataResource,
			boolean isSuper) {
		QueryWrapper<SysResource> queryWrapper = new QueryWrapper<SysResource>();
		if (!hasDataResource) {
			queryWrapper.gt("res_level", 0);
		}
		if (!hasUnallot) {
			// queryWrapper.isNotNull("parent_id");// 父节点不能为空
		}
		if (!isSuper) {
			queryWrapper.eq("is_super", YesOrNo.no.name());
		}

		List<SysResource> resList = sysResourceMapper.selectList(queryWrapper);
		EasyUINodeConf conf = new EasyUINodeConf("id", "resName", "parentId", "showOrder");
		// conf.setTextConvert(i18NConvert);

		conf.setIconClsCol("icon");
		conf.setAttrCols("isEdit", "resType", "resLevel", "resCode", "resName", "resValue", "remark", "bound1",
				"bound2");

		if (hasDataResource) {
			conf.setIsRoot(new Predicate() {
				@Override
				public boolean evaluate(Object object) {
					Long parentId = new Long(String.valueOf(object)); // Long.parseLong(String.valueOf(object));
					// return parentId == 1;// 设置所有的模块为树的根
					return parentId == null;
				}
			});
		} else {
			conf.setIsRoot(new Predicate() {
				@Override
				public boolean evaluate(Object object) {
					long parentId = Long.parseLong(String.valueOf(object));
					return parentId == 1;// 设置所有的模块为树的根
				}
			});
		}

		if (hasUnallot) {
			// 构造虚拟的节点
			SysResource unAllot = new SysResource();
			unAllot.setId(-1l);
			unAllot.setResName("未分配的资源");
			unAllot.setResValue("0");
			unAllot.setResType(ResAuthType.DIR.name());
			unAllot.setIcon("icon-folder-star");
			unAllot.setResLevel(1);
			unAllot.setParentId(1l);
			unAllot.setShowOrder(10000l);
			resList.add(unAllot);
			// 把未分配的节点全放到它目录下
			for (SysResource res : resList) {
				if (res.getParentId() == null && res.getId() != 1l && res.getId() != 2l) {
					res.setParentId(-1l);
				}
			}
		}

		List<EasyUINode> ret = CollectionFactory.newList();
		try {
			ret = EasyUiAssist.getTreeRoot(resList, conf);
		} catch (Exception e) {
			log.error("构造资源树出错", e);
		}
		return ret;
	}

	@Override
	public List<SysResource> findSyncRes() {
		QueryWrapper<SysResource> queryWrapper = new QueryWrapper<SysResource>();
		queryWrapper.eq("is_sync", "yes");
		return sysResourceMapper.selectList(queryWrapper);
	}

	@Override
	public Set<Long> findSuperResId() {
		QueryWrapper<SysResource> queryWrapper = new QueryWrapper<SysResource>();
		queryWrapper.eq("is_super", "yes");
		List<SysResource> selectList = sysResourceMapper.selectList(queryWrapper);
		Set<Long> ids = CollectionUtil.getColSetFromObj(selectList, "id");
		return ids;
	}



}
