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

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ru.micord.ervu.security.esia.model.PersonDataModel;
import ru.micord.ervu.security.esia.model.PersonModel;
import ru.micord.ervu.security.esia.service.EsiaAuthService;
import ru.micord.ervu.security.esia.service.PersonalDataService;
import org.springframework.beans.factory.annotation.Autowired;
import ru.micord.ervu.security.webbpm.jwt.service.JwtTokenService;

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

  @Autowired
  private EsiaAuthService esiaAuthService;

  @Autowired
  private PersonalDataService personalDataService;

  @Autowired
  private JwtTokenService jwtTokenService;

  @RequestMapping(value = "/esia/url")
  public String getEsiaUrl() {
    return esiaAuthService.generateAuthCodeUrl();
  }

  @GetMapping(value = "/esia/auth", params = "code")
  public ResponseEntity<?> esiaAuth(@RequestParam("code") String code, HttpServletRequest request, HttpServletResponse response) {
    return esiaAuthService.getEsiaTokensByCode(code, request, response);
  }

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

  @RequestMapping(value = "/esia/person")
  public PersonDataModel getPersonModel(HttpServletRequest request) {
    String accessToken = jwtTokenService.getAccessToken(request);
    DateFormat df = new SimpleDateFormat("dd.MM.yyyy");
    PersonModel personModel = personalDataService.getPersonModel(accessToken);
    PersonDataModel personDataModel = new PersonDataModel();
    personDataModel.birthDate = personModel.getBirthDate();
    personDataModel.fullname = personModel.getLastName() + " " + personModel.getFirstName() + " "
                               + personModel.getMiddleName();
    personDataModel.snils = personModel.getSnils();
    personDataModel.seriesAndNumber =
        personModel.getPassportModel().getSeries() + " " + personModel.getPassportModel()
            .getNumber();
    personDataModel.issueDate = personModel.getPassportModel().getIssueDate();
    personDataModel.issuedBy = personModel.getPassportModel().getIssuedBy();
    personDataModel.issueId = personModel.getPassportModel().getIssueId();
    return personDataModel;
  }

  @RequestMapping(value = "/esia/userfullname")
  public String getUserFullname(HttpServletRequest request) {
    String accessToken = jwtTokenService.getAccessToken(request);
    PersonModel personModel = personalDataService.getPersonModel(accessToken);
    return personModel.getLastName() + " " + personModel.getFirstName().charAt(0) + ". " + personModel.getMiddleName().charAt(0) + ".";
  }
}
