/*
 * Copyright 2021 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.tomcat.vestige.launcher;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.jar.Manifest;

import org.apache.tomcat.Jar;

import fr.gaellalire.vestige.spi.resolver.VestigeJar;
import fr.gaellalire.vestige.spi.resolver.VestigeJarEntry;

/**
 * @author Gael Lalire
 */
public class VestigeJarJar implements Jar {

    private VestigeJar vestigeJar;

    private Enumeration<? extends VestigeJarEntry> entries;

    private VestigeJarEntry currentEntry;

    private URL url;
    
    private String root;

    public VestigeJarJar(URL url, VestigeJar vestigeJar, String root) {
        this.url = url;
        this.vestigeJar = vestigeJar;
        this.root = root;
    }

    @Override
    public void reset() throws IOException {
        entries = vestigeJar.getEntries();
        currentEntry = null;
    }

    @Override
    public void nextEntry() {
        if (entries == null) {
            try {
                reset();
            } catch (IOException e) {
                currentEntry = null;
            }
        }
        if (!entries.hasMoreElements()) {
            currentEntry = null;
        } else {
            currentEntry = entries.nextElement();
            if (root.length() != 0) {
                while (currentEntry != null) {
                    String entryName = currentEntry.getName();
                    if (entryName.startsWith(root) && entryName.length() != root.length()) {
                        break;
                    }
                    if (!entries.hasMoreElements()) {
                        currentEntry = null;
                        break;
                    }
                    currentEntry = entries.nextElement();
                }
            }
        }
    }

    @Override
    public String getURL(String entry) {
        return "jar:" + vestigeJar.getCodeBase() + "!/" + root + entry;
    }

    @Override
    public Manifest getManifest() throws IOException {
        if (root.length() != 0) {
            return null;
        }
        return vestigeJar.getManifest();
    }

    @Override
    public long getLastModified(String name) throws IOException {
        VestigeJarEntry vestigeJarEntry = vestigeJar.getEntry(name);
        if (vestigeJarEntry == null) {
            return -1;
        }
        return vestigeJarEntry.getModificationTime();
    }

    @Override
    public URL getJarFileURL() {
        return url;
    }

    @Override
    public InputStream getInputStream(String name) throws IOException {
        VestigeJarEntry vestigeJarEntry = vestigeJar.getEntry(name);
        if (vestigeJarEntry == null) {
            return null;
        }
        return vestigeJarEntry.open();
    }

    @Override
    public String getEntryName() {
        if (currentEntry == null) {
            return null;
        }
        return currentEntry.getName().substring(root.length());
    }

    @Override
    public InputStream getEntryInputStream() throws IOException {
        if (currentEntry == null) {
            return null;
        }
        return currentEntry.open();
    }

    public boolean entryExists(String name) throws IOException {
        return vestigeJar.getEntry(root + name) != null;
    }

    public boolean exists(String name) throws IOException {
        return vestigeJar.getEntry(root + name) != null;
    }

    @Override
    public void close() {
    }


}
