package com.caimei.www.utils;

import org.springframework.util.StringUtils;

/**
 * Description
 *
 * @author : Charles
 * @date : 2020/7/1
 */
public class ImageUtil {

	public static String getImageURL(String dirName, String src, String domain) {
		return getImageURL(dirName, src, 0,domain);
	}

	/***
	 * 获取图片地址
	 *
	 * @param src
	 *            保存在数据库中的图片文件名
	 * @param type
	 *            图片的前缀 (如 type = 200 那么则获取的图片是 200_XXX的图片)
	 * @param dirName
	 *            图片保存的文件夹名 如 (league)
	 * @param domain
	 * 			   加上域名拼成完整路径
	 * @return
	 */
	public static String getImageURL(String dirName, String src, int type, String domain) {


		//正式环境 域名 http --- https处理
		if (domain != null && !"".equals(domain) && domain.startsWith("http:") && domain.toLowerCase().lastIndexOf("config/beta")== -1 && domain.toLowerCase().lastIndexOf("localhost")== -1) {
			domain = domain.replace("http:", "https:");
		}

		//正式环境 图片地址 http --- https处理
		if(src != null && src.startsWith("https:")){
			//非正式环境 使用http
			if (domain !=null && !"".equals(domain) && domain.toLowerCase().lastIndexOf("config/beta")>-1 || domain.toLowerCase().lastIndexOf("localhost")>-1){
				src = src.replace("https:","http:");
			}
			return src;
		}

		//正式环境 图片地址  http --- https处理
		if(src != null && src.startsWith("http:")){
			//非正式环境 使用http
			if (domain !=null && !"".equals(domain) && domain.toLowerCase().lastIndexOf("config/beta")==-1 && domain.toLowerCase().lastIndexOf("localhost")==-1){
				src = src.replace("http:","https:");
			}
			return src;
		}
		type = 0 ;
		dirName = dirName.trim();
		if (dirName == null) {dirName = "";}
		if(src == null || src.equalsIgnoreCase("null")) {src = "";}
		if (src.indexOf(",") > 0) {
			String tmp = src;
			src = tmp.substring(0, tmp.indexOf(","));
		}
		String image = "/public/3.0/img/default/none.jpg";
		if (dirName.equals("user")) {
			image = "/public/3.0/img/default/HeaderImg.png";
		} else if (dirName.equals("club")) {
			image = "/public/3.0/img/default/default_club.jpg";
		} else if (dirName.equals("shopLogo")) {
			image = "/public/3.0/img/default/suppliver.jpg";
		}else if (dirName.equals("caiMeiImage")) {
			image = "/public/3.0/img/default/caiMeiImage.jpg";
		}else {
			image = "/public/3.0/img/default/none.jpg";
		}
		if (src != null && !src.equals("")) {
			if (type != 0 || dirName.equals("product")) {
				src = src.replace("\\", "/");
				String srcActual = src;
				String subDirName = "";
				int index = src.lastIndexOf("/");

				if (index != -1) {
					subDirName = src.substring(0, index + 1);
					srcActual = src.substring(index + 1);
				}
				boolean b = src.startsWith("/uploadFile");
				if(b){
					image = src;
				}else{
					image = "/uploadFile/" + dirName + "/" + subDirName
							+ (type == 0 ? "" : type + "_") + srcActual;
				}
			} else {
				boolean b = src.startsWith("/uploadFile");
				if(b){
					image = src;
				}else{
					image = "/uploadFile/" + dirName + "/" + src;
				}
			}
		}else {
			if(StringUtils.isEmpty(image)) {
				image = "/public/3.0/img/default/none.jpg";
			}
		}
		return domain + image;
	}

	public static String getProductImageURL(String image, int type, String domain) {
		if (image == null) {
			return getImageURL("product", "", type, domain);
		}
		return getImageURL("product", image, type, domain);
	}
}

