package se.bitcraze.crazyflie.lib.toc;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Container for TocElements
 */
public class Toc {

    final Logger mLogger = LoggerFactory.getLogger("Toc");

    private Map<String, Map<String, TocElement>> mTocElementMap = new HashMap<String, Map<String, TocElement>>();

    private int mTocSize = 0;

    public Toc() {
    }

    /**
     * Clear the TOC
     */
    public void clear() {
        this.mTocElementMap.clear();
        this.mTocSize = 0;
    }

    /**
     * Add a new TocElement to the TOC container
     *
     * @param tocElement
     */
    public void addElement(TocElement tocElement) {
        if (tocElement.getGroup().isEmpty()) {
            mLogger.warn("TocElement has no group!");
            return;
        }
        if (!mTocElementMap.containsKey(tocElement.getGroup())) {
            mTocElementMap.put(tocElement.getGroup(), new HashMap<String, TocElement>());
        }
        mTocElementMap.get(tocElement.getGroup()).put(tocElement.getName(), tocElement);
        mTocSize++;
    }

    /**
     * Get a TocElement element identified by complete name from the container.
     *
     * @param completeName
     * @return
     */
    public TocElement getElementByCompleteName(String completeName) {
        return getElementById(getElementId(completeName));
    }

    /**
     * Get the TocElement element id-number of the element with the supplied name.
     *
     * @param completeName
     * @return
     */
    public int getElementId(String completeName) {
        String[] groupNameArray = completeName.split("\\.");
        TocElement TocElement = getElement(groupNameArray[0], groupNameArray[1]);
        if(TocElement != null) {
            return TocElement.getIdent();
        }
        mLogger.warn("Unable to find variable " + completeName);
        return -1;
    }

    /**
     * Get a TocElement element identified by name and group from the container
     *
     * @param group
     * @param name
     * @return
     */
    public TocElement getElement(String group, String name) {
        if (mTocElementMap.get(group) == null) {
            return null;
        }
        return mTocElementMap.get(group).get(name);
    }

    /**
     * Get a TocElement element identified by index number from the container
     *
     * @param ident
     * @return
     */
    public TocElement getElementById(int ident) {
        for(String group : mTocElementMap.keySet()) {
            for (String name : mTocElementMap.get(group).keySet()) {
                if (mTocElementMap.get(group).get(name).getIdent() == ident) {
                    return mTocElementMap.get(group).get(name);
                }
            }
        }
        mLogger.warn("Unable to find TOC element with ID " + ident);
        return null;
    }

    //TODO: generate list not every time
    public List<TocElement> getElements() {
        List<TocElement> tocElementList = new ArrayList<TocElement>();
        for (int i = 0; i < mTocSize; i++) {
            tocElementList.add(getElementById(i));
        }
        return tocElementList;
    }

    public Map<String, Map<String, TocElement>> getTocElementMap() {
        return mTocElementMap;
    }

    public void setTocElementMap(Map<String, Map<String, TocElement>> map) {
        this.mTocElementMap = map;
    }

    public int getTocSize() {
        return mTocSize;
    }
}

@@ -429,39 +429,8 @@
 );%0A%0A
-    private int mTocSize = 0;%0A%0A
     
@@ -555,35 +555,8 @@
 ();%0A
-        this.mTocSize = 0;%0A
     
@@ -1097,28 +1097,8 @@
 t);%0A
-        mTocSize++;%0A
     
@@ -2848,24 +2848,139 @@
 ull;%0A    %7D%0A%0A
+    /**%0A     * Get TocElements as list sorted by ID%0A     *%0A     * @return list of TocElements sorted by ID%0A     */%0A
     //TODO: 
@@ -3008,16 +3008,16 @@
 ry time%0A
-
     publ
@@ -3151,24 +3151,28 @@
  0; i %3C 
-m
+get
 TocSize
+()
 ; i++) %7B
@@ -3539,23 +3539,192 @@
     
-return mTocSize
+int tocSizeCount = 0;%0A        for(String group : mTocElementMap.keySet()) %7B%0A            tocSizeCount += mTocElementMap.get(group).keySet().size();%0A        %7D%0A        return tocSizeCount
 ;%0A  

Fix getTocSize() method