package com.fdkankan.ucenter.common;

import cn.hutool.core.map.TolerantMap;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.util.DateEditor;
import com.fdkankan.common.util.JwtUtil;
import com.fdkankan.ucenter.common.constants.NacosProperty;
import com.fdkankan.ucenter.common.constants.ResultCode;
import com.fdkankan.ucenter.constant.LoginConstant;
import com.fdkankan.ucenter.entity.User;
import com.fdkankan.ucenter.exception.BusinessException;
import com.fdkankan.ucenter.service.IUserService;
import com.fdkankan.ucenter.util.DateUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;

@Slf4j
public class BaseController {

    @Autowired
    protected HttpServletRequest request;

    @Autowired
    protected HttpServletResponse response;

    @Autowired
    IUserService userService;

    @InitBinder
    protected void initBinder(WebDataBinder webDataBinder) {
        webDataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
        webDataBinder.registerCustomEditor(Date.class, new DateEditor(true));
    }

    /**
     * 带参重定向
     *
     * @param path
     * @return
     */
    protected String redirect(String path) {
        return "redirect:" + path;
    }

    /**
     * 不带参重定向
     *
     * @param response
     * @param path
     * @return
     */
    protected String redirect(HttpServletResponse response, String path) {
        try {
            response.sendRedirect(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }



    public static void output(HttpServletResponse resp, File file) {
        OutputStream os = null;
        BufferedInputStream bis = null;
        byte[] buff = new byte[1024];
        try {
            os = resp.getOutputStream();
            bis = new BufferedInputStream(new FileInputStream(file));
            int i = 0;
            while ((i = bis.read(buff)) != -1) {
                os.write(buff, 0, i);
                os.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    protected String getToken(){
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            throw new BusinessException(ResultCode.USER_NOT_LOGIN);
        }
        return token;
    }

    protected String getUserName(){
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            throw new BusinessException(ResultCode.USER_NOT_LOGIN);
        }
        String username = JwtUtil.getUsername(token);
        if(StringUtils.isEmpty(username)){
            throw new BusinessException(ResultCode.USER_NOT_LOGIN);
        }
        return username;
    }
    protected Long getUserId(){
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return null;
        }
        String username = JwtUtil.getUsername(token);
        User user = userService.getByUserName(username);
        if(user == null){
            return null;
        }
        return user.getId();
    }
    protected User getUser(){
        String username = JwtUtil.getUsername(getToken());
        if(org.apache.commons.lang3.StringUtils.isBlank(username)){
            throw new BusinessException(ResultCode.USER_NOT_LOGIN);
        }
        User user = userService.getByUserName(username);
        if(user == null){
            throw new BusinessException(ResultCode.USER_NOT_LOGIN);
        }
        return user;
    }


    protected String getLang(){
        return StringUtils.isEmpty(request.getHeader("lang")) ? "en":request.getHeader("lang");
    }
    protected String getSign(){
        return request.getHeader("sign");
    }
    protected String getAgentKey(){
        return request.getHeader("agent-key");
    }

    protected Integer getTimeZone(){
        Integer minute = 0;
        String timeZone = request.getHeader("timeZone");
        try {
            if(!StringUtils.isEmpty(timeZone)){
                minute = Integer.parseInt(timeZone);
            }
        }catch (Exception e){
            log.error("getClientTime-error:requestHeaderTimeZone:{}",timeZone,e);
        }
        if(!NacosProperty.activeFile.contains("eur")){
            minute += 480;
        }
        return minute;
    }
    protected String getClientTime(){
        return DateUserUtil.AddMinute(new Date(),getTimeZone());

    }

}
