package com.fine.theme.icon;

import com.fr.general.IOUtils;
import com.fr.io.utils.ResourceIOUtils;
import com.fr.third.errorprone.annotations.Immutable;
import org.jetbrains.annotations.NotNull;

import java.io.InputStream;
import java.util.StringJoiner;

/**
 * url图标资源
 *
 * @author vito
 * @since 11.0
 * Created on 2023/11/15
 */
@Immutable
public class UrlIconResource implements IconResource {

    private final String path;

    public UrlIconResource(String path) {
        this.path = path;
    }

    public String getPath() {
        return path;
    }

    @Override
    @NotNull
    public InputStream getInputStream() {
        InputStream inputStream = getInputStream(path);
        if (inputStream == null) {
            throw new IconException("Icon load failed: " + path);
        }
        return inputStream;
    }


    private InputStream getInputStream(String path) {
        InputStream inputStream = IOUtils.getInputStream(path);
        return inputStream != null ? inputStream : ResourceIOUtils.read(path);
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", UrlIconResource.class.getSimpleName() + "[", "]")
                .add("path='" + path + "'")
                .toString();
    }
}