package pwc.taxtech.atms.web.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import pwc.taxtech.atms.dto.AtmsTokenDto;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

@Controller
@RequestMapping("/")
public class IndexController {
    @Value("${api.url}")
    private String apiUrl;

    @Autowired
    JwtUtil jwtUtil;

    @RequestMapping(value = {"/", "/index", "/index.html"}, method = RequestMethod.GET)
    public String login(@CookieValue(value = "AtmsApiToken", required = false) String atmsApiToken,
                        @CookieValue(value = "LtpaToken", required = false) String ltpaToken,
                        HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        if (StringUtils.hasText(atmsApiToken)) {
            return "index";
        } else if (StringUtils.hasText(ltpaToken)) {
            String user = LtpaToken.validate(ltpaToken);
            if (StringUtils.isEmpty(user)) {
                return "redirect:Account/LogOn";
            } else {
                AtmsTokenDto token = new AtmsTokenDto();
                String accessToken = jwtUtil.generateToken(user, user, user);
                token.setAccess_token(accessToken);
                token.setToken_type("bearer");
                token.setExpires_in(86400000L);
                // api_host可以由atms-web端来赋值
                token.setApi_host(apiUrl);
                token.setVat_api_host(apiUrl);
                token.setTp_url(apiUrl);
                token.setVersion("1.0" + ".0.0");
                token.setUser_name(user);
                token.setLocal_name(user);
                token.setNeed_change_password(false);
                token.setIs_external_user(true);
                token.setUser_id(user);
                String cookieString = JSON.toJSONString(token);
                String cookieValue = URLEncoder.encode(cookieString, "UTF-8");
                Cookie cookie = new Cookie("AtmsApiToken", cookieValue);
                response.addCookie(cookie);
                return "redirect:index";
            }

        }
        return "redirect:Account/LogOn";
    }

    @RequestMapping(value = {"/admin", "/admin.html"}, method = RequestMethod.GET)
    public String admin(@CookieValue(value = "AtmsApiToken", required = false) String atmsApiToken) {
        if (StringUtils.hasText(atmsApiToken)) {
            return "admin";
        }
        return "redirect:Account/LogOn";
    }

}