package cn.wise.sc.energy.power.plant.business.security;

import cn.wise.sc.energy.power.plant.business.domain.UserInfo;
import cn.wise.sc.energy.power.plant.common.core.bean.BaseResponse;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.servlet.FilterChain;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.OffsetDateTime;
import java.util.Date;

import static cn.wise.sc.energy.power.plant.business.security.SecurityConstants.AUTHORIZATION_HEADER;
import static cn.wise.sc.energy.power.plant.business.security.SecurityConstants.TOKEN_PREFIX;


public class JWTAuthenticationFilter extends
        UsernamePasswordAuthenticationFilter {

    private final AuthenticationManager authenticationManager;
    private final ObjectMapper objectMapper = new ObjectMapper();

    public JWTAuthenticationFilter(
            final AuthenticationManager authenticationManager) {
        this.authenticationManager = authenticationManager;
    }

    @Override
    public Authentication attemptAuthentication(final HttpServletRequest request,
                                                final HttpServletResponse response) throws AuthenticationException {
        try {

            final LoginRequest loginRequest = this.objectMapper
                    .readValue(request.getInputStream(), LoginRequest.class);
            return this.authenticationManager.authenticate(
                    new UsernamePasswordAuthenticationToken(
                            loginRequest.getUsername(),
                            loginRequest.getPassword()));
        } catch (final IOException e) {
            throw new AuthenticationServiceException(e.getMessage(), e);
        }
    }

    @Override
    protected void successfulAuthentication(final HttpServletRequest request,
                                            final HttpServletResponse response, final FilterChain chain,
                                            final Authentication authResult) {
        final String token = Jwts.builder()
                .setSubject(((UserInfo) authResult.getPrincipal()).getUsername())
                .setIssuedAt(new Date())
                .setExpiration(Date.from(OffsetDateTime.now().plusDays(5).toInstant()))
                .signWith(JWTKeyHolder.KEY, SignatureAlgorithm.HS512)
                .compact();
        response.addHeader(AUTHORIZATION_HEADER, TOKEN_PREFIX + token);
        try {
            String role = ((UserInfo) authResult.getPrincipal()).getRole() + "";
            String rts = JSON.toJSONString(BaseResponse.okData(role));
            response.setHeader("Content-type", "text/html;charset=UTF-8");
            response.setCharacterEncoding("utf-8");
            response.getWriter().write(rts);
        } catch (IOException e) {
            e.printStackTrace();
        }
//        response.addCookie(new Cookie(SecurityConstants.AUTH_COOKIE, token));
    }

    public static void main(String[] args) {
        String t = "asdadasd\tadasdasd";
        System.out.println(t);
    }
}
