/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */

package net.ranides.assira.collection.maps;

import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import net.ranides.assira.collection.arrays.ArrayUtils;
import net.ranides.assira.collection.arrays.NativeArraySort;
import net.ranides.assira.collection.iterators.RandomAccessIterator;
import net.ranides.assira.generic.CompareUtils;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class ArrayMap<K,V> extends AbstractMap<K, V> {
    
    private final Class<?> type;
    private final K[] keys;
    private final V[] vars;
    
    private final Comparator<? super K> cmp;
    
    public ArrayMap(K[] keys, V[] vars) {
        this(keys, vars, CompareUtils.comparator());
    }

    public ArrayMap(K[] keys, V[] vars, Comparator<? super K> comparator) {
        this.type = keys.getClass().getComponentType();
        this.keys = keys.clone();
        this.vars = vars.clone();
        this.cmp = comparator;
        sort();
    }
    
    private void sort() {
        NativeArraySort.quickSort(0, keys.length, (a,b) -> cmp.compare(keys[a],keys[b]), (a,b) -> {
            swap(keys, a, b);
            swap(vars, a, b);
        });
    }
    
    private static <T> void swap(T[] array, int a, int b) {
        T t = array[a];
        array[a] = array[b];
        array[b] = t;
    }

    @Override
    public Set<Entry<K, V>> entrySet() {
        return new AbstractSet<Entry<K, V>>() {
            @Override
            public Iterator<Entry<K, V>> iterator() {
                return new AIterator(0);
            }

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

        };
    }

    @Override
    public Collection<V> values() {
        return Arrays.asList(vars);
    }

    @Override
    public Set<K> keySet() {
        return new AbstractSet<K>() {
            @Override
            public Iterator<K> iterator() {
                return ArrayUtils.iterator(keys);
            }

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

            @Override
            public boolean contains(Object o) {
                return containsKey(o);
            }
        };
    }

    @Override
    public V get(Object key) {
        if(!type.isInstance(key)) {
            return null;
        }
        @SuppressWarnings("unchecked")
        int i = Arrays.binarySearch(keys, (K)key, cmp);
        return i<0 ? null : vars[i];
    }

    @Override
    @SuppressWarnings("unchecked")
    public boolean containsKey(Object key) {
        if(!type.isInstance(key)) {
            return false;
        }
        @SuppressWarnings("unchecked")
        int i = Arrays.binarySearch(keys, (K)key, cmp);
        return i >= 0;
    }

    @Override
    public boolean containsValue(Object value) {
        if (value==null) {
            for (int i=0; i<vars.length; i++) {
                if (vars[i]==null) { return true; }
            }
        } else {
            for(int i=0; i<vars.length; i++) {
                if( value.equals(vars[i]) ) { return true; }
            }
        }
        return false;
    }
    
    @Override
    public int size() {
        return keys.length;
    }
    
    private class AEntry implements Entry<K,V> {
    
        private final int index;

        public AEntry(int index) {
            this.index = index;
        }

        @Override
        public K getKey() {
            return keys[index];
        }

        @Override
        public V getValue() {
            return vars[index];
        }

        @Override
        public V setValue(V value) {
            V prev = vars[index];
            vars[index] = value;
            return prev;
        }
        
    }
    
    private class AIterator extends RandomAccessIterator<Entry<K,V>> {

        public AIterator(int index) {
            super(index);
        }

        @Override
        protected int size() {
            return keys.length;
        }

        @Override
        protected Entry<K, V> get(int index) {
            return new AEntry(index);
        }

    }

}
