package csa.jmom.unit;

import csa.util.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;

public class  UnitData
{
	protected String mClass="";
	public String mName="";
	protected String mPeople="";
	protected Vector<String> mBattlefieldCompositeTileName=new Vector<String>();
	protected Vector<String> mBattlefieldCompositeFileName=new Vector<String>();
	protected Vector<String> mWorldCompositeTileName=new Vector<String>();
	protected Vector<String> mWorldCompositeFileName=new Vector<String>();
	protected int mNumberOfSingleUnits=0;
	protected String mNameOfSingleBattlefieldUnitTile="";
	protected int mBaseMeleeAttack=0;
	protected int mBaseMeleeDefense=0;
	protected int mBaseRangeAttack=0;
	protected int mBaseRangeDefense=0;
	protected int mBaseSpellPoints=0;
	protected int mBaseSpellHit=0;
	protected int mBaseSpellResistence=0;
	protected Vector<String> mKnownSpells=new Vector<String>();
	protected int mStackPriority=0;
	protected int mEndurance=0;
	protected Vector<String> mAbilities=new Vector<String>();
	public String getName()
	{
		return mName;
	}
	public void setName(String n)
	{
		mName=n;
	}
	public String getCClass()
	{
		return mClass;
	}
	public void setCClass(String c)
	{
		mClass=c;
	}
	public String getPeople()
	{
		return mPeople;
	}
	public void setPeople(String People)
	{
		mPeople=People;
	}
	public Vector<String> getBattlefieldCompositeTileName()
	{
		return mBattlefieldCompositeTileName;
	}
	public void setBattlefieldCompositeTileName(Vector<String> BattlefieldCompositeTileName)
	{
		mBattlefieldCompositeTileName=BattlefieldCompositeTileName;
	}
	public Vector<String> getBattlefieldCompositeFileName()
	{
		return mBattlefieldCompositeFileName;
	}
	public void setBattlefieldCompositeFileName(Vector<String> BattlefieldCompositeFileName)
	{
		mBattlefieldCompositeFileName=BattlefieldCompositeFileName;
	}
	public Vector<String> getWorldCompositeTileName()
	{
		return mWorldCompositeTileName;
	}
	public void setWorldCompositeTileName(Vector<String> WorldCompositeTileName)
	{
		mWorldCompositeTileName=WorldCompositeTileName;
	}
	public Vector<String> getWorldCompositeFileName()
	{
		return mWorldCompositeFileName;
	}
	public void setWorldCompositeFileName(Vector<String> WorldCompositeFileName)
	{
		mWorldCompositeFileName=WorldCompositeFileName;
	}
	public int getNumberOfSingleUnits()
	{
		return mNumberOfSingleUnits;
	}
	public void setNumberOfSingleUnits(int NumberOfSingleUnits)
	{
		mNumberOfSingleUnits=NumberOfSingleUnits;
	}
	public String getNameOfSingleBattlefieldUnitTile()
	{
		return mNameOfSingleBattlefieldUnitTile;
	}
	public void setNameOfSingleBattlefieldUnitTile(String NameOfSingleBattlefieldUnitTile)
	{
		mNameOfSingleBattlefieldUnitTile=NameOfSingleBattlefieldUnitTile;
	}
	public int getBaseMeleeAttack()
	{
		return mBaseMeleeAttack;
	}
	public void setBaseMeleeAttack(int BaseMeleeAttack)
	{
		mBaseMeleeAttack=BaseMeleeAttack;
	}
	public int getBaseMeleeDefense()
	{
		return mBaseMeleeDefense;
	}
	public void setBaseMeleeDefense(int BaseMeleeDefense)
	{
		mBaseMeleeDefense=BaseMeleeDefense;
	}
	public int getBaseRangeAttack()
	{
		return mBaseRangeAttack;
	}
	public void setBaseRangeAttack(int BaseRangeAttack)
	{
		mBaseRangeAttack=BaseRangeAttack;
	}
	public int getBaseRangeDefense()
	{
		return mBaseRangeDefense;
	}
	public void setBaseRangeDefense(int BaseRangeDefense)
	{
		mBaseRangeDefense=BaseRangeDefense;
	}
	public int getBaseSpellPoints()
	{
		return mBaseSpellPoints;
	}
	public void setBaseSpellPoints(int BaseSpellPoints)
	{
		mBaseSpellPoints=BaseSpellPoints;
	}
	public int getBaseSpellHit()
	{
		return mBaseSpellHit;
	}
	public void setBaseSpellHit(int BaseSpellHit)
	{
		mBaseSpellHit=BaseSpellHit;
	}
	public int getBaseSpellResistence()
	{
		return mBaseSpellResistence;
	}
	public void setBaseSpellResistence(int BaseSpellResistence)
	{
		mBaseSpellResistence=BaseSpellResistence;
	}
	public Vector<String> getKnownSpells()
	{
		return mKnownSpells;
	}
	public void setKnownSpells(Vector<String> KnownSpells)
	{
		mKnownSpells=KnownSpells;
	}
	public int getStackPriority()
	{
		return mStackPriority;
	}
	public void setStackPriority(int StackPriority)
	{
		mStackPriority=StackPriority;
	}
	public int getEndurance()
	{
		return mEndurance;
	}
	public void setEndurance(int Endurance)
	{
		mEndurance=Endurance;
	}
	public Vector<String> getAbilities()
	{
		return mAbilities;
	}
	public void setAbilities(Vector<String> Abilities)
	{
		mAbilities=Abilities;
	}
	private String exportXML()
	{
		StringBuffer s = new StringBuffer();
		s.append( "\t<UnitData>\n");
		s.append( "\t\t<Class>"+UtilityString.toXML(mClass)+"</Class>\n");
		s.append( "\t\t<Name>"+UtilityString.toXML(mName)+"</Name>\n");
		s.append( "\t\t<People>"+UtilityString.toXML(mPeople)+"</People>\n");
		s.append( "\t\t<BattlefieldCompositeTileNames>\n");
		for (int i=0;i<mBattlefieldCompositeTileName.size(); i++)
		{
			s.append( "\t\t\t<BattlefieldCompositeTileName>"+UtilityString.toXML(mBattlefieldCompositeTileName.elementAt(i))+"</BattlefieldCompositeTileName>\n");
		}
		s.append( "\t\t</BattlefieldCompositeTileNames>\n");
		s.append( "\t\t<BattlefieldCompositeFileNames>\n");
		for (int i=0;i<mBattlefieldCompositeFileName.size(); i++)
		{
			s.append( "\t\t\t<BattlefieldCompositeFileName>"+UtilityString.toXML(mBattlefieldCompositeFileName.elementAt(i))+"</BattlefieldCompositeFileName>\n");
		}
		s.append( "\t\t</BattlefieldCompositeFileNames>\n");
		s.append( "\t\t<WorldCompositeTileNames>\n");
		for (int i=0;i<mWorldCompositeTileName.size(); i++)
		{
			s.append( "\t\t\t<WorldCompositeTileName>"+UtilityString.toXML(mWorldCompositeTileName.elementAt(i))+"</WorldCompositeTileName>\n");
		}
		s.append( "\t\t</WorldCompositeTileNames>\n");
		s.append( "\t\t<WorldCompositeFileNames>\n");
		for (int i=0;i<mWorldCompositeFileName.size(); i++)
		{
			s.append( "\t\t\t<WorldCompositeFileName>"+UtilityString.toXML(mWorldCompositeFileName.elementAt(i))+"</WorldCompositeFileName>\n");
		}
		s.append( "\t\t</WorldCompositeFileNames>\n");
		s.append( "\t\t<NumberOfSingleUnits>"+mNumberOfSingleUnits+"</NumberOfSingleUnits>\n");
		s.append( "\t\t<NameOfSingleBattlefieldUnitTile>"+UtilityString.toXML(mNameOfSingleBattlefieldUnitTile)+"</NameOfSingleBattlefieldUnitTile>\n");
		s.append( "\t\t<NameOfSingleBattlefieldUnitTile>"+mBaseMeleeAttack+"</NameOfSingleBattlefieldUnitTile>\n");
		s.append( "\t\t<BaseMeleeDefense>"+mBaseMeleeDefense+"</BaseMeleeDefense>\n");
		s.append( "\t\t<BaseRangeAttack>"+mBaseRangeAttack+"</BaseRangeAttack>\n");
		s.append( "\t\t<BaseRangeDefense>"+mBaseRangeDefense+"</BaseRangeDefense>\n");
		s.append( "\t\t<BaseSpellPoints>"+mBaseSpellPoints+"</BaseSpellPoints>\n");
		s.append( "\t\t<BaseSpellHit>"+mBaseSpellHit+"</BaseSpellHit>\n");
		s.append( "\t\t<BaseSpellResistence>"+mBaseSpellResistence+"</BaseSpellResistence>\n");
		s.append( "\t\t<KnownSpellss>\n");
		for (int i=0;i<mKnownSpells.size(); i++)
		{
			s.append( "\t\t\t<KnownSpells>"+UtilityString.toXML(mKnownSpells.elementAt(i))+"</KnownSpells>\n");
		}
		s.append( "\t\t</KnownSpellss>\n");
		s.append( "\t\t<StackPriority>"+mStackPriority+"</StackPriority>\n");
		s.append( "\t\t<Endurance>"+mEndurance+"</Endurance>\n");
		s.append( "\t\t<Abilitiess>\n");
		for (int i=0;i<mAbilities.size(); i++)
		{
			s.append( "\t\t\t<Abilities>"+UtilityString.toXML(mAbilities.elementAt(i))+"</Abilities>\n");
		}
		s.append( "\t\t</Abilitiess>\n");
		s.append( "\t</UnitData>\n");
		return s.toString();
	}
	@Override public String toString()
	{
		return mName;
	}
	private static UnitDataXMLHandler XMLHANDLER = new UnitDataXMLHandler();
	public static UnitDataXMLHandler getXMLParseHandler()
	{
		return XMLHANDLER;
	}
	public static boolean saveCollectionAsXML(String filename, Collection<UnitData> col)
	{
		try
		{
			PrintWriter pw = new PrintWriter(csa.Global.mBaseDir+filename, "UTF-8");
			pw.print("<?xml version=\"1.0\"?>\n");
			pw.print("<AllUnitData>\n");
			Iterator<UnitData> iter = col.iterator();
			while (iter.hasNext())
			{
				UnitData item = iter.next();
				pw.print(item.exportXML());
			}
			pw.print("</AllUnitData>\n");
			pw.close();
		}
		catch (IOException e)
		{
			System.err.println(e.toString());
			return false;
		}
		return true;
	}
	public static HashMap<String, UnitData> getHashMapFromXML(String filename)
	{
		HashMap<String, UnitData> filters = new HashMap<String, UnitData>();
		try
		{
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();
			UnitDataXMLHandler h = UnitData.getXMLParseHandler();
			saxParser.parse(csa.Global.mBaseDir+filename, h);
			filters = h.getLastHashMap();
		}
		catch (Throwable e)
		{
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.toString() ,"UnitData Load Error...",  JOptionPane.INFORMATION_MESSAGE);
		}
		return filters;
	}
}
