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

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ru.micord.ervu.security.esia.model.Addresses;
import ru.micord.ervu.security.esia.model.Contacts;
import ru.micord.ervu.security.esia.model.EmployeeModel;
import ru.micord.ervu.security.esia.model.EsiaAccessToken;
import ru.micord.ervu.security.esia.model.OrgInfoModel;
import ru.micord.ervu.security.esia.model.OrganizationModel;
import ru.micord.ervu.security.esia.model.PersonModel;
import ru.micord.ervu.security.webbpm.jwt.service.JwtTokenService;

/**
 * @author Eduard Tihomirov
 */
@Service
public class EsiaDataService {

  @Autowired
  private UlDataService ulDataService;

  @Autowired
  private JwtTokenService jwtTokenService;

  public OrgInfoModel getOrgInfo(HttpServletRequest request) {
    String accessToken = jwtTokenService.getAccessToken(request);
    if (accessToken == null) {
      return null;
    }
    OrganizationModel organizationModel = ulDataService.getOrganizationModel(accessToken);
    EmployeeModel chiefEmployeeModel = ulDataService.getChiefEmployeeModel(accessToken);
    EmployeeModel employeeModel = ulDataService.getEmployeeModel(accessToken);
    OrgInfoModel orgInfoModel = new OrgInfoModel();

    orgInfoModel.empFullname =
        employeeModel.getPerson().getLastName() + " " + employeeModel.getPerson().getFirstName()
        + " " + employeeModel.getPerson().getMiddleName();
    orgInfoModel.empPosition = employeeModel.getPosition();
    orgInfoModel.fullName = organizationModel.getFullName();
    orgInfoModel.shortName = organizationModel.getShortName();
    Addresses addresses = organizationModel.getAddresses();
    if (addresses != null) {
      Arrays.stream(addresses.getElements()).forEach(addressModel -> {
        if (addressModel.getType().equals("OLG")) {
          orgInfoModel.olgAddress = addressModel.getAddressStr();
        }
        else if (addressModel.getType().equals("OPS")) {
          orgInfoModel.opsAddress = addressModel.getAddressStr();
        }
      } );
    }
    if (chiefEmployeeModel != null) {
      orgInfoModel.chiefFullname =
          chiefEmployeeModel.getPerson().getLastName() + " " + chiefEmployeeModel.getPerson()
              .getFirstName() + " " + chiefEmployeeModel.getPerson().getMiddleName();
      orgInfoModel.chiefPosition = chiefEmployeeModel.getPosition();
    }
    orgInfoModel.ogrn = organizationModel.getOgrn();
    orgInfoModel.kpp = organizationModel.getKpp();
    orgInfoModel.inn = organizationModel.getInn();
    Contacts contacts = organizationModel.getContacts();
    if (contacts != null) {
      Arrays.stream(contacts.getElements()).forEach(contactModel -> {
        if (contactModel.getType().equals("OPH") && contactModel.getVrfStu().equals("VERIFIED")) {
          orgInfoModel.mobile = contactModel.getValue();
        }
        else if (contactModel.getType().equals("OEM") && contactModel.getVrfStu().equals("VERIFIED")) {
          orgInfoModel.email = contactModel.getValue();
        }
      } );
    }
    orgInfoModel.userRoles = ulDataService.getAllUserRoles(accessToken);
    return orgInfoModel;
  }

  public String getOrgUnitName(HttpServletRequest request) {
    String accessToken = jwtTokenService.getAccessToken(request);
    if (accessToken == null) {
      return null;
    }
    OrganizationModel organizationModel = ulDataService.getOrganizationModel(accessToken);
    return organizationModel.getFullName();
  }

  public String getUserFullname(HttpServletRequest request) {
    String accessToken = jwtTokenService.getAccessToken(request);
    if (accessToken == null) {
      return null;
    }
    EsiaAccessToken esiaAccessToken = ulDataService.readToken(accessToken);
    PersonModel personModel = ulDataService.getPersonData(esiaAccessToken.getSbjId(), accessToken);
    return personModel.getLastName() + " " + personModel.getFirstName().charAt(0) + ". " + personModel.getMiddleName().charAt(0) + ".";
  }
}
