content: /*
   This file is part of the OdinMS Maple Story Server
   Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
   Matthias Butz <matze@odinms.de>
   Jan Christian Meyer <vimes@odinms.de>
  
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as
   published by the Free Software Foundation version 3 as published by
   the Free Software Foundation. You may not use, modify or distribute
   this program under any other version of the GNU Affero General Public
   License.
  
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Affero General Public License for more details.
  
   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
   */
  package mapleskillmakerfetcher;
  
  import java.io.File;
  import java.util.HashMap;
  import java.util.LinkedHashMap;
  import java.util.Map;
  
  import provider.MapleData;
  import provider.MapleDataDirectoryEntry;
  import provider.MapleDataFileEntry;
  import provider.MapleDataProvider;
  import provider.MapleDataProviderFactory;
  import provider.MapleDataTool;
  /**
   *
   * @author Ronan
   *
   */
  public class MapleItemInformationProvider {
      private final static String wzPath = "../../wz";
      
      private static MapleItemInformationProvider instance = null;
      protected MapleDataProvider itemData;
      protected MapleDataProvider equipData;
      protected MapleDataProvider stringData;
      protected MapleData eqpStringData;
      
      protected Map<Integer, Map<String, Integer>> equipStatsCache = new HashMap<>();
      protected Map<Integer, String> nameCache = new HashMap<>();
      
      private MapleItemInformationProvider() {
          itemData = MapleDataProviderFactory.getDataProvider(new File(wzPath + "/Item.wz"));
          equipData = MapleDataProviderFactory.getDataProvider(new File(wzPath + "/Character.wz"));
          stringData = MapleDataProviderFactory.getDataProvider(new File(wzPath + "/String.wz"));
          eqpStringData = stringData.getData("Eqp.img");
      }
  
      public static MapleItemInformationProvider getInstance() {
          if (instance == null) {
              instance = new MapleItemInformationProvider();
          }
          return instance;
      }
  
      private MapleData getItemData(int itemId) {
          MapleData ret = null;
          String idStr = "0" + String.valueOf(itemId);
          MapleDataDirectoryEntry root = itemData.getRoot();
          for (MapleDataDirectoryEntry topDir : root.getSubdirectories()) {
              for (MapleDataFileEntry iFile : topDir.getFiles()) {
                  if (iFile.getName().equals(idStr.substring(0, 4) + ".img")) {
                      ret = itemData.getData(topDir.getName() + "/" + iFile.getName());
                      if (ret == null) {
                          return null;
                      }
                      ret = ret.getChildByPath(idStr);
                      return ret;
                  } else if (iFile.getName().equals(idStr.substring(1) + ".img")) {
                      return itemData.getData(topDir.getName() + "/" + iFile.getName());
                  }
              }
          }
          root = equipData.getRoot();
          for (MapleDataDirectoryEntry topDir : root.getSubdirectories()) {
              for (MapleDataFileEntry iFile : topDir.getFiles()) {
                  if (iFile.getName().equals(idStr + ".img")) {
                      return equipData.getData(topDir.getName() + "/" + iFile.getName());
                  }
              }
          }
          return ret;
      }
      
      public Map<String, Integer> getEquipStats(int itemId) {
          if (equipStatsCache.containsKey(itemId)) {
              return equipStatsCache.get(itemId);
          }
          Map<String, Integer> ret = new LinkedHashMap<>();
          MapleData item = getItemData(itemId);
          if (item == null) {
              return null;
          }
          MapleData info = item.getChildByPath("info");
          if (info == null) {
              return null;
          }
          for (MapleData data : info.getChildren()) {
              if (data.getName().startsWith("inc")) {
                  ret.put(data.getName().substring(3), MapleDataTool.getIntConvert(data));
              }
              /*else if (data.getName().startsWith("req"))
               ret.put(data.getName(), MapleDataTool.getInt(data.getName(), info, 0));*/
          }
          ret.put("reqJob", MapleDataTool.getInt("reqJob", info, 0));
          ret.put("reqLevel", MapleDataTool.getInt("reqLevel", info, 0));
          ret.put("reqDEX", MapleDataTool.getInt("reqDEX", info, 0));
          ret.put("reqSTR", MapleDataTool.getInt("reqSTR", info, 0));
          ret.put("reqINT", MapleDataTool.getInt("reqINT", info, 0));
          ret.put("reqLUK", MapleDataTool.getInt("reqLUK", info, 0));
          ret.put("reqPOP", MapleDataTool.getInt("reqPOP", info, 0));
          ret.put("cash", MapleDataTool.getInt("cash", info, 0));
          ret.put("tuc", MapleDataTool.getInt("tuc", info, 0));
          ret.put("cursed", MapleDataTool.getInt("cursed", info, 0));
          ret.put("success", MapleDataTool.getInt("success", info, 0));
          ret.put("fs", MapleDataTool.getInt("fs", info, 0));
          equipStatsCache.put(itemId, ret);
          return ret;
      }
  
      private MapleData getStringData(int itemId) {
          String cat = "null";
          MapleData theData;
          if ((itemId >= 1010000 && itemId < 1040000) || (itemId >= 1122000 && itemId < 1123000) || (itemId >= 1132000 && itemId < 1133000) || (itemId >= 1142000 && itemId < 1143000)) {
              theData = eqpStringData;
              cat = "Eqp/Accessory";
          } else if (itemId >= 1000000 && itemId < 1010000) {
              theData = eqpStringData;
              cat = "Eqp/Cap";
          } else if (itemId >= 1102000 && itemId < 1103000) {
              theData = eqpStringData;
              cat = "Eqp/Cape";
          } else if (itemId >= 1040000 && itemId < 1050000) {
              theData = eqpStringData;
              cat = "Eqp/Coat";
          } else if (itemId >= 20000 && itemId < 22000) {
              theData = eqpStringData;
              cat = "Eqp/Face";
          } else if (itemId >= 1080000 && itemId < 1090000) {
              theData = eqpStringData;
              cat = "Eqp/Glove";
          } else if (itemId >= 30000 && itemId < 35000) {
              theData = eqpStringData;
              cat = "Eqp/Hair";
          } else if (itemId >= 1050000 && itemId < 1060000) {
              theData = eqpStringData;
              cat = "Eqp/Longcoat";
          } else if (itemId >= 1060000 && itemId < 1070000) {
              theData = eqpStringData;
              cat = "Eqp/Pants";
          } else if (itemId >= 1802000 && itemId < 1842000) {
              theData = eqpStringData;
              cat = "Eqp/PetEquip";
          } else if (itemId >= 1112000 && itemId < 1120000) {
              theData = eqpStringData;
              cat = "Eqp/Ring";
          } else if (itemId >= 1092000 && itemId < 1100000) {
              theData = eqpStringData;
              cat = "Eqp/Shield";
          } else if (itemId >= 1070000 && itemId < 1080000) {
              theData = eqpStringData;
              cat = "Eqp/Shoes";
          } else if (itemId >= 1900000 && itemId < 2000000) {
              theData = eqpStringData;
              cat = "Eqp/Taming";
          } else if (itemId >= 1300000 && itemId < 1800000) {
              theData = eqpStringData;
              cat = "Eqp/Weapon";
          } else {
              return null;
          }
          if (cat.equalsIgnoreCase("null")) {
              return theData.getChildByPath(String.valueOf(itemId));
          } else {
              return theData.getChildByPath(cat + "/" + itemId);
          }
      }
      
      public String getName(int itemId) {
          if (nameCache.containsKey(itemId)) {
              return nameCache.get(itemId);
          }
          MapleData strings = getStringData(itemId);
          if (strings == null) {
              return null;
          }
          String ret = MapleDataTool.getString("name", strings, null);
          nameCache.put(itemId, ret);
          return ret;
      }
  }
  