/*
 * OrderedLatentLiabilityTransformParser.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;

import com.sun.prism.MaskTextureGraphics;
import dr.evolution.datatype.DataType;
import dr.evolution.datatype.TwoStates;
import dr.evomodel.continuous.OrderedLatentLiabilityLikelihood;
import dr.inference.model.*;
import dr.inferencexml.model.MaskedParameterParser;
import dr.util.Transform;
import dr.xml.*;

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

/**
 * @author Marc A. Suchard
 */

public class OrderedLatentLiabilityTransformParser extends AbstractXMLObjectParser {

    public static final String NAME = "orderedLatentLiabilityTransform";

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

        OrderedLatentLiabilityLikelihood likelihood = (OrderedLatentLiabilityLikelihood)
                xo.getChild(OrderedLatentLiabilityLikelihood.class);

        CompoundParameter parameter = likelihood.getTipTraitParameter();

        DataType dataType = likelihood.getPatternList().getDataType();

        if (!(dataType instanceof TwoStates)) {
            throw new XMLParseException("Liability transformation is currently only implemented for binary traits");
        }

        Parameter mask = null;
        if (xo.hasChildNamed(MaskedParameterParser.MASKING)) {
            mask = (Parameter) xo.getElementFirstChild(MaskedParameterParser.MASKING);
        }

        List<Transform> transforms = new ArrayList<Transform>();
        int index = 0;
        for (int tip = 0; tip < parameter.getParameterCount(); ++tip) {

            final int[] tipData = likelihood.getData(tip);

            for (int trait = 0; trait < tipData.length; ++trait) {
                int discreteState = tipData[trait];
                boolean valid = true;

                Transform transform = null;
                if (discreteState == 0) {
                    transform = Transform.LOG_NEGATE;
//                    transforms.add(Transform.LOG_NEGATE);

                    if (parameter.getParameterValue(index) >= 0.0) {
                        valid = false;
                    }
                } else if (discreteState == 1) {
                    transform = Transform.LOG;
//                    transforms.add(Transform.LOG);

                    if (parameter.getParameterValue(index) <= 0.0) {
                        valid = false;
                    }
                } else {
                    transform = Transform.NONE;
//                    transforms.add(Transform.NONE);
                }

                if (!valid) {
                    throw new XMLParseException("Incompatible binary trait and latent value in tip '" +
                            parameter.getParameter(tip).getId() + "'");
                }

                if (mask == null || mask.getParameterValue(index) == 1.0) {
                    transforms.add(transform);
                }

                ++index;
            }
        }

//        Parameter cast = parameter;
//
//        if (xo.hasChildNamed(MaskedParameterParser.MASKING)) {
//            Parameter mask = (Parameter) xo.getElementFirstChild(MaskedParameterParser.MASKING);
//            cast = new MaskedParameter(parameter, mask, true);
//
//            List<Transform> newTransforms = new ArrayList<Transform>();
//            for (int i = 0; i < transforms.size(); ++i) {
//                if (mask.getParameterValue(i) == 1.0) {
//                    newTransforms.add(transforms.get(i));
//                }
//            }
//            transforms = newTransforms;
//        }


//        Parameter cast = (mask == null) ? parameter :
//                new MaskedParameter(parameter, mask, true);
//                new MaskedParameter(parameter);


//        System.err.println("dim = " + parameter.getDimension());
//        System.err.println("dim = " + cast.getDimension());
//        System.err.println("dim = " + transforms.size());
//        System.exit(-1);
//        cast = parameter; // TODO Remove

        Transform array = new Transform.Array(transforms, parameter);
        return array;
    }

    @Override
    public XMLSyntaxRule[] getSyntaxRules() {
        return new XMLSyntaxRule[] {
                new ElementRule(OrderedLatentLiabilityLikelihood.class),
                new ElementRule(MaskedParameterParser.MASKING,
                        new XMLSyntaxRule[]{
                                new ElementRule(Parameter.class)
                        }, true),
        };
    }

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

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

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

}

@@ -1049,50 +1049,8 @@
 s;%0A%0A
-import com.sun.prism.MaskTextureGraphics;%0A
 impo
@@ -1084,16 +1084,16 @@
 taType;%0A
+
 import d
@@ -2737,23 +2737,16 @@
 ransform
- = null
 ;%0A      
@@ -3869,26 +3869,24 @@
      %7D%0A%0A
-//
         
 Paramete
@@ -3881,1070 +3881,14 @@
     
-Parameter cast = parameter;%0A//%0A//        if (xo.hasChildNamed(MaskedParameterParser.MASKING)) %7B%0A//            Parameter mask = (Parameter) xo.getElementFirstChild(MaskedParameterParser.MASKING);%0A//            cast = new MaskedParameter(parameter, mask, true);%0A//%0A//            List%3CTransform%3E newTransforms = new ArrayList%3CTransform%3E();%0A//            for (int i = 0; i %3C transforms.size(); ++i) %7B%0A//                if (mask.getParameterValue(i) == 1.0) %7B%0A//                    newTransforms.add(transforms.get(i));%0A//                %7D%0A//            %7D%0A//            transforms = newTransforms;%0A//        %7D%0A%0A%0A//        Parameter cast = (mask == null) ? parameter :%0A//                new MaskedParameter(parameter, mask, true);%0A//                new MaskedParameter(parameter);%0A%0A%0A//        System.err.println(%22dim = %22 + parameter.getDimension());%0A//        System.err.println(%22dim = %22 + cast.getDimension());%0A//        System.err.println(%22dim = %22 + transforms.size());%0A//        System.exit(-1);%0A//        cast = parameter; // TODO Remove%0A%0A        Transform array =
+return
  new
@@ -3932,30 +3932,8 @@
 r);%0A
-        return array;%0A
     

fix incorrect import