package com.tiesheng.core.config.web;

import cn.hutool.log.LogFactory;
import com.tiesheng.core.config.web.bean.CurrentWebUser;
import com.tiesheng.core.pojos.dao.CorePlatformUnique;
import com.tiesheng.login.config.token.bean.TokenBean;
import com.tiesheng.util.exception.ApiException;
import com.tiesheng.util.exception.ApiRespEnum;
import com.tiesheng.util.pojos.ApiResp;
import org.springframework.context.annotation.Configuration;

import javax.servlet.http.HttpServletResponse;

/**
 * WEB配置
 *
 * @author hao
 */
@Configuration
public interface TieshengWebConfigurer {


    /**
     * 获取当前用户的姓名
     *
     * @return
     */
    CurrentWebUser getCurrentUserName();


    /**
     * 添加其他异常处理
     *
     * @param e 异常
     */
    default ApiResp<String> addExceptionHandler(Exception e) {
        ApiResp<String> apiResp = ApiResp.respCust(ApiRespEnum.ServerError);
        apiResp.setException(e);
        LogFactory.get().info(apiResp.getException());
        return apiResp;
    }


    /**
     * 添加文件校验
     */
    default void uploadFileCheck(String fileExt) {
    }


    /**
     * 配置登录
     *
     * @return
     */
    default LoginConfigurer loginConfigurer() {
        return new LoginConfigurer() {
            @Override
            public TokenBean doLogin(CorePlatformUnique platformUnique) {
                throw new ApiException("请配置TieshengWebConfigurer->loginConfigurer");
            }

            @Override
            public void redirect(TokenBean bean, String extra, HttpServletResponse response) {
                throw new ApiException("请配置TieshengWebConfigurer->loginConfigurer");
            }
        };
    }


    interface LoginConfigurer {

        /**
         * 登录逻辑
         *
         * @param platformUnique
         * @return
         */
        TokenBean doLogin(CorePlatformUnique platformUnique);


        /**
         * 登录重定向
         *
         * @param bean
         * @param extra
         * @param response
         */
        void redirect(TokenBean bean, String extra, HttpServletResponse response);

    }


}
