package com.bonus.core.autth.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.constant.DelFlagEnum;
import com.bonus.constant.LeConstants;
import com.bonus.core.autth.enums.RoleCodeV2Enum;
import com.bonus.core.autth.po.MgrUserAuthPO;
import com.bonus.core.autth.role.MgrRoleTypeV2Enum;
import com.bonus.core.autth.role.mapper.MgrRoleOrgMapper;
import com.bonus.core.customer.dto.CustOrgTreeDTO;
import com.bonus.core.customer.model.PigxUser;
import com.bonus.core.customer.utils.SecureManager;
import com.bonus.i18n.I18n;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.system.api.model.LoginUser;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MgrAuthApi {

   private MgrRoleOrgMapper mgrRoleOrgMapper;


   public List<CustOrgTreeDTO> getTenantOrg(Integer excludeMiddle, Integer delFlag, boolean filterOtherRole) {
      MgrUserAuthPO mgrUserAuthPO = this.getUserAuthPO();
      return (List)(filterOtherRole && ObjectUtil.notEqual(mgrUserAuthPO.getRoleType(), MgrRoleTypeV2Enum.ROLE_ORG.getKey()) ? CollUtil.newArrayList(new CustOrgTreeDTO[0]) : this.mgrRoleOrgMapper.getTenantOrg(excludeMiddle, mgrUserAuthPO, delFlag));
   }

   public List<CustOrgTreeDTO> getTenantOrg(Integer excludeMiddle, Integer delFlag) {
      return this.getTenantOrg(excludeMiddle, delFlag, false);
   }

   public List<CustOrgTreeDTO> getTenantOrg(Integer excludeMiddle) {
      return this.getTenantOrg(excludeMiddle, DelFlagEnum.DEL_FALSE.key());
   }

   public MgrUserAuthPO getUserAuthPO() {
      MgrUserAuthPO userAuthPO = new MgrUserAuthPO();
      LoginUser user = SecurityUtils.getLoginUser();
      user.setUserid(SecurityUtils.getUserId());
      user.setUsername(SecurityUtils.getUsername());
      String roleCode = (String) SecureManager.getSubjectData().get("roleCode");
      if (ObjectUtil.isEmpty(roleCode)) {
         throw new ServiceException(I18n.getMessage("auth_user_no_role_code", new Object[0]));
      } else {
         Integer roleType;
         if (roleCode.startsWith(RoleCodeV2Enum.ROLE_ADMIN.key())) {
            if (CharSequenceUtil.split(roleCode, "&&").size() > 1) {
               roleType = MgrRoleTypeV2Enum.ROLE_ADMIN.getKey();
            } else {
               roleType = LeConstants.DATA_DEFAULT_INTEGER;
            }
         } else if (roleCode.startsWith(RoleCodeV2Enum.ROLE_ORG.key())) {
            roleType = MgrRoleTypeV2Enum.ROLE_ORG.getKey();
         } else {
            if (!roleCode.startsWith(RoleCodeV2Enum.ROLE_MERCHANT.key())) {
               throw new ServiceException(I18n.getMessage("auth_role_type_error", new Object[0]));
            }

            roleType = MgrRoleTypeV2Enum.ROLE_MERCHANT.getKey();
         }

         userAuthPO.setUserId(user.getUserid());
         userAuthPO.setRoleType(roleType);
         userAuthPO.setRoleCode(roleCode);
         userAuthPO.setUserName(user.getUsername());
         return userAuthPO;
      }
   }
}
