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        ArrayList<String[]> sectorList6 = null;
071
072
073        String line = null;
074        try {
075
076            if(version <= SymbolID.Version_2525Dch1)
077                ver = SymbolID.Version_2525Dch1;
078            else
079                ver = SymbolID.Version_2525Ech1;
080
081            //get sector mods
082            InputStream is;
083            if(ver == SymbolID.Version_2525Dch1)
084                is = context.getResources().openRawResource(R.raw.smd);
085            else
086                is = context.getResources().openRawResource(R.raw.sme);
087
088            //get sector mods
089            if(is != null)
090            {
091                String[] entry = null;
092                BufferedReader br = new BufferedReader(new InputStreamReader(is));
093
094                line = br.readLine();
095                while (line != null)
096                {
097                    //parse first line
098                    temp = line.split(delimiter);
099                    if(temp.length == 3)
100                    {
101                        if(sectorList != null && sectorList.size() > 0)
102                        {//add completed list to sectorModLists
103                            String[] vers = temp[2].split(",");
104
105                            for(String v : vers) {
106                                sb = new StringBuilder();
107                                sb.append(v).append("-").append(ss).append("-").append(l);
108                                id = sb.toString();
109
110                                if(Integer.parseInt(v) == SymbolID.Version_2525Ech1 || Integer.parseInt(v) == SymbolID.Version_2525Dch1)
111                                    _sectorModLists.put(id, sectorList);
112                                else
113                                    _sectorModLists.put(id, sectorList6);
114                            }
115                        }
116
117                        //get symbol set
118                        ss = Integer.parseInt(temp[0].split(" ")[0]);
119                        //get location; 1=top, 2=bottom
120                        l = Integer.parseInt(temp[1]);
121                        //start new list
122                        sectorList = new ArrayList<>();
123                        sectorList6 = new ArrayList<>();
124                    }
125                    else if(temp != null && temp.length >= 4)
126                    {
127                        name = temp[0];
128                        code = temp[2];
129                        if(code.length()==1)
130                            code = "0" + code;
131
132                        String[] vers = temp[3].split(",");
133                        for(String v : vers) {
134                            sb = new StringBuilder();
135                            id = sb.append(v).append("-").append(ss).append("-").append(l).append("-").append(code).toString();
136                            entry = new String[2];
137                            entry[0] = code;
138                            entry[1] = name;
139
140                            if(Integer.parseInt(v) == SymbolID.Version_2525Ech1 || Integer.parseInt(v) == SymbolID.Version_2525Dch1)
141                                sectorList.add(entry);
142                            else
143                                sectorList6.add(entry);
144                            _sectorMods.put(id, name);
145                        }
146                    }
147                    //read next line for next loop
148                    line = br.readLine();
149                }
150                br.close();
151                is.close();
152            }
153
154        } catch (Exception e) {
155            System.out.println(e.getMessage());
156        }
157    }
158
159    /**
160     *
161     * @param version like SymbolID.Version_2525Dch1 or SymbolID.Version_2525Ech1  Only tracks sector mods for these 2 versions.
162     * @param symbolSet  like SymbolID.SymbolSet_Air; use 0 for Common Modifiers as they are not tied to a symbol set.
163     * @param location 1 for top, 2 for bottom
164     * @return and ArrayList of String[] like ["00","Unspecified"],["01","Attack/Strike"]
165     */
166    public ArrayList<String[]> getSectorModList(int version, int symbolSet, int location)
167    {
168        StringBuilder sb = new StringBuilder();
169        int ver = version;
170        if(version == SymbolID.Version_2525E)
171            ver = SymbolID.Version_2525Ech1;
172        if(version == SymbolID.Version_APP6Ech1)
173            ver = SymbolID.Version_APP6Ech2;
174
175        int ss = symbolSet;
176        if (ss > 50 && ss < 60)
177            ss = 50;
178
179        sb.append(ver).append("-").append(ss).append("-").append(location);
180        String id = sb.toString();
181        if(_sectorModLists.containsKey(id))
182            return _sectorModLists.get(sb.toString());
183        else
184        {
185            String[] entry = {"00","Unspecified"};
186            ArrayList<String[]> al = new ArrayList<>();
187            al.add(entry);
188            return al;
189        }
190    }
191
192    /**
193     *
194     * @param version like SymbolID.Version_2525Dch1 or SymbolID.Version_2525Ech1  Only tracks sector mods for these 2 versions.
195     * @param symbolSet  like SymbolID.SymbolSet_Air; use 0 for Common Modifiers as they are not tied to a symbol set.
196     * @param location 1 for top, 2 for bottom
197     * @param code like "01" or "100"
198     */
199    public String getName(int version, int symbolSet, int location, String code)
200    {
201        StringBuilder sb = new StringBuilder();
202        int ver = version;
203        if(version == SymbolID.Version_2525E)
204            ver = SymbolID.Version_2525Ech1;
205        if(version == SymbolID.Version_APP6Ech1)
206            ver = SymbolID.Version_APP6Ech2;
207
208        int ss = symbolSet;
209        if (ss > 50 && ss < 60)
210            ss = 50;
211
212        //verify code is the correct length
213        if(ss > 0 && code.length() != 2)
214        {
215            if(code.length() > 2)
216                code = code.substring(0, 2);
217            else
218            {
219                while(code.length()<2)
220                    code = "0" + code;
221            }
222        }
223        else if(ss == 0 && code.length() != 3)
224        {
225            if (code.length() > 3)
226                code = code.substring(0, 3);
227            else
228            {
229                if (code.startsWith("0"))
230                    code = "1" + code;
231                while (code.length() < 3)
232                    code = "0" + code;
233            }
234        }
235
236        sb.append(ver).append("-").append(ss).append("-").append(location).append("-").append(code);
237        String id = sb.toString();
238
239        if(_sectorMods.containsKey(id))
240            return _sectorMods.get(id);
241        else
242            return "";
243    }
244}
245