package com.apobates.forum.attention.core;

import com.apobates.forum.attention.ImageDirectoryEnum;
import com.apobates.forum.attention.ImagePathConvert;
import com.apobates.forum.utils.Commons;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 上传图片的路径句柄 Schema(image://)
 *
 * @author xiaofanku
 * @since 20200516
 */
public class UploadImagePathConvert implements ImagePathConvert {
    //图片的访问域名
    //http://center.test.com
    private final String imageDomain;
    //imagestore
    private final String uploadDirectName;
    private final static String DIRECT = "direct";
    private final static String FILE = "file";
    private final static Logger logger = LoggerFactory.getLogger(UploadImagePathConvert.class);
    
    /**
     * 适用于编码回复内容或编码或解码单张图片.解码回复内容的图片不要调用此方法
     *
     * @param imageDomain 图片存储的域名. 以http开头,例:http://center.test.com
     * @param uploadDirectName 图片保存的目录名称
     */
    public UploadImagePathConvert(String imageDomain, String uploadDirectName) {
        super();
        this.imageDomain = imageDomain;
        this.uploadDirectName = uploadDirectName;
    }
    
    @Override
    public Optional<String> encode(URL imageURL, ImageDirectoryEnum directoryEnum) {
        if(null == imageURL || null == directoryEnum){
            return Optional.empty();
        }
        String lookUploadImgPrefix = imageDomain + "/" + uploadDirectName; //http://center.test.com/imagestore
        String replaceUploadImgPrefix = "image://" + directoryEnum.name().toLowerCase();
        String data = imageURL.toString().replace(lookUploadImgPrefix, replaceUploadImgPrefix).toLowerCase();
        return Optional.of(data);
    }
    
    @Override
    public Optional<URL> decode(String imageEncodeURL, ImageDirectoryEnum directoryEnum, String scale) throws MalformedURLException {
        if(null == imageEncodeURL || null == directoryEnum){
            return Optional.empty();
        }
        String lookUploadImgPrefix = "image://" + directoryEnum.name().toLowerCase();
        if (imageEncodeURL.startsWith(lookUploadImgPrefix)) {
            Map<String, String> pathInfo = getPathInfo(imageEncodeURL, directoryEnum);
            String originalSrc = String.format("%s/thumbs/%s/%s/%s", imageDomain, pathInfo.get(DIRECT), checkImageScale(scale), pathInfo.get(FILE));
            return Optional.ofNullable(new URL(originalSrc));
        }
        return Optional.empty();
    }
    /**
     * 获得目录名和文件名
     *
     * @param encodeImageFilePath
     * @param directoryEnum
     * @return
     */
    public Map<String, String> getPathInfo(String encodeImageFilePath, ImageDirectoryEnum directoryEnum) {
        String lookUploadImgPrefix = "image://" + directoryEnum.name().toLowerCase();
        String[] pathData = encodeImageFilePath.replace(lookUploadImgPrefix, "").split("/");
        //
        Map<String, String> data = new HashMap<>();
        data.put(DIRECT, "/");
        data.put(FILE, "");
        //
        if (pathData.length == 3) {
            data.put(DIRECT, pathData[1]);
            data.put(FILE, pathData[2]);
        }
        if (pathData.length == 2) {
            data.put(FILE, pathData[1]);
        }
        return data;
    }
    public String checkImageScale(String scale) {
        if (null == scale) {
            return "auto";
        }
        if (scale.contains("x")) {
            String[] wh = scale.split("x");
            if (wh.length == 2) {
                return (Commons.isNumeric(wh[0]) && Commons.isNumeric(wh[1])) ? scale : "auto";
            }
            return "auto";
        }
        return "auto";
    }
    /**
     * 图片保存的域名,例:http://pic.test.com
     * @return 
     */
    public String getImageDomain() {
        return imageDomain;
    }
    /**
     * 图片保存的域名,例:upload
     * @return 
     */
    public String getUploadDirectName() {
        return uploadDirectName;
    }
}