package com.inspect.base.core.sftp;

import com.inspect.base.core.utils.StringUtils;
import lombok.Getter;
import lombok.Setter;

import java.io.InputStream;
import java.util.UUID;

public class SftpUploadEntity {
    @Getter
    private String filePath = "";
    @Getter
    private String fileName = UUID.randomUUID().toString();
    @Setter
    @Getter
    private String suffix = ".jpg";
    @Setter
    private String fileFullName;
    @Setter
    @Getter
    private InputStream inputStream;

    public SftpUploadEntity() {
    }

    public SftpUploadEntity(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
        if (filePath.contains(".")) {
            String[] split = filePath.split("\\.");
            this.filePath = split[0];
            this.suffix = "." + split[split.length - 1];
        } else if (!filePath.endsWith("/")) {
            this.filePath = this.filePath + "/";
        }
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
        if (fileName.contains(".")) {
            String[] split = fileName.split("\\.");
            this.fileName = split[0];
            this.suffix = "." + split[split.length - 1];
        }

    }

    public String getFileFullName() {
        return !StringUtils.isEmpty(this.fileFullName) ? this.fileFullName : this.filePath + this.fileName + this.suffix;
    }

    public String toResultPath(String basePath) {
        this.fileFullName = this.getFileFullName();
        return this.fileFullName.startsWith(basePath) ? this.fileFullName.replaceFirst(basePath, "") : this.fileFullName;
    }
}
