package org.springblade.modules.resource.template;

import cn.hutool.core.util.IdUtil;
import com.obs.services.ObsClient;
import com.obs.services.model.PutObjectResult;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.oss.OssTemplate;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.oss.model.OssFile;
import org.springblade.core.oss.props.OssProperties;
import org.springblade.core.oss.rule.OssRule;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author: lianghanqiang
 * @description:
 * @since: 7/27/21 -- 3:06 PM
 */
@Data
@AllArgsConstructor
@Slf4j
public class ObsTemplate implements OssTemplate {

	private final ObsClient obsClient;
	private final OssProperties ossProperties;
	private final OssRule ossRule;


	@Override
	public void makeBucket(String bucketName) {

	}

	@Override
	public void removeBucket(String bucketName) {

	}

	@Override
	public boolean bucketExists(String bucketName) {
		return false;
	}

	@Override
	public void copyFile(String bucketName, String fileName, String destBucketName) {

	}

	@Override
	public void copyFile(String bucketName, String fileName, String destBucketName, String destFileName) {

	}

	@Override
	public OssFile statFile(String fileName) {
		return null;
	}

	@Override
	public OssFile statFile(String bucketName, String fileName) {
		return null;
	}

	@Override
	public String filePath(String fileName) {
		return null;
	}

	@Override
	public String filePath(String bucketName, String fileName) {
		return null;
	}

	@Override
	public String fileLink(String fileName) {
		return null;
	}

	@Override
	public String fileLink(String bucketName, String fileName) {
		return null;
	}

	@Override
	public BladeFile putFile(MultipartFile file) {
		String fileName  = file.getOriginalFilename();
		return this.putFile(fileName,file);
	}

	@Override
	public BladeFile putFile(String fileName, MultipartFile file) {
		InputStream inputStream = null;

		try {
			inputStream = file.getInputStream();
		} catch (IOException e) {
			log.error("文件上传失败！ 原因："+e.getMessage());
		}

		return this.putFile(fileName,inputStream);
	}

	@Override
	public BladeFile putFile(String bucketName, String fileName, MultipartFile file) {
		InputStream inputStream = null;

		try {
			inputStream = file.getInputStream();
		} catch (IOException e) {
			log.error("文件上传失败！ 原因："+e.getMessage());
		}
		return this.putFile(bucketName,fileName,inputStream);
	}

	@Override
	public BladeFile putFile(String fileName, InputStream stream) {
		return this.putFile(ossProperties.getBucketName(),fileName,stream);
	}

	@Override
	public BladeFile putFile(String bucketName, String fileName, InputStream stream) {
		// 文件名转换
		String fullName = generateName(fileName);
		//上传文件
		PutObjectResult putObjectResult = obsClient.putObject(bucketName,fullName, stream);

		Assert.isTrue(putObjectResult.getStatusCode()==200,"obs 上传文件失败！文件名："+fileName);

		//构造返回文件实体
		BladeFile bladeFile = new BladeFile();
		bladeFile.setDomain(ossProperties.getEndpoint());
		bladeFile.setLink(putObjectResult.getObjectUrl());
		bladeFile.setName(fullName);
		bladeFile.setOriginalName(fileName);
		return bladeFile;
	}

	@Override
	public void removeFile(String fileName) {

	}

	@Override
	public void removeFile(String bucketName, String fileName) {

	}

	@Override
	public void removeFiles(List<String> fileNames) {

	}

	@Override
	public void removeFiles(String bucketName, List<String> fileNames) {

	}

	private String generateName(String fileName){
		String prefix = IdUtil.simpleUUID();
		return prefix+"-"+fileName;
	}
}
