package com.lightinit.hsdataportal.handler;
import com.lightinit.hsdataportal.common.ConfigUtils;
import com.lightinit.hsdataportal.common.SysContants;
import com.lightinit.hsdataportal.common.WebUtilsPro;
import com.lightinit.hsdataportal.dictionary.DicPlatformType;
import com.lightinit.hsdataportal.dictionary.DicShiroTokenUserType;
import com.lightinit.hsdataportal.entity.Admin;
import com.lightinit.hsdataportal.model.ResultState;
import com.lightinit.hsdataportal.model.ResultStateCode;
import com.lightinit.hsdataportal.service.IAdminService;
import com.lightinit.hsdataportal.service.IRoleService;
import com.lightinit.hsdataportal.service.IShiroPermissionsService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Mr.Yao on 2016/1/25.
 */
public class AuthInterceptor extends HandlerInterceptorAdapter {

    private static final Logger logger = LoggerFactory.getLogger(AuthInterceptor.class);

    @Autowired
    private IRoleService roleService;
    @Autowired
    private IAdminService adminService;
    @Autowired
    private IShiroPermissionsService shiroPermissionsService;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String relativeUrl = request.getRequestURI();
        String userType = ConfigUtils.getConfiguration(SysContants.PropertyFilePath.CONFIG).getString(SysContants.PropertyKey.USER_TYPE);
        if (StringUtils.isEmpty(userType)){
            writeNoPermissionReponse(request, response);
            return false;
        }
        if (userType.equals(DicShiroTokenUserType.ADMIN) && relativeUrl.contains("/open/")){
            return false;
        }else if (userType.equals(DicShiroTokenUserType.USER) && relativeUrl.contains("/share/")){
            return false;
        }
        return true;

        /*Subject subject = SecurityUtils.getSubject();
        boolean isPermitted = subject.isPermitted(relativeUrl.contains("/open/") ? DicPlatformType.OPEN : DicPlatformType.SHARE);
        if (!isPermitted){
            writeNoPermissionReponse(request, response);
        }
        return isPermitted;*/
    }

    private void writeNoPermissionReponse(HttpServletRequest request, HttpServletResponse response) throws Exception {
        logger.debug("无权访问");
        if (WebUtilsPro.isAjaxRequest(request)){
            ResultState resultState = new ResultState();
            resultState.setStateCode(ResultStateCode.NO_PERMISSION);
            resultState.setMsg("无权访问");
            WebUtilsPro.writeJson(response, resultState);
        }else {
            response.sendRedirect("/403.html");
        }
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        /*if (null == modelAndView)
            return;
        Subject user = SecurityUtils.getSubject();
        if (null == user)
            return;
        Object objectUserName = user.getPrincipal();
        String UserShowName="游客";
        if (null != objectUserName) {
            String currentUsername = (String) user.getPrincipal();
            if(StringUtils.isEmpty(currentUsername)){
                return;
            }else {
                Admin mUser = adminService.SelectOne(currentUsername);
                if(mUser!=null){
                    UserShowName=mUser.getShowname();
                }
            }
        }
        modelAndView.addObject("showname", UserShowName);*/
    }
}
