package cn.palmte.work.controller.backend;


import cn.palmte.work.config.Constant;
import cn.palmte.work.model.Admin;
import cn.palmte.work.utils.InterfaceUtil;
import cn.palmte.work.utils.RSAUtils;
import cn.palmte.work.utils.Utils;
import com.alibaba.fastjson.JSON;
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 javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


public class BaseController {
    public static final String KEYWORDS            = "keywords";
    public static final String PAGE_NUMBER         = "pageNumber";
    public static final String PAGE_SIZE           = "pageSize";
    public static final String PAGER               = "pager";
    public static final String DEFAULT_PAGE_NUMBER = "1";
    public static final String DEFAULT_PAGE_SIZE   = "10";

    /**
     * Gets the request.
     *
     * @return the request
     */
    protected HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
    }


    /**
     * Gets the response.
     *
     * @return the response
     */
    protected HttpServletResponse getResponse() {
        return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getResponse();
    }


    /**
     * Gets the session
     *
     * @return the session
     */
    protected HttpSession getSession() {
        return getRequest().getSession();
    }

    protected void downloadHeader(HttpServletResponse response, String fileName) {
        downloadHeader(response, fileName, "application/octet-stream");
    }

    /**
     * 处理下载文件的http header
     *
     * @param response HttpServletResponse
     * @param fileName 文件名
     */
    protected void downloadHeader(HttpServletResponse response, String fileName, String contentType) {
        response.setContentType(contentType);
        response.setCharacterEncoding("UTF-8");
        try {
            response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + java.net.URLEncoder.encode(fileName, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    /**
     * @see BaseController {@link #downloadHeader(HttpServletResponse, String, String)}
     */
    protected void downloadHearder(HttpServletResponse response, String fileName) {
        downloadHeader(response, fileName, "application/octet-stream");
    }


    public ConcurrentHashMap<String, String> getSearchInfo(String keywords, Map<String, Object> model) {
        ConcurrentHashMap<String, String> searchMap = Utils.parseMap(keywords);
        setModers(searchMap, model);
        return searchMap;
    }

    /**
     * 保存搜索条件到model
     */
    public void setModers(ConcurrentHashMap<String, String> searchInfo, Map<String, Object> model) {
        for (Map.Entry<String, String> item : searchInfo.entrySet()) {
            model.put(item.getKey(), item.getValue());
        }
    }


    /**
     * 保存搜索条件到request
     */
    public void setAttrs(Map<String, String> searchInfo) {
        HttpServletRequest request = getRequest();
        for (Map.Entry<String, String> item : searchInfo.entrySet()) {
            request.setAttribute(item.getKey(), item.getValue());
        }
        keepKeywords(searchInfo);
    }

    public Map<String, String> getSearchInfo(String keywords) {

        Map<String, String> searchMap = Utils.parseMap(keywords);
        setAttrs(searchMap);
        return searchMap;
    }

    /**
     * 将搜索条件按“keywords”保存 ，分页才能带上搜索条件
     */
    protected void keepKeywords(Map<String, String> map) {
        HttpServletRequest request = getRequest();
        request.setAttribute(KEYWORDS, JSON.toJSONString(map));
    }

    public void genRSAKeyPair(HttpServletRequest request, Map<String, Object> model) {
        /* 生成KeyPair */
        Map<String, Object> keyMap;
        try {
            keyMap = RSAUtils.genKeyPair();
            String publicKey = RSAUtils.getPublicKey(keyMap);
            String privateKey = RSAUtils.getPrivateKey(keyMap);

            /* 保存私钥到session */
            request.getSession().setAttribute(Constant.PRIVATEKEY, privateKey);

            /* 公钥给前端页面 */
            model.put(Constant.PUBLICKEY, publicKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取当前用户对象
     */
    public Admin getAdmin() {
        return InterfaceUtil.getAdmin();
    }

    /**
     * 获取当前用户对象Id
     */
    public int getAdminId() {
        return InterfaceUtil.getAdmin().getId();
    }


}
