package com.icloud.server.web.Jwts;


import com.alibaba.fastjson.JSONObject;

import com.icloud.server.scala.shiro.User;
import com.icloud.server.scala.shiro.UserRepository;
import com.icloud.server.utils.Constants;
import com.icloud.server.web.Jwts.JWTToken;
import com.icloud.server.web.Jwts.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

@Slf4j
public class UserRealm extends AuthorizingRealm {

    @Autowired
    private UserRepository userrepository;

    /**
     * 必须重写此方法，不然会报错
     */
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JWTToken;
    }

    @Override
    @SuppressWarnings("unchecked")
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("————权限认证————");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        return info;
    }

    /**
     * 验证当前登录的Subject
     * LoginController.login()方法中执行Subject.login()时 执行此方法
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
        System.out.println("————身份认证方法————");
        String token = (String) authcToken.getCredentials();
        // 解密获得username，用于和数据库进行对比
        String username = JWTUtil.getUsername(token);
        Long clientId = null;
        String ddd=JWTUtil.getClientId(token);
        if(StringUtils.isNotBlank(JWTUtil.getClientId(token))){
            clientId = Long.valueOf(JWTUtil.getClientId(token));
        }
        JSONObject param = new JSONObject();
        param.put("username",username);
        param.put("clientId",clientId);
        if (clientId==null || username == null || !JWTUtil.verify(token, param.toJSONString())) {
            throw new AuthenticationException("token认证失败！");
        }
        User channelDat = userrepository.findByUsernameAndDfe(username,0);
//        String loginName = (String) authcToken.getPrincipal();
//        ChannelDat channelDat = channelService.getLoginExist(clientId,username);
//        AccountDat accountDat = accountService.getAccountByUserName(username);
        if (channelDat == null) {
            throw new AuthenticationException("该用户不存在！");
        }
        JSONObject accountJson = (JSONObject)JSONObject.toJSON(channelDat);
        SecurityUtils.getSubject().getSession().setAttribute(Constants.SESSION_ACCOUNT_INFO,accountJson);
        return new SimpleAuthenticationInfo(token, token, "MyRealm");
    }
}
