package ru.micord.ervu.security.esia.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import ru.micord.ervu.security.esia.model.OrgInfoModel;
import ru.micord.ervu.security.esia.service.EsiaAuthService;
import ru.micord.ervu.security.esia.service.EsiaDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Eduard Tihomirov
 */
@RestController
public class EsiaController {

  @Autowired
  private EsiaAuthService esiaAuthService;

  @Autowired
  private EsiaDataService esiaDataService;

  @GetMapping(value = "/esia/url")
  public String getEsiaUrl(HttpServletResponse response, HttpServletRequest request) {
    return esiaAuthService.generateAuthCodeUrl(response, request);
  }

  @GetMapping(value = "/esia/auth")
  public void esiaAuth(@RequestParam String code,
      @RequestParam String state,
      HttpServletResponse response, HttpServletRequest request) {
    esiaAuthService.authEsiaTokensByCode(code, state, response, request);
  }

  @PostMapping(value = "/esia/refresh")
  public void refreshToken(HttpServletRequest request, HttpServletResponse response) {
    esiaAuthService.getEsiaTokensByRefreshToken(request, response);
  }

  @GetMapping(value = "/esia/org")
  public OrgInfoModel getOrgInfo(HttpServletRequest request) {
    return esiaDataService.getOrgInfo(request);
  }

  @GetMapping(value = "/esia/userfullname")
  public String getUserFullname(HttpServletRequest request) {
    return esiaDataService.getUserFullname(request);
  }

  @GetMapping(value = "/esia/orgunitname")
  public String getOrgUnitName(HttpServletRequest request) {
    return esiaDataService.getOrgUnitName(request);
  }
}
