package com.apobates.forum.attention.core;

import com.apobates.forum.attention.ImageDirectoryEnum;
import com.apobates.forum.attention.ImagePathConvert;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.apobates.forum.attention.ImageStoreDomain;
import java.util.Objects;

/**
 * 头像图片的路径句柄
 *
 * Schema(avtar://)
 *
 * @author xiaofanku
 * @since 20200516
 */
public class AvatarImagePathConvert implements ImagePathConvert {
    private final ImageStoreDomain storeDomain;
    //头像存储的目录:avatar
    private final String avatarDirectName;
    private final static Logger logger = LoggerFactory.getLogger(AvatarImagePathConvert.class);
    //avtar://
    
    public AvatarImagePathConvert(ImageStoreDomain storeDomain, String avatarDirectName) {
        super();
        this.storeDomain = storeDomain;
        this.avatarDirectName = avatarDirectName;
    }
    
    @Override
    public Optional<String> encode(URL imageURL, ImageDirectoryEnum directoryEnum) {
        if(null == imageURL || null == directoryEnum){
            return Optional.empty();
        }
        logger.info("[AIP]encode avatar:"+imageURL);
        if (!storeDomain.getDirectoryEnumDomain(directoryEnum).isPresent()) {
            return Optional.empty();
        }
        //http://center.test.com/avatar{/default//03.png}
        String lookUploadImgPrefix = storeDomain.getDirectoryEnumDomain(directoryEnum).get() + "/" + avatarDirectName; //http://center.test.com + / + direct[avatar]
        //avtar://defat{/default/03.png}
        String replaceUploadImgPrefix = "avtar://" + 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();
        }
        logger.info("[AIP]decode avatar:"+imageEncodeURL);
        if (ImageDirectoryEnum.DEFAT == directoryEnum) {
            return Optional.of(new URL(imageEncodeURL.replaceAll("avtar://defat", storeDomain.getDefaultDomain() + "/" + avatarDirectName))); //http://xx.com/avatar
        }
        if (ImageDirectoryEnum.LOCAL == directoryEnum) {
            return Optional.of(new URL(imageEncodeURL.replaceAll("avtar://local", storeDomain.getLocalDomain() + "/" + avatarDirectName)));
        }
        return Optional.empty();
    }
    
    /**
     * 返回默认头像编码中的文件路径 默认头像为系统提供的头像
     *
     * @param encodeAvtarFormatPath 默认头像编码的路径,例:avtar://defat/default/0.png
     * @return 若参数等于: avtar://defat/default/0.png 返回 /default/0.png
     */
    public static String getDefaultAvtarFilePath(String encodeAvtarFormatPath) {
        Objects.requireNonNull(encodeAvtarFormatPath);
        try {
            return encodeAvtarFormatPath.substring(13);
        } catch (StringIndexOutOfBoundsException e) {
            return encodeAvtarFormatPath; //参数 = /empty.png
        }
    }
}