package com.qmth.exam.reserve.controller;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.boot.core.exception.StatusException;
import com.qmth.boot.tools.io.IOUtils;
import com.qmth.exam.reserve.bean.login.LoginUser;
import com.qmth.exam.reserve.enums.Role;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Objects;

public class BaseController {

    protected LoginUser curLoginUser() {
        LoginUser user = (LoginUser) this.curRequest().getAttribute(ApiConstant.ATTRIBUTE_ACCESS_ENTITY);
        if (user == null) {
            throw new StatusException("请先登录！");
        }

        if (Role.STUDENT == user.getRole()) {
            // 限制学生账号
            throw new StatusException("当前用户禁止访问！");
        }
        return user;
    }

    protected LoginUser curLoginStudent() {
        LoginUser user = (LoginUser) this.curRequest().getAttribute(ApiConstant.ATTRIBUTE_ACCESS_ENTITY);
        if (user == null) {
            throw new StatusException("请先登录！");
        }

        if (Role.STUDENT != user.getRole()) {
            // 限制非学生账号
            throw new StatusException("当前用户禁止访问！");
        }
        return user;
    }

    protected HttpServletRequest curRequest() {
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes()))
                .getRequest();
    }

    protected HttpServletResponse curResponse() {
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes()))
                .getResponse();
    }

    protected void exportFile(String fileName, File file) {
        OutputStream out = null;
        InputStream in = null;
        try {
            in = new FileInputStream(file);
            fileName = URLEncoder.encode(fileName, "UTF-8");
            HttpServletResponse response = curResponse();
            response.reset();
            response.setHeader("Content-Disposition", "inline; filename=" + fileName);
            response.addHeader("Content-Length", "" + file.length());
            response.setContentType("application/octet-stream;charset=UTF-8");
            out = new BufferedOutputStream(response.getOutputStream());
            IOUtils.copy(in, out);
            out.flush();
        } catch (IOException e) {
            throw new StatusException(e.getMessage(), e);
        } finally {
            IOUtils.closeQuietly(out);
            IOUtils.closeQuietly(in);
        }
    }

    protected void exportFile(String fileName, InputStream in) {
        OutputStream out = null;
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
            HttpServletResponse response = curResponse();
            response.reset();
            response.setHeader("Content-Disposition", "inline; filename=" + fileName);
            response.setContentType("application/octet-stream;charset=UTF-8");
            out = new BufferedOutputStream(response.getOutputStream());
            IOUtils.copy(in, out);
            out.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            IOUtils.closeQuietly(out);
            IOUtils.closeQuietly(in);
        }
    }

}
