/*
 * NodeHeightGradientParser.java
 *
 * Copyright (c) 2002-2017 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.continuous.hmc;

import dr.evomodel.branchratemodel.ArbitraryBranchRates;
import dr.evomodel.branchratemodel.BranchRateModel;
import dr.evomodel.branchratemodel.BranchSpecificFixedEffects;
import dr.evomodel.branchratemodel.DefaultBranchRateModel;
import dr.evomodel.treedatalikelihood.BeagleDataLikelihoodDelegate;
import dr.evomodel.treedatalikelihood.DataLikelihoodDelegate;
import dr.evomodel.treedatalikelihood.TreeDataLikelihood;
import dr.evomodel.treedatalikelihood.continuous.ContinuousDataLikelihoodDelegate;
import dr.evomodel.treedatalikelihood.discrete.HyperParameterBranchRateGradient;
import dr.evomodel.treedatalikelihood.discrete.LocationGradient;
import dr.evomodel.treedatalikelihood.discrete.ScaleGradient;
import dr.evomodelxml.treelikelihood.TreeTraitParserUtilities;
import dr.inference.model.Parameter;
import dr.xml.*;

import static dr.evomodelxml.treelikelihood.TreeTraitParserUtilities.DEFAULT_TRAIT_NAME;

public class LocationScaleGradientParser extends AbstractXMLObjectParser {

    private static final String NAME = "locationScaleGradient";
    private static final String LOCATION = "location";
    private static final String SCALE = "scale";

    private static final String TRAIT_NAME = TreeTraitParserUtilities.TRAIT_NAME;
    private static final String USE_HESSIAN = "useHessian";

    public String getParserName(){ return NAME; }

    public Object parseXMLObject(XMLObject xo) throws XMLParseException {

        String traitName = xo.getAttribute(TRAIT_NAME, DEFAULT_TRAIT_NAME);
        boolean useHessian = xo.getAttribute(USE_HESSIAN, false);

        final TreeDataLikelihood treeDataLikelihood = (TreeDataLikelihood) xo.getChild(TreeDataLikelihood.class);
//        final Parameter locationScaleParameter = (Parameter) xo.getChild(Parameter.class);
        BranchRateModel branchRateModel = treeDataLikelihood.getBranchRateModel();

        if (branchRateModel instanceof DefaultBranchRateModel || branchRateModel instanceof ArbitraryBranchRates) {

            DataLikelihoodDelegate delegate = treeDataLikelihood.getDataLikelihoodDelegate();

            if (delegate instanceof ContinuousDataLikelihoodDelegate) {

                throw new XMLParseException("Not yet implemented! ");

            } else if (delegate instanceof BeagleDataLikelihoodDelegate) {

                BeagleDataLikelihoodDelegate beagleData = (BeagleDataLikelihoodDelegate) delegate;

                if (xo.hasChildNamed(LOCATION)) {
                    Object locationObject = xo.getElementFirstChild(LOCATION);
                    BranchSpecificFixedEffects location = null;

                    if (locationObject instanceof Parameter) {
                        location = new BranchSpecificFixedEffects.None((Parameter) xo.getElementFirstChild(LOCATION));
                    } else if (locationObject instanceof BranchSpecificFixedEffects) {
                        location = (BranchSpecificFixedEffects) locationObject;
                    } else {
                        throw new XMLParseException("Poorly formed");
                    }

                    return new LocationGradient(traitName, treeDataLikelihood, beagleData, location, useHessian);

                } else if (xo.hasChildNamed(SCALE)) {

                    Parameter scale = (Parameter) xo.getElementFirstChild(SCALE);
                    return new ScaleGradient(traitName, treeDataLikelihood, beagleData, scale, useHessian);

                } else {
                    throw new XMLParseException("Poorly formed");
                }
            } else {
                throw new XMLParseException("Unknown likelihood delegate type");
            }

        } else {
            throw new XMLParseException("Only implemented for an arbitrary rates model");
        }
    }

    @Override
    public XMLSyntaxRule[] getSyntaxRules() {
        return rules;
    }

    private final XMLSyntaxRule[] rules = {
            AttributeRule.newStringRule(TRAIT_NAME),

            new ElementRule(TreeDataLikelihood.class),

            new XORRule(
                new ElementRule(LOCATION, new XMLSyntaxRule[]{
                        new XORRule(
                                new ElementRule(BranchSpecificFixedEffects.class),
                                new ElementRule(Parameter.class)
                        )
                }),
                new ElementRule(SCALE, new XMLSyntaxRule[]{
                        new ElementRule(Parameter.class),
                })
            ),
    };

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

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

@@ -1036,16 +1036,60 @@
 s.hmc;%0A%0A
+import dr.evomodel.branchmodel.BranchModel;%0A
 import d
@@ -2950,129 +2950,8 @@
 );%0A%0A
-        if (branchRateModel instanceof DefaultBranchRateModel %7C%7C branchRateModel instanceof ArbitraryBranchRates) %7B%0A%0A    
     
@@ -3041,28 +3041,24 @@
 );%0A%0A        
-   
- 
 if (delegate
@@ -3069,16 +3069,17 @@
 tanceof 
+ 
 Continuo
@@ -3098,34 +3098,32 @@
 hoodDelegate) %7B%0A
-%0A 
                t
@@ -3110,35 +3110,32 @@
 ) %7B%0A            
-   
 throw new XMLPar
@@ -3172,25 +3172,24 @@
 ed! %22);%0A
-%0A
         
     %7D el
@@ -3172,36 +3172,32 @@
 ed! %22);%0A        
-    
 %7D else if (deleg
@@ -3211,16 +3211,17 @@
 tanceof 
+ 
 BeagleDa
@@ -3240,29 +3240,24 @@
 Delegate) %7B%0A
-%0A    
             
@@ -3340,16 +3340,137 @@
 egate;%0A%0A
+%0A            if (branchRateModel instanceof DefaultBranchRateModel %7C%7C branchRateModel instanceof ArbitraryBranchRates) %7B%0A
         
@@ -3649,15 +3649,8 @@
 tion
- = null
 ;%0A%0A 
@@ -4659,40 +4659,53 @@
 on(%22
-Unknown likelihood delegate type
+Only implemented for an arbitrary rates model
 %22);%0A
@@ -4710,33 +4710,32 @@
 ;%0A            %7D%0A
-%0A
         %7D else %7B
@@ -4780,53 +4780,40 @@
 on(%22
-Only implemented for an arbitrary rates model
+Unknown likelihood delegate type
 %22);%0A

change if else statement order