package com.r35157.nenjim.hubd.impl.ref;

import com.r35157.libs.valuetypes.basic.SemanticVersion;
import com.r35157.nenjim.hubd.ctx.Context;

public final class NenjimClassLoader extends ClassLoader {
    // TODO: Check protection - it is only main that can create a 'half' initialized classloader (and set the rest with setters afterwards)
    NenjimClassLoader(Context context) {
        this.context = context;
        //moduleVersion = new SemanticVersion(0, 1, 0);
    }

    @Override
    protected Class<?> findClass(String className) throws ClassNotFoundException {
        System.out.println("NenjimClassLoader asked to load '" + className + "'...");

        // Check if we have any local version preferences configured for the associated Context.
        SemanticVersion version = context.getVersion(className);

        if(version == null) {
            // No local configurations for a specific version has been configured for this package
            // in this Context. Resolution and Journal integration are not implemented yet.
            System.out.println("   Context (" + context.getName()
                    + ") does NOT have a resolved version for '" + className + "'.");
        }
        System.out.println("Searching for class '" + className + "' in local Nenjim class cache...");
        String relativePath = className.replace('.', '/') + ".class";
        int a = 0;
/*
        Path classFile = classCacheRoot.resolve(relativePath);

        if (!Files.exists(classFile)) {
            throw new ClassNotFoundException("Class not found: " + className + " at " + classFile);
        }

        try {
            byte[] classBytes = Files.readAllBytes(classFile);
            return defineClass(className, classBytes, 0, classBytes.length);
        } catch (IOException e) {
            throw new ClassNotFoundException("Failed to load class: " + className, e);
        }
    */
        return null;
    }

    private Context context;
}
