/*
 * BranchSpecificSubstitutionRateModelParser.java
 *
 * Copyright (c) 2002-2018 Alexei Drummond, Andrew Rambaut and Marc Suchard
 *
 * This file is part of BEAST.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership and licensing.
 *
 * BEAST is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 *  BEAST 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with BEAST; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

package dr.evomodelxml.branchmodel;

import dr.evomodel.branchmodel.BranchSpecificSubstitutionParameterBranchModel;
import dr.evomodel.branchratemodel.BranchRateModel;
import dr.evomodel.substmodel.ParameterReplaceableSubstitutionModel;
import dr.evomodel.tree.TreeModel;
import dr.inference.model.Parameter;
import dr.xml.*;

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

/**
 * @author Marc Suchard
 * @author Xiang Ji
 */
public class BranchSpecificSubstitutionParameterBranchModelParser extends AbstractXMLObjectParser {
    private static final String OLD_PARAMETER = "substitutionParameters";
    private static final String NEW_PARAMETER = "branchRateModels";
    private static final String BRANCH_SPECIFIC_SUBSTITUTION_PARAMETER_BRANCH_MODEL = "branchSpecificSubstitutionParameterBranchModel";

    @Override
    public Object parseXMLObject(XMLObject xo) throws XMLParseException {
        List<Parameter> parameters = new ArrayList<>();
        for (XMLObject xoc : xo.getAllChildren(OLD_PARAMETER)) {
            Parameter parameter = (Parameter) xoc.getChild(Parameter.class);
            parameters.add(parameter);
        }

        List<BranchRateModel> branchRateModels = new ArrayList<>();
        for (XMLObject xoc : xo.getAllChildren(NEW_PARAMETER)) {
            BranchRateModel branchRateModel = (BranchRateModel) xoc.getChild(BranchRateModel.class);
            branchRateModels.add(branchRateModel);
        }

        ParameterReplaceableSubstitutionModel substitutionModel = (ParameterReplaceableSubstitutionModel) xo.getChild(ParameterReplaceableSubstitutionModel.class);
        TreeModel tree = (TreeModel) xo.getChild(TreeModel.class);

        return new BranchSpecificSubstitutionParameterBranchModel(BRANCH_SPECIFIC_SUBSTITUTION_PARAMETER_BRANCH_MODEL,
                parameters, branchRateModels, substitutionModel, tree);
    }

    @Override
    public XMLSyntaxRule[] getSyntaxRules() {
        return new XMLSyntaxRule[]{
                new ElementRule(ParameterReplaceableSubstitutionModel.class, "The substitution model throughout the tree."),
                new ElementRule(OLD_PARAMETER, Parameter.class, "Parameters of the substitution model to be replaced.",
                        0, Integer.MAX_VALUE),
                new ElementRule(NEW_PARAMETER, BranchRateModel.class, "BranchRateModels to store the branchSpecific parameters.",
                        0, Integer.MAX_VALUE),
                new ElementRule(TreeModel.class)
        };
    }

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

    @Override
    public Class getReturnType() {
        return BranchSpecificSubstitutionParameterBranchModel.class;
    }

    @Override
    public String getParserName() {
        return BRANCH_SPECIFIC_SUBSTITUTION_PARAMETER_BRANCH_MODEL;
    }
}

@@ -1911,16 +1911,17 @@
 ption %7B%0A
+%0A
         
@@ -1968,37 +1968,32 @@
 st%3C%3E();%0A        
-for (
 XMLObject xoc : 
@@ -1990,35 +1990,29 @@
 ect xoc 
-:
+=
  xo.get
-All
 Child
-ren
 (OLD_PAR
@@ -2010,32 +2010,86 @@
 d(OLD_PARAMETER)
+;%0A        for (int i = 0; i %3C xoc.getChildCount(); ++i
 ) %7B%0A            
@@ -2135,31 +2135,17 @@
 etChild(
-Parameter.class
+i
 );%0A     
@@ -2269,46 +2269,25 @@
     
-for (XMLObject 
 xoc 
-:
+=
  xo.get
-All
 Child
-ren
 (NEW
@@ -2297,16 +2297,70 @@
 RAMETER)
+;%0A        for (int i = 0; i %3C xoc.getChildCount(); ++i
 ) %7B%0A    
@@ -2432,37 +2432,17 @@
 etChild(
-BranchRateModel.class
+i
 );%0A     

fix parser for multiple parameters substitutions per model