package cn.pluss.platform.controller;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.util.Base64Util;
import cn.pluss.platform.util.FileUtil;
import cn.pluss.platform.util.ParametersUtil;
import cn.pluss.platform.util.StringUtil;
import cn.pluss.platform.vo.Images;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Properties;

@RestController
@RequiredArgsConstructor
@RequestMapping("/upLoadImage")
@Transactional
@Api(value = "UpLoadImage控制类", tags = { "UpLoadImage接口" })
public class UploadImageController {

	@PostMapping
	@ApiOperation(tags = {"页面-设置"}, value = "图片上传, 只用于乐刷进件", notes = "图片的base64字符串", httpMethod = "POST")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "Image", value = "上传的图片实例", paramType = "body", dataType = "Image", required = true) })
	public Result<Images> add(@RequestBody Images image) {
		Integer size = FileUtil.imageSize(image.getBase64());
		boolean bytesToKB =FileUtil.bytesToKB(size, 1);
		if (bytesToKB) {
			return ResultGenerator.genFailResult("请上传小于1M的图片（可以添加图片的时候点击编辑，裁剪多余的部分就可以小于1M）");
		}
		String visit=ParametersUtil.upload_visit_path;
		String fileName = null;
		String name = null;
		String picUrl = "";
		try {
			String path=ParametersUtil.upload_save_path;
			Properties prop = System.getProperties();
			String os = prop.getProperty("os.name");
			if (os.startsWith("win") || os.startsWith("Win")) {
				// windows目录
				path = "D:\\xm\\media\\java\\Media\\resources\\manage\\upload";
				visit = "http://127.0.0.1/resources/manage/upload";
			}
			name = StringUtil.getBillno();
			fileName = path + "/" + name + ".jpg";
			Base64Util.decoderBase64File(image.getBase64(), fileName);
			picUrl = visit + "/" + name + ".jpg";
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult(e.getMessage());
		}
		image = new Images();
		image.setVisitUrl(picUrl);

		return ResultGenerator.genSuccessResult(image);
	}

	@PostMapping("/other")
	@ApiOperation(tags = {"页面-设置"}, value = "图片上传, 意见反馈等", notes = "图片的base64字符串", httpMethod = "POST")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "Image", value = "上传的图片实例", paramType = "body", dataType = "Image", required = true) })
	public Result<Images> addOther(@RequestBody Images image) {
		Integer size = FileUtil.imageSize(image.getBase64());
		boolean bytesToKB =FileUtil.bytesToKB(size, 5);
		if (bytesToKB) {
			return ResultGenerator.genFailResult("请上传小于5M的图片（可以添加图片的时候点击编辑，裁剪多余的部分就可以小于1M）");
		}
		String visit=ParametersUtil.upload_visit_path;
		String fileName = null;
		String name = null;
		String picUrl = "";
		try {
			String path=ParametersUtil.upload_save_path;
			Properties prop = System.getProperties();
			String os = prop.getProperty("os.name");
			if (os.startsWith("win") || os.startsWith("Win")) {
				// windows目录
				path = "D:\\xm\\media\\java\\Media\\resources\\manage\\upload";
				visit = "http://127.0.0.1/resources/manage/upload";
			}
			name = StringUtil.getBillno();
			fileName = path + "/" + name + ".jpg";
			Base64Util.decoderBase64File(image.getBase64(), fileName);
			picUrl = visit + "/" + name + ".jpg";
		} catch (Exception e) {
			e.printStackTrace();
			return ResultGenerator.genFailResult(e.getMessage());
		}
		image = new Images();
		image.setVisitUrl(picUrl);
		return ResultGenerator.genSuccessResult(image);
	}
}
