/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.flowable.utils;

import com.jeeplus.common.utils.Encodes;
import com.jeeplus.common.utils.SpringContextHolder;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.TimeUtils;
import com.jeeplus.config.properties.JeePlusProperites;
import com.jeeplus.modules.flowable.entity.Flow;
import com.jeeplus.modules.sys.entity.Role;
import com.jeeplus.modules.sys.entity.User;
import net.sf.json.util.JSONUtils;
import org.flowable.editor.constants.ModelDataJsonConstants;
import org.flowable.engine.repository.Model;
import org.flowable.idm.engine.impl.persistence.entity.GroupEntity;
import org.flowable.idm.engine.impl.persistence.entity.GroupEntityImpl;
import org.flowable.idm.engine.impl.persistence.entity.UserEntity;
import org.flowable.idm.engine.impl.persistence.entity.UserEntityImpl;

import java.util.HashMap;
import java.util.Map;

/**
 * 流程工具
 * @version 2018-08-03
 */
public class FlowableUtils {

	private static JeePlusProperites jeePlusProperites = SpringContextHolder.getBean(JeePlusProperites.class);


	/**
	 * 获取流程表单URL
	 * @param formKey
	 * @param flow 表单传递参数
	 * @return
	 */
	public static String getFormUrl(String formKey, Flow flow){

		StringBuilder formUrl = new StringBuilder();
		formUrl.append(formKey).append(formUrl.indexOf("?") == -1 ? "?" : "&");
		formUrl.append("act.taskId=").append(flow.getTaskId() != null ? flow.getTaskId() : "");
		formUrl.append("&act.taskName=").append(flow.getTaskName() != null ? Encodes.urlEncode(flow.getTaskName()) : "");
		formUrl.append("&act.taskDefKey=").append(flow.getTaskDefKey() != null ? flow.getTaskDefKey() : "");
		formUrl.append("&act.procInsId=").append(flow.getProcInsId() != null ? flow.getProcInsId() : "");
		formUrl.append("&act.procDefId=").append(flow.getProcDefId() != null ? flow.getProcDefId() : "");
		formUrl.append("&act.status=").append(flow.getStatus() != null ? flow.getStatus() : "");
		formUrl.append("&id=").append(flow.getBusinessId() != null ? flow.getBusinessId() : "");

		return formUrl.toString();
	}

	public static UserEntity toFlowableUser(User user){
		if (user == null){
			return null;
		}
		UserEntity userEntity = new UserEntityImpl();
		userEntity.setId(user.getId());
		userEntity.setFirstName(user.getName());
		userEntity.setLastName(StringUtils.EMPTY);
		userEntity.setPassword(user.getPassword());
		userEntity.setEmail(user.getEmail());
		userEntity.setRevision(1);
		return userEntity;
	}

	public static GroupEntity toFlowableGroup(Role role){
		if (role == null){
			return null;
		}
		GroupEntity groupEntity = new GroupEntityImpl();
		groupEntity.setId(role.getEnname());
		groupEntity.setName(role.getName());
//		groupEntity.setType(role.getRoleType());
		groupEntity.setRevision(1);
		return groupEntity;
	}


	public static String toTimeString(long time) {
		TimeUtils t = new TimeUtils(time);
		int day = t.get(TimeUtils.DAY);
		int hour = t.get(TimeUtils.HOUR);
		int minute = t.get(TimeUtils.MINUTE);
		int second = t.get(TimeUtils.SECOND);
		StringBuilder sb = new StringBuilder();
		if (day > 0){
			sb.append(day).append("天");
		}
		if (hour > 0){
			sb.append(hour).append("时");
		}
		if (minute > 0){
			sb.append(minute).append("分");
		}
		if (second > 0){
			sb.append(second).append("秒");
		}
		return sb.toString();
	}

	public static Model buildModel(Model model, String categoryId, String name, String key, String description) {
		Map map = new HashMap();
		map.put("category", categoryId);
		map.put("key", key);
		map.put(ModelDataJsonConstants.MODEL_NAME, name);
		map.put(ModelDataJsonConstants.MODEL_REVISION, 1);
		map.put(ModelDataJsonConstants.MODEL_DESCRIPTION, description);
		model.setMetaInfo(JSONUtils.valueToString(map));
		model.setKey(key);
		model.setName(name);
		model.setCategory(categoryId);
		model.setKey(key);
		return model;
	}
}
