package ervu.service.fileupload;

import ervu.model.fileupload.EmployeeInfoFileFormType;
import ervu.model.fileupload.EmployeeInfoKafkaMessage;
import ervu.model.fileupload.FileInfo;
import ervu.model.fileupload.FileStatus;
import ervu.model.fileupload.UploadOrgInfo;
import org.springframework.stereotype.Service;
import ru.micord.ervu.journal.SenderInfo;
import ru.micord.ervu.security.esia.model.OrganizationModel;
import ru.micord.ervu.security.esia.model.PersonModel;
import ru.micord.ervu.security.esia.service.UlDataService;

/**
 * @author Alexandr Shalaginov
 */
@Service
public class EmployeeInfoKafkaMessageService {
  private final UlDataService ulDataService;


  public EmployeeInfoKafkaMessageService(UlDataService ulDataService) {
    this.ulDataService = ulDataService;
  }

  public EmployeeInfoKafkaMessage getKafkaMessage(String fileId, String fileUrl, String fileName,
      EmployeeInfoFileFormType formType, String departureDateTime, String accessToken,
      String offset, FileStatus fileStatus, String ervuId, String prnOid,
      PersonModel personModel, long fileSize) {
    return new EmployeeInfoKafkaMessage(
        getOrgInfo(accessToken, ervuId, prnOid, personModel),
        getFileInfo(
            fileId,
            fileUrl,
            fileName,
            formType,
            departureDateTime,
            offset,
            fileStatus,
            fileSize
        )
    );
  }

  private FileInfo getFileInfo(String fileId, String fileUrl, String fileName,
      EmployeeInfoFileFormType formType, String departureDateTime, String offset,
      FileStatus fileStatus, long fileSize) {
    return new FileInfo(
        fileId,
        fileUrl,
        fileName,
        formType.getFilePatternCode(),
        formType.getFilePatternName(),
        String.valueOf(fileSize),
        departureDateTime,
        offset,
        fileStatus
    );
  }

  private UploadOrgInfo getOrgInfo(String accessToken, String ervuId, String prnOid, PersonModel personModel) {
    OrganizationModel organizationModel = ulDataService.getOrganizationModel(accessToken);
    SenderInfo senderInfo = new SenderInfo();
    senderInfo.setFirstName(personModel.getFirstName());
    senderInfo.setLastName(personModel.getLastName());
    senderInfo.setMiddleName(personModel.getMiddleName());
    senderInfo.setPrnOid(prnOid);
    return new UploadOrgInfo(organizationModel.getFullName(), ervuId, senderInfo,
        organizationModel.getOid());
  }
}
