001package armyc2.c5isr.renderer.utilities;
002
003import android.content.Context;
004
005import java.io.BufferedReader;
006import java.io.InputStream;
007import java.io.InputStreamReader;
008import java.util.ArrayList;
009import java.util.HashMap;
010import java.util.List;
011import java.util.Map;
012
013import armyc2.c5isr.renderer.R;
014
015public class SectorModUtils
016{
017    private static SectorModUtils _instance = null;
018    private static Boolean _initCalled = false;
019
020    private String TAG = "SectorModUtils";
021    private List<String> _IDList = new ArrayList<>();
022
023    private static Map<String, String> _sectorMods = new HashMap<>();
024    private static Map<String, ArrayList<String[]>> _sectorModLists = new HashMap<>();
025
026
027    private SectorModUtils() {
028
029    }
030
031    public static synchronized SectorModUtils getInstance() {
032        if (_instance == null) {
033            _instance = new SectorModUtils();
034        }
035        return _instance;
036    }
037
038    public final synchronized void init(Context context)
039    {
040        if (_initCalled == false)
041        {
042            try
043            {
044                loadData(context, SymbolID.Version_2525Dch1);
045                loadData(context, SymbolID.Version_2525Ech1);
046                _initCalled = true;
047
048            } catch (Exception e) {
049                System.out.println(e.getMessage());
050            }
051        }
052    }
053
054    /**
055     *
056     * @param context
057     */
058    private void loadData(Context context, int version)
059    {
060        String[] temp = null;
061        String delimiter = "\t";
062        int ver = 0;
063        int ss = -1;
064        int l = 0;
065        String code = "00";
066        String name = "";
067        String id = null;
068        StringBuilder sb = null;
069        ArrayList<String[]> sectorList = null;
070
071
072        try {
073
074            if(version <= SymbolID.Version_2525Dch1)
075                ver = SymbolID.Version_2525Dch1;
076            else
077                ver = SymbolID.Version_2525Ech1;
078
079            //get sector mods
080            InputStream is;
081            if(ver == SymbolID.Version_2525Dch1)
082                is = context.getResources().openRawResource(R.raw.smd);
083            else
084                is = context.getResources().openRawResource(R.raw.sme);
085
086            if(is != null)
087            {
088                String[] entry = null;
089                BufferedReader br = new BufferedReader(new InputStreamReader(is));
090
091                String line = br.readLine();
092                while (line != null)
093                {
094                    //parse first line
095                    temp = line.split(delimiter);
096                    if(temp.length == 2)
097                    {
098                        if(sectorList != null && sectorList.size() > 0)
099                        {//add completed list to sectorModLists
100                            sb = new StringBuilder();
101                            sb.append(ver).append("-").append(ss).append("-").append(l);
102                            id = sb.toString();
103                            _sectorModLists.put(id,sectorList);
104                        }
105
106                        //get symbol set
107                        ss = Integer.parseInt(temp[0].split(" ")[0]);
108                        //get location; 1=top, 2=bottom
109                        l = Integer.parseInt(temp[1]);
110                        //start new list
111                        sectorList = new ArrayList<>();
112                    }
113                    else if(temp != null && temp.length >= 3)
114                    {
115                        name = temp[0];
116                        code = temp[2];
117                        if(code.length()==1)
118                            code = "0" + code;
119
120                        sb = new StringBuilder();
121                        id = sb.append(ver).append("-").append(ss).append("-").append(l).append("-").append(code).toString();
122                        entry = new String[2];
123                        entry[0] = code;
124                        entry[1] = name;
125                        sectorList.add(entry);
126                        _sectorMods.put(id, name);
127                    }
128                    //read next line for next loop
129                    line = br.readLine();
130                }
131                br.close();
132                is.close();
133            }
134
135        } catch (Exception e) {
136            System.out.println(e.getMessage());
137        }
138    }
139
140    /**
141     *
142     * @param version like SymbolID.Version_2525Dch1 or SymbolID.Version_2525Ech1  Only tracks sector mods for these 2 versions.
143     * @param symbolSet  like SymbolID.SymbolSet_Air; use 0 for Common Modifiers as they are not tied to a symbol set.
144     * @param location 1 for top, 2 for bottom
145     * @return and ArrayList of String[] like ["00","Unspecified"],["01","Attack/Strike"]
146     */
147    public ArrayList<String[]> getSectorModList(int version, int symbolSet, int location)
148    {
149        StringBuilder sb = new StringBuilder();
150        int ver = SymbolID.Version_2525Dch1;
151        if(version >= SymbolID.Version_2525E )
152            ver = SymbolID.Version_2525Ech1;
153
154        int ss = symbolSet;
155        if (ss > 50 && ss < 60)
156            ss = 50;
157
158        sb.append(ver).append("-").append(ss).append("-").append(location);
159        String id = sb.toString();
160        if(_sectorModLists.containsKey(id))
161            return _sectorModLists.get(sb.toString());
162        else
163        {
164            String[] entry = {"00","Unspecified"};
165            ArrayList<String[]> al = new ArrayList<>();
166            al.add(entry);
167            return al;
168        }
169    }
170
171    /**
172     *
173     * @param version like SymbolID.Version_2525Dch1 or SymbolID.Version_2525Ech1  Only tracks sector mods for these 2 versions.
174     * @param symbolSet  like SymbolID.SymbolSet_Air; use 0 for Common Modifiers as they are not tied to a symbol set.
175     * @param location 1 for top, 2 for bottom
176     * @param code like "01" or "100"
177     */
178    public String getName(int version, int symbolSet, int location, String code)
179    {
180        StringBuilder sb = new StringBuilder();
181        int ver = SymbolID.Version_2525Dch1;
182        if(version >= SymbolID.Version_2525E )
183            ver = SymbolID.Version_2525Ech1;
184
185        int ss = symbolSet;
186        if (ss > 50 && ss < 60)
187            ss = 50;
188
189        //verify code is the correct length
190        if(ss > 0 && code.length() != 2)
191        {
192            if(code.length() > 2)
193                code = code.substring(0, 2);
194            else
195            {
196                while(code.length()<2)
197                    code = "0" + code;
198            }
199        }
200        else if(ss == 0 && code.length() != 3)
201        {
202            if (code.length() > 3)
203                code = code.substring(0, 3);
204            else
205            {
206                if (code.startsWith("0"))
207                    code = "1" + code;
208                while (code.length() < 3)
209                    code = "0" + code;
210            }
211        }
212
213        sb.append(ver).append("-").append(ss).append("-").append(location).append("-").append(code);
214        String id = sb.toString();
215
216        if(_sectorMods.containsKey(id))
217            return _sectorMods.get(id);
218        else
219            return "";
220    }
221}
222