package org.springblade.flow.util;

import java.util.ArrayList;
import java.util.List;

import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.flow.base.UserInfo;
import org.springblade.flow.model.login.PcUserVO;
import org.springblade.flow.model.login.UserCommonInfoVO;
import org.springblade.flow.model.login.UserPositionVO;
import org.springblade.flow.permission.entity.PositionEntity;
import org.springblade.flow.permission.entity.UserEntity;
import org.springblade.flow.permission.service.ModuleServicePlatomix;
import org.springblade.flow.permission.service.OrganizeService;
import org.springblade.flow.permission.service.PositionService;
import org.springblade.flow.permission.service.User4FlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.hutool.core.collection.CollUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2021/3/16 8:59
 */
@Slf4j
@Component
public class UserProvider {

	@Autowired
	private OrganizeService deptService;

	@Autowired
	private PositionService positionService;

	@Autowired
	private ModuleServicePlatomix menuService;

	@Autowired
	private User4FlowService userService;

	public static String getToken() {
		String token = ServletUtil.getHeader("Blade-Auth");
		return token;
	}

	public UserInfo get() {
		UserEntity user = null;
		try {
			user = userService.getById(SecureUtil.getUserId());
		} catch (Exception e) {
			log.info("get user is Exception is: " + e.getMessage());
		}

		UserInfo userInfo = new UserInfo();
		userInfo.setDepartmentId(user.getOrganizeId());
		userInfo.setId(user.getId());
		userInfo.setUserId(user.getId());
		userInfo.setIsAdministrator(true);
		userInfo.setOrganizeId(user.getOrganizeId());
		userInfo.setTenantId(user.getTenantId());
		userInfo.setPositionIds(new String[] { user.getPositionId() });
		userInfo.setUserAccount(user.getAccount());
		userInfo.setRoleIds(new String[] { user.getRoleId() });
		userInfo.setUserGender(user.getGender() == 1 ? "男" : "女");
		userInfo.setUserName(user.getRealName());
		userInfo.setUserIcon(user.getHeadIcon());
		userInfo.setTokenTimeout((int) (SecureUtil.getExpire() / 1000));
		return userInfo;
	}

//	public UserInfo get() {
//		BladeUser user = SecureUtil.getUser();
//		UserInfo userInfo = new UserInfo();
//		userInfo.setDepartmentId(user.getDeptId());
//		userInfo.setId(user.getUserId() + "");
//		userInfo.setUserId(user.getUserId() + "");
//		userInfo.setIsAdministrator(true);
//		userInfo.setOrganizeId(user.getDeptId());
//		userInfo.setTenantId(user.getTenantId());
//		userInfo.setPositionIds(new String[] { user.getPostId() });
//		userInfo.setUserAccount(user.getAccount());
//		userInfo.setRoleIds(new String[] { user.getRoleId() });
//		userInfo.setUserGender("男");
//		userInfo.setUserName(user.getUserName());
//		userInfo.setUserIcon("");
//		userInfo.setTokenTimeout((int) (SecureUtil.getExpire() / 1000));
//		return userInfo;
//	}

	public String getUserId() {
		return String.valueOf(SecureUtil.getUserId());
	}

	public String getTenantId() {
		return String.valueOf(SecureUtil.getTenantId());
	}

	private UserCommonInfoVO getUserCommonInfoVO() {
		UserInfo user = get();
		UserCommonInfoVO vo = new UserCommonInfoVO();
		vo.setDepartmentId(user.getDepartmentId());
		vo.setDepartmentName(deptService.getById(user.getDepartmentId()).getFullName());
		vo.setHeadIcon(user.getUserIcon());
		vo.setOrganizeId(user.getOrganizeId());
		vo.setOrganizeName("联通在线");

		List<PositionEntity> posList = positionService.getPositionName(user.getPositionIds());
		if (CollUtil.isNotEmpty(posList)) {
			List<UserPositionVO> plist = new ArrayList<>();
			posList.forEach(pos -> {
				UserPositionVO upv = new UserPositionVO();
				upv.setId(pos.getId());
				upv.setName(pos.getFullName());
				plist.add(upv);
			});
			vo.setPositionIds(plist);
		}
		vo.setUserAccount(user.getUserAccount());
		vo.setUserId(user.getUserId());
		vo.setUserName(user.getUserName());
		return vo;
	}

	/**
	 * 获取用户登陆信息
	 * @return
	 */
	public PcUserVO getCurrentUser() {
		PcUserVO userVO = new PcUserVO();
		userVO.setUserInfo(getUserCommonInfoVO());
		userVO.setMenuList(menuService.getTreeList());
		return userVO;
	}

}
