package net.northking.cctp.common.s3;

import net.northking.cctp.common.enums.FileBusinessTypeEnum;
import net.northking.cctp.common.util.UUIDUtil;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;

public class S3File implements NKFile
{
  private String id;
  private String bucket;
  private String fileName;
  private String contentType;
  private long length;
  private Date createdTime = new Date();
  private String createdBy;
  private String tenantId;
  private String objId;
  private String businessCode;

  public S3File()
  {
  }

  public S3File(String bucket, String tenantId, MultipartFile multipartFile, String objId, FileBusinessTypeEnum businessCode)
  {
    this.bucket = bucket;
    this.tenantId = tenantId;
    this.contentType = multipartFile.getContentType();
    this.length = multipartFile.getSize();
    this.fileName = multipartFile.getOriginalFilename();
    this.objId = objId;
    this.businessCode = businessCode.getCode();
    assert fileName != null;
    int index = fileName.lastIndexOf(".");
    if (index <= 0)
    {
      // 文件没有后缀名
      this.id = createDatePath() + UUIDUtil.create32UUID();
    } else
    {
      String postfix = fileName.substring(index + 1);
      // 文件ID长度= 32 + 11 + 32 + 后缀名（<10）
      this.id = createDatePath() + UUIDUtil.create32UUID() + "." + postfix;
    }
  }

  public S3File(String bucket, String tenantId, String suffixName)
  {
    this.bucket = bucket;
    this.tenantId = tenantId;
    if (suffixName == null)
    {
      // 文件没有后缀名
      this.id = createDatePath() + UUIDUtil.create32UUID();
    } else
    {
      // 文件ID长度= 32 + 11 + 32 + 后缀名（<10）
      this.id = createDatePath() + UUIDUtil.create32UUID() + "." + suffixName;
    }
  }

  public S3File(String bucket, String tenantId, MultipartFile multipartFile, String uploadId, String chunkIndex)
  {
    this.bucket = bucket;
    this.tenantId = tenantId;
    this.contentType = multipartFile.getContentType();
    this.length = multipartFile.getSize();
    this.fileName = multipartFile.getOriginalFilename();
    this.id = "/temp/" + uploadId + "/" + chunkIndex;
  }

  public S3File(String bucket, String tenantId, File file, String objId, FileBusinessTypeEnum businessCode)
  {
    this.bucket = bucket;
    this.tenantId = tenantId;
    this.length = file.length();
    this.fileName = file.getName();
    this.objId = objId;
    this.businessCode = businessCode.getCode();
    int index = fileName.lastIndexOf(".");
    if (index <= 0)
    {
      // 文件没有后缀名
      this.id = createDatePath() + UUIDUtil.create32UUID();
      this.contentType = ContentType.Application_Octet_Stream.getCode();
    } else
    {
      String postfix = fileName.substring(index + 1);
      // 文件ID长度= 32 + 11 + 32 + 后缀名（<10）
      this.id = createDatePath() + UUIDUtil.create32UUID() + "." + postfix;
      this.contentType = ContentType.findCode(postfix.toLowerCase());
    }
  }

  public String getId()
  {
    return id;
  }

  public void setId(String id)
  {
    this.id = id;
  }

  public String getBucket()
  {
    return bucket;
  }

  public void setBucket(String bucket)
  {
    this.bucket = bucket;
  }

  @Override
  public String getTenantId()
  {
    return tenantId;
  }

  @Override
  public void setTenantId(String tenantId)
  {
    this.tenantId = tenantId;
  }

  public String getFileName()
  {
    return fileName;
  }

  public void setFileName(String fileName)
  {
    this.fileName = fileName;
  }

  public String getContentType()
  {
    return contentType;
  }

  public void setContentType(String contentType)
  {
    this.contentType = contentType;
  }

  public Long getLength()
  {
    return length;
  }

  public void setLength(long length)
  {
    this.length = length;
  }

  public Date getCreatedTime()
  {
    return createdTime;
  }

  public void setCreatedTime(Date createdTime)
  {
    this.createdTime = createdTime;
  }

  public String getCreatedBy()
  {
    return createdBy;
  }

  public void setCreatedBy(String createdBy)
  {
    this.createdBy = createdBy;
  }

  public String toString()
  {
    return "bucket=" + bucket + ", tenantId=" + tenantId + ", ID=" + id + ", fileName=" + fileName + ", ContentType=" + contentType;
  }

  private boolean deleted = false;

  public boolean isDeleted()
  {
    return deleted;
  }

  public void setDeleted(boolean deleted)
  {
    this.deleted = deleted;
  }

  public String getObjId() {
    return objId;
  }

  public void setObjId(String objId) {
    this.objId = objId;
  }

  public String getBusinessCode() {
    return businessCode;
  }

  public void setBusinessCode(String businessCode) {
    this.businessCode = businessCode;
  }
}
