package at.irian.ankor.viewmodel;

import at.irian.ankor.ref.Ref;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.*;

public class ViewModelListBase<T> extends ViewModelBase {

    protected List<T> list;

    public ViewModelListBase(Ref viewModelRef, String name, List<T> list) {
        super(viewModelRef.append(name));
        this.list = new ArrayList<T>(list);
    }

    private Ref listRef() {
        return thisRef().append("list");
    }

    private Ref listRef(int i) {
        return thisRef().append(String.format("list[%s]", i));
}

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

    public boolean isEmpty() {
        return list.isEmpty();
    }

    public boolean contains(Object o) {
        return list.contains(o);
    }

    public Iterator<T> iterator() {
        return list.iterator();
    }

    public Object[] toArray() {
        return list.toArray();
    }

    public <T1> T1[] toArray(T1[] a) {
        return list.toArray(a);
    }

    public boolean add(T t) {
        list.add((T) new Object());
        listRef(size() - 1).setValue(t);
        return true;
    }

    public boolean remove(Object o) {
        int index = indexOf(o);
        boolean containsElement = (index >= 0);
        if (containsElement) {
            remove(index);
        }
        return containsElement;
    }

    public boolean containsAll(Collection<?> c) {
        throw new NotImplementedException();
    }

    public boolean addAll(Collection<? extends T> c) {
        throw new NotImplementedException();
    }

    public boolean addAll(int index, Collection<? extends T> c) {
        throw new NotImplementedException();
    }

    public boolean removeAll(Collection<?> c) {
        throw new NotImplementedException();
    }

    public boolean retainAll(Collection<?> c) {
        throw new NotImplementedException();
    }

    public void clear() {
        listRef().setValue(new ArrayList<T>());
    }

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

    public T set(int index, T element) {
        T oldValue = get(index);
        listRef(index).setValue(element);
        return oldValue;
    }

    /**
     * Starting from index, shifts all elements of the list to the left (closer to 0),
     * while informing the client about the changes.
     *
     * The last element of the list will be set to null, while the element at index will be be removed from the list.
     * Note that this will not shift the entire list like a typical shift functions.
     *
     * @param index The index from which to start the shift.
     *              The element on this position will be removed from the collection.
     */
    private void shiftLeft(int index) {
        int i;
        for(i = index; i < size() - 1; i++) {
            listRef(i).setValue(get(i + 1));
        }

        listRef(i).setValue(null);
        list.remove(list.size()-1);
    }

    /**
     * Starting from index + 1, shifts all elements of the list to the right (away from 0),
     * while informing the client about the changes.
     *
     * The element at index and the element at index + 1 will be identical after the shift.
     * Note that this will not shift the entire List like typical shift functions.
     *
     * @param index The index from which to start the shift.
     *              The element on this position and the next will be identical after the shift.
     */
    private void shiftRight(int index) {
        list.add((T) new Object());

        int i;
        for(i = size() - 1; i > index; i--) {
            listRef(i).setValue(this.get(i - 1));
        }
    }


    public void add(int index, T element) {
        shiftRight(index);
        listRef(index).setValue(element);
    }

    public T remove(int index) {
        T toRemove = get(index);
        shiftLeft(index);
        return toRemove;
    }

    public int indexOf(Object o) {
        return list.indexOf(o);
    }

    public int lastIndexOf(Object o) {
        return list.lastIndexOf(o);
    }

    public ListIterator<T> listIterator() {
        return list.listIterator();
    }

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

    public List<T> subList(int fromIndex, int toIndex) {
        throw new NotImplementedException();
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }
}

@@ -460,25 +460,16 @@
 thisRef(
-).append(
 %22list%22);
@@ -520,28 +520,28 @@
      return 
-th
+l
 is
+t
 Ref().append
@@ -544,40 +544,20 @@
 pend
-(String.format(%22list%5B%25s%5D%22, i));%0A
+Idx(i);%0A    
 %7D%0A%0A 
@@ -1032,69 +1032,29 @@
 list
-.add((T) new Object());%0A        listRef(size() - 1).setValue(
+Ref().insert(size(), 
 t);%0A
@@ -1906,32 +1906,115 @@
 t%3CT%3E());%0A    %7D%0A%0A
+    public void reset(List%3C? extends T%3E c) %7B%0A        listRef().setValue(c);%0A    %7D%0A%0A
     public T get
@@ -2222,1410 +2222,48 @@
     
-/**%0A     * Starting from index, shifts all elements of the list to the left (closer to 0),%0A     * while informing the client about the changes.%0A     *%0A     * The last element of the list will be set to null, while the element at index will be be removed from the list.%0A     * Note that this will not shift the entire list like a typical shift functions.%0A     *%0A     * @param index The index from which to start the shift.%0A     *              The element on this position will be removed from the collection.%0A     */%0A    private void shiftLeft(int index) %7B%0A        int i;%0A        for(i = index; i %3C size() - 1; i++) %7B%0A            listRef(i).setValue(get(i + 1));%0A        %7D%0A%0A        listRef(i).setValue(null);%0A        list.remove(list.size()-1);%0A    %7D%0A%0A    /**%0A     * Starting from index + 1, shifts all elements of the list to the right (away from 0),%0A     * while informing the client about the changes.%0A     *%0A     * The element at index and the element at index + 1 will be identical after the shift.%0A     * Note that this will not shift the entire List like typical shift functions.%0A     *%0A     * @param index The index from which to start the shift.%0A     *              The element on this position and the next will be identical after the shift.%0A     */%0A    private void shiftRight(int index) %7B%0A        list.add((T) new Object());%0A%0A        int i;%0A        for(i = size() - 1; i %3E index; i--) %7B%0A    
+public void add(int index, T element) %7B%0A
     
@@ -2278,159 +2278,24 @@
 Ref(
-i
 ).
-setValue(this.get(i - 1));%0A        %7D%0A    %7D%0A%0A%0A    public void add(int index, T element) %7B%0A        shiftRight(index);%0A        listRef(index).setValue(
+insert(index, 
 elem
@@ -2385,17 +2385,24 @@
     
-shiftLeft
+listRef().delete
 (ind

update view model list base to new api