/*
 * The MIT License
 *
 * Copyright 2013 Dra0211.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package kinugasa.field;

import java.awt.image.BufferedImage;
import java.util.List;
import java.util.Map;
import kinugasa.contents.graphics.ImageUtil;
import kinugasa.contents.graphics.SerializableImage;
import kinugasa.contents.resource.ContentsFileNotFoundException;
import kinugasa.contents.resource.ContentsIOException;
import kinugasa.contents.resource.IllegalXMLFormatException;
import kinugasa.contents.resource.NameNotFoundException;
import kinugasa.contents.resource.Storage;
import kinugasa.contents.text.XMLElement;
import kinugasa.contents.text.XMLFileSupport;
import kinugasa.contents.text.XMLReader;

/**
 * 全てのチップセットを登録するストレージです.
 * <br>
 * チップセットのXMLは、kinugasaChipSet.dtdを使用します。<br>
 * チップセットをXMLからロードする際のフォーマットはDTDを確認してください。<br>
 * <br>
 * @version 1.0.0 - 2013/04/28_23:30:28<br>
 * @author Dra0211<br>
 */
public class ChipSetStorage extends Storage<ChipSet>
		implements XMLFileSupport {

	/** 唯一のインスタンスです. */
	private static final ChipSetStorage INSTANCE = new ChipSetStorage();

	/**
	 * シングルトンクラスです.
	 * getInstanceを使用してください。<br>
	 */
	private ChipSetStorage() {
	}

	/**
	 * インスタンスを取得します.
	 * @return ChipSetStorageのインスタンスを返します。<br>
	 */
	public static ChipSetStorage getInstance() {
		return INSTANCE;
	}

	/**
	 * チップセットの構築を行います.
	 * <br>
	 * {@inheritDoc }
	 * @throws NumberFormatException 切り出しサイズの数値がint型に変換できない場合に投げられます。<br>
	 * @throws NameNotFoundException 指定された名前を持つチップ属性が
	 * ChipAttributeStorageに登録されていない場合に投げられます。<br>
	 */
	@Override
	public void readFromXML(String filePath)
			throws IllegalXMLFormatException, ContentsFileNotFoundException,
			ContentsIOException, NumberFormatException,
			NameNotFoundException {
		XMLReader reader = new XMLReader(filePath).load();
		XMLElement root = reader.getFirst();

		if (!"kinugasaChipSet".equals(root.getName())) {
			throw new IllegalXMLFormatException("illegal root node name :" + root);
		}

		List<XMLElement> chipSetElementList = root.getElement("chipSet");
		for (int i = 0, chipSetSize = chipSetElementList.size(); i < chipSetSize; i++) {
			XMLElement chipSetElement = chipSetElementList.get(i);
			int cutWidth = chipSetElement.getAttributes().get("cutWidth").getIntValue();
			int cutHeight = chipSetElement.getAttributes().get("cutHeight").getIntValue();
			ChipSet chipSet = new ChipSet(chipSetElement.getAttributes().get("name").getValue(),
					cutWidth, cutHeight);
			Map<String, BufferedImage> imageMap = ImageUtil.splitAsMap(
					ImageUtil.load(chipSetElement.getAttributes().get("image").getValue()),
					cutWidth, cutHeight);
			List<XMLElement> mapChipElementList = chipSetElement.getElement("mapChip");
			for (int j = 0, mapChipSize = mapChipElementList.size(); j < mapChipSize; j++) {
				XMLElement mapChipElement = mapChipElementList.get(j);
				String mapChipName = mapChipElement.getAttributes().get("name").getValue();
				ChipAttribute attribute = ChipAttributeStorage.getInstance().get(mapChipElement.getAttributes().get("attribute").getValue());
				chipSet.add(new MapChip(
						mapChipName,
						new SerializableImage(imageMap.get(mapChipName)),
						attribute));
			}
		}
		reader.free();
	}
}
