package dr.app.beagle.evomodel.parsers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import dr.app.beagle.evomodel.sitemodel.EpochBranchSubstitutionModel;
import dr.app.beagle.evomodel.substmodel.FrequencyModel;
import dr.app.beagle.evomodel.substmodel.SubstitutionModel;
import dr.evolution.datatype.DataType;
import dr.evomodel.branchratemodel.BranchRateModel;
import dr.evomodel.substmodel.AbstractSubstitutionModel;
import dr.inference.model.Parameter;
import dr.xml.AbstractXMLObjectParser;
import dr.xml.ElementRule;
import dr.xml.XMLObject;
import dr.xml.XMLParseException;
import dr.xml.XMLSyntaxRule;

public class BeagleSubstitutionEpochModelParser extends AbstractXMLObjectParser {

	public static final String SUBSTITUTION_EPOCH_MODEL = "beagleSubstitutionEpochModel";
	public static final String MODELS = "models";
	public static final String TRANSITION_TIMES = "transitionTimes";

	@Override
	public Object parseXMLObject(XMLObject xo) throws XMLParseException {

		DataType dataType = null;
		List<FrequencyModel> frequencyModelList = new ArrayList<FrequencyModel>();
		List<SubstitutionModel> substModelList = new ArrayList<SubstitutionModel>();
		XMLObject cxo = xo.getChild(MODELS);

		for (int i = 0; i < cxo.getChildCount(); i++) {

			SubstitutionModel substModel = (SubstitutionModel) cxo.getChild(i);

			if (dataType == null) {

				dataType = substModel.getDataType();

			} else if (dataType != substModel.getDataType()) {

				throw new XMLParseException(
						"Substitution models across epoches must use the same data type.");

			}//END: dataType check

			if (frequencyModelList.size() == 0) {

				frequencyModelList.add(substModel.getFrequencyModel());

			} else if (frequencyModelList.get(0) != substModel.getFrequencyModel()) {

				throw new XMLParseException(
						"Substitution models across epoches must currently use the same frequency model.\n Harass Marc to fix this.");

			}//END: freqModels no check

			substModelList.add(substModel);
		}//END: i loop

		BranchRateModel branchRateModel = (BranchRateModel)xo.getChild(BranchRateModel.class);
		
		Parameter epochTransitionTimes = (Parameter) xo
				.getChild(Parameter.class);

		if (epochTransitionTimes.getDimension() != substModelList.size() - 1) {
			throw new XMLParseException(
					"# of transition times must equal # of substitution models - 1\n"
							+ epochTransitionTimes.getDimension() + "\n"
							+ substModelList.size());
		}

	    // quietly sort in increasing order
		double sortedEpochTransitionTimes[] = epochTransitionTimes.getAttributeValue();
		Arrays.sort(sortedEpochTransitionTimes);
		for(int i = 0; i < epochTransitionTimes.getDimension(); i ++) {
			epochTransitionTimes.setParameterValueQuietly(i, sortedEpochTransitionTimes[i]);
		}//END: i loop

		return new EpochBranchSubstitutionModel(substModelList, 
				frequencyModelList, 
				branchRateModel,
				epochTransitionTimes);
	}// END: parseXMLObject

	@Override
	public XMLSyntaxRule[] getSyntaxRules() {

//        return new XMLSyntaxRule[]{
//                new ElementRule(MODELS,
//                        new XMLSyntaxRule[]{
//                                new ElementRule(AbstractSubstitutionModel.class, 1, Integer.MAX_VALUE),
//                        }
//                ),
//                new ElementRule(Parameter.class),
//        };

		return null;
	}

	@Override
	public String getParserDescription() {
		return null;
	}

	@Override
	public Class<EpochBranchSubstitutionModel> getReturnType() {
		return EpochBranchSubstitutionModel.class;
	}

	public String getParserName() {
		return SUBSTITUTION_EPOCH_MODEL;
	}

}// END: class

@@ -173,32 +173,96 @@
 stitutionModel;%0A
+import dr.app.beagle.evomodel.substmodel.BaseSubstitutionModel;%0A
 import dr.app.be
@@ -502,32 +502,73 @@
 stitutionModel;%0A
+import dr.inference.model.AbstractModel;%0A
 import dr.infere
@@ -3122,18 +3122,16 @@
 es() %7B%0A%0A
-//
         
@@ -3150,34 +3150,32 @@
 MLSyntaxRule%5B%5D%7B%0A
-//
                 
@@ -3198,18 +3198,16 @@
 MODELS,%0A
-//
         
@@ -3243,18 +3243,16 @@
 Rule%5B%5D%7B%0A
-//
         
@@ -3295,36 +3295,24 @@
 ule(Abstract
-Substitution
 Model.class,
@@ -3335,18 +3335,81 @@
 VALUE),%0A
-//
+                                //TODO other subst model classes%0A
         
@@ -3426,18 +3426,16 @@
       %7D%0A
-//
         
@@ -3445,18 +3445,72 @@
      ),%0A
-//
+                new ElementRule(BranchRateModel.class),%0A
         
@@ -3551,18 +3551,16 @@
 class),%0A
-//
         
@@ -3563,16 +3563,18 @@
     %7D;%0A%0A
+//
 %09%09return
@@ -3574,32 +3574,53 @@
 %09return null;%0A%09%7D
+//END: getSyntaxRules
 %0A%0A%09@Override%0A%09pu

throw meaningful xml parse exceptions