001/*
002 * To change this template, choose Tools | Templates and open the template in the editor.
003 */
004
005package armyc2.c2sd.renderer.utilities;
006
007import armyc2.c2sd.renderer.R;
008
009import android.content.Context;
010import android.util.Log;
011
012import java.io.BufferedInputStream;
013import java.io.DataInputStream;
014import java.io.IOException;
015import java.util.ArrayList;
016import java.util.HashMap;
017import java.util.Map;
018
019/**
020 *
021 * @author michael.spinelli
022 */
023public class UnitDefTable
024{
025
026    private static Boolean _initCalled = false;
027    private static UnitDefTable _instance = null;
028    // private static SymbolTableThingy
029    private static Map<String, UnitDef> _UnitDefinitionsB = null;
030    private static ArrayList<UnitDef> _UnitDefDupsB = null;
031
032    private static Map<String, UnitDef> _UnitDefinitionsC = null;
033    private static ArrayList<UnitDef> _UnitDefDupsC = null;
034
035    private String TAG = "UnitDefTable";
036
037    private UnitDefTable()
038    {
039
040    }
041
042    public static synchronized UnitDefTable getInstance()
043    {
044        if (_instance == null)
045            _instance = new UnitDefTable();
046
047        return _instance;
048    }
049
050    /*
051     * public String[] searchByHierarchy(String hierarchy) { for(UnitDef foo : _UnitDefinitions.values() ) {
052     * if(foo.getHierarchy().equalsIgnoreCase(hierarchy)) { return } } }
053     */
054
055    public synchronized void init(Context context)
056    {
057        if (_initCalled == false) {
058
059            _UnitDefinitionsB = new HashMap<>();
060            _UnitDefDupsB = new ArrayList<>();
061
062            _UnitDefinitionsC = new HashMap<>();
063            _UnitDefDupsC = new ArrayList<>();
064
065            try {
066                DataInputStream dis = new DataInputStream(new BufferedInputStream(context.getResources().openRawResource(R.raw.unitconstants)));
067                readBinary(dis);
068                dis.close();
069            } catch (IOException e) {
070                Log.e("UnitDefTable", "Could not load", e);
071            }
072
073            _initCalled = true;
074        }
075    }
076
077    /**
078     * @name getSymbolDef
079     *
080     * @desc Returns a SymbolDef from the SymbolDefTable that matches the passed in Symbol Id
081     *
082     * @param basicSymbolID - IN - A 15 character MilStd code
083     * @return SymbolDef whose Symbol Id matches what is passed in
084     */
085    public UnitDef getUnitDef(String basicSymbolID, int symStd)
086    {
087        UnitDef returnVal = null;
088        try {
089            if (symStd == RendererSettings.Symbology_2525B) {
090                returnVal = _UnitDefinitionsB.get(basicSymbolID);
091            } else if (symStd == RendererSettings.Symbology_2525C) {
092                returnVal = _UnitDefinitionsC.get(basicSymbolID);
093            }
094        } catch (Exception exc) {
095            Log.e("UnitDefTable", exc.getMessage(), exc);
096        } catch (Throwable thrown) {
097            Log.wtf("UnitDefTable", thrown.getMessage(), thrown);
098        }
099        return returnVal;
100    }
101
102    /**
103     *
104     * @return
105     */
106    public Map<String, UnitDef> getAllUnitDefs(int symStd)
107    {
108        if (symStd == RendererSettings.Symbology_2525B)
109            return _UnitDefinitionsB;
110        else
111            return _UnitDefinitionsC;
112    }
113
114    /**
115     * SymbolIDs are no longer unique thanks to 2525C and some EMS symbols.
116     * Here are the EMS symbols that reused symbol IDs.
117     * Like how EMS.INCDNT.CVDIS.DISPOP uses the same symbol code as STBOPS.ITM.RFG (O*I*R-----*****)
118     * @param symStd
119     * @return
120     */
121    public ArrayList<UnitDef> getUnitDefDups(int symStd)
122    {
123        if (symStd == RendererSettings.Symbology_2525B)
124            return _UnitDefDupsB;
125        else
126            return _UnitDefDupsC;
127    }
128
129    /**
130     * 
131     * @param basicSymbolID
132     * @return
133     */
134    public Boolean hasUnitDef(String basicSymbolID, int symStd)
135    {
136        if (basicSymbolID != null && basicSymbolID.length() == 15) {
137            if (symStd == RendererSettings.Symbology_2525B)
138                return _UnitDefinitionsB.containsKey(basicSymbolID);
139            else if (symStd == RendererSettings.Symbology_2525C)
140                return _UnitDefinitionsC.containsKey(basicSymbolID);
141            else
142                return false;
143        } else
144            return false;
145    }
146
147    private void readBinary(DataInputStream dis) throws IOException
148    {
149        int count = dis.readInt();
150        for (int i = 0; i < count; i++) {
151            UnitDef def = UnitDef.readBinary(dis);
152            _UnitDefinitionsB.put(def.getBasicSymbolId(), def);
153        }
154
155        count = dis.readInt();
156        for (int i = 0; i < count; i++) {
157            UnitDef def = UnitDef.readBinary(dis);
158            _UnitDefDupsB.add(def);
159        }
160
161        count = dis.readInt();
162        for (int i = 0; i < count; i++) {
163            UnitDef def = UnitDef.readBinary(dis);
164            _UnitDefinitionsC.put(def.getBasicSymbolId(), def);
165        }
166
167        count = dis.readInt();
168        for (int i = 0; i < count; i++) {
169            UnitDef def = UnitDef.readBinary(dis);
170            _UnitDefDupsC.add(def);
171        }
172    }
173}