package net.ranides.assira.observable;

import net.ranides.assira.events.EventListener;
import net.ranides.assira.events.EventRouter;

import java.util.*;

public class ObservableList<T> extends AbstractList<T> {

    private final EventRouter router;

    private final List<T> list;

    public ObservableList(EventRouter router, List<T> list) {
        this.router = router;
        this.list = list;
    }

    public EventRouter router() {
        return router;
    }

    public <E extends ListEvent<T>> void addEventListener(Class<E> event, EventListener<? super E> listener) {
        router.addEventListener(event, listener);
    }

    @Override
    public T get(int index) {
        return list.get(index);
    }

    @Override
    public int size() {
        return list.size();
    }

    @Override
    public boolean add(T t) {
        list.add(t);
        router.signalEvent(new ListEvent.Add<>(list, t));
        return true;
    }

    @Override
    public T set(int index, T element) {
        T prev = list.set(index, element);
        router.signalEvent(new ListEvent.Set<>(list, prev, element));
        return prev;
    }

    @Override
    public void add(int index, T element) {
        list.add(index, element);
        router.signalEvent(new ListEvent.Add<>(list, element));
    }

    @Override
    public T remove(int index) {
        T prev = list.remove(index);
        router.signalEvent(new ListEvent.Remove<>(list, prev));
        return prev;
    }

    @Override
    public void clear() {
        list.clear();
        router.signalEvent(new ListEvent.Clear<>(list));
    }

    @Override
    public boolean addAll(int index, Collection<? extends T> c) {
        if(list.addAll(index, c)) {
            router.signalEvent(new ListEvent.AddMany<>(list, c));
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Iterator<T> iterator() {
        return new CIterator(list.listIterator());
    }

    @Override
    public ListIterator<T> listIterator() {
        return new CIterator(list.listIterator());
    }

    @Override
    public ListIterator<T> listIterator(int index) {
        return new CIterator(list.listIterator(index));
    }

    @Override
    public boolean remove(Object o) {
        if (list.remove(o)) {
            router.signalEvent(new ListEvent.Remove<>(list, (T) o));
            return true;
        }
        return false;
    }

    @Override
    public boolean addAll(Collection<? extends T> c) {
        if (list.addAll(c)) {
            router.signalEvent(new ListEvent.AddMany<>(list, c));
            return true;
        }
        return false;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        if (list.removeAll(c)) {
            router.signalEvent(new ListEvent.Clear<>(list));
            return true;
        }
        return false;
    }

    private class CIterator extends ObservableListIterator<T> {
        public CIterator(ListIterator<T> itr) {
            super(itr);
        }

        @Override
        protected void signalSet(T previous, T value) {
            router.signalEvent(new ListEvent.Set<>(list, previous, value));
        }

        @Override
        protected void signalAdd(T value) {
            router.signalEvent(new ListEvent.Add<>(list, value));
        }

        @Override
        protected void signalRemove(T value) {
            router.signalEvent(new ListEvent.Remove<>(list, value));
        }
    }
}
