package net.ranides.assira.system.impl;

import net.ranides.assira.reflection.IClass;
import net.ranides.assira.system.HostContext;

import java.util.NoSuchElementException;

public class HostCompositeContext implements HostContext {

    private final HostContext[] contexts;

    public HostCompositeContext(HostContext[] contexts) {
        this.contexts = contexts;
    }

    @Override
    public Object getBean(String name) {
        for (HostContext c : contexts) {
            try {
                return c.getBean(name);
            } catch (Exception e) {
                // skip
            }
        }
        throw new NoSuchElementException("No bean with name: " + name);
    }

    @Override
    public <T> T getBean(IClass<T> type) {
        for (HostContext c : contexts) {
            try {
                return c.getBean(type);
            } catch (Exception e) {
                // skip
            }
        }
        throw new NoSuchElementException("No bean with type: " + type);
    }

    @Override
    public <T> T getBean(Class<T> type) {
        for (HostContext c : contexts) {
            try {
                return c.getBean(type);
            } catch (Exception e) {
                // skip
            }
        }
        throw new NoSuchElementException("No bean with type: " + type);
    }

    @Override
    public boolean isPrototype(String name) {
        for (HostContext c : contexts) {
            try {
                return c.isPrototype(name);
            } catch (Exception e) {
                // skip
            }
        }
        throw new NoSuchElementException("No bean with name: " + name);
    }

    @Override
    public boolean isSingleton(String name) {
        for (HostContext c : contexts) {
            try {
                return c.isSingleton(name);
            } catch (Exception e) {
                // skip
            }
        }
        throw new NoSuchElementException("No bean with name: " + name);
    }

    @Override
    public void registerPrototype(String name, IClass<?> type) {
        for (HostContext c : contexts) {
            try {
                c.registerPrototype(name, type);
                break;
            } catch (Exception e) {
                // skip
            }
        }
        throw new UnsupportedOperationException();
    }

    @Override
    public void registerPrototype(String name, Class<?> type) {
        for (HostContext c : contexts) {
            try {
                c.registerPrototype(name, type);
                break;
            } catch (Exception e) {
                // skip
            }
        }
        throw new UnsupportedOperationException();
    }

    @Override
    public void registerSingleton(String name, Object instance) {
        for (HostContext c : contexts) {
            try {
                c.registerSingleton(name, instance);
                break;
            } catch (Exception e) {
                // skip
            }
        }
        throw new UnsupportedOperationException();
    }
}
