/*
 * HomogenousBranchSubstitutionModel.java
 *
 * Copyright (c) 2002-2012 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.app.beagle.evomodel.sitemodel;

import beagle.Beagle;
import dr.app.beagle.evomodel.substmodel.EigenDecomposition;
import dr.app.beagle.evomodel.substmodel.FrequencyModel;
import dr.app.beagle.evomodel.substmodel.SubstitutionModel;
import dr.app.beagle.evomodel.treelikelihood.BufferIndexHelper;
import dr.evolution.tree.NodeRef;
import dr.evolution.tree.Tree;
import dr.inference.model.AbstractModel;
import dr.inference.model.Model;
import dr.inference.model.Parameter;
import dr.inference.model.Variable;

/**
 * @author Andrew Rambaut
 * @author Alexei Drummond
 * @version $Id$
 */
public class HomogenousBranchSubstitutionModel extends AbstractModel implements BranchSubstitutionModel {
    public HomogenousBranchSubstitutionModel(SubstitutionModel substModel, FrequencyModel frequencyModel) {
        super("HomogenousBranchSubstitutionModel");

        this.substModel = substModel;
        addModel(substModel);
        this.frequencyModel = frequencyModel;
        addModel(frequencyModel);
    }

    /**
     * Homogenous model - returns the same substitution model for all branches/categories
     *
     * @param branchIndex
     * @param categoryIndex
     * @return
     */
    public EigenDecomposition getEigenDecomposition(int branchIndex, int categoryIndex) {
        return substModel.getEigenDecomposition();
    }
    
	@Override
	public void setEigenDecomposition(Beagle beagle, int eigenIndex, BufferIndexHelper bufferHelper, int dummy) {
        EigenDecomposition ed = getEigenDecomposition(eigenIndex, dummy);

        
        
        beagle.setEigenDecomposition(
//                offsetIndex,
        		eigenIndex,
                ed.getEigenVectors(),
                ed.getInverseEigenVectors(),
                ed.getEigenValues());
		
	}   

    public SubstitutionModel getSubstitutionModel(int branchIndex, int categoryIndex) {
        return substModel;
    }

    /**
     * Homogenous model - returns the same frequency model for all categories
     *
     * @param categoryIndex
     * @return
     */
    public double[] getStateFrequencies(int categoryIndex) {
        return frequencyModel.getFrequencies();
    }

    /**
     * Homogenous model - returns if substitution model can return complex diagonalization
     *
     * @return
     */
    public boolean canReturnComplexDiagonalization() {
        return substModel.canReturnComplexDiagonalization();
    }

    /**
     * Homogenous model - always returns model 0
     *
     * @param tree
     * @param node
     * @return
     */
    public int getBranchIndex(final Tree tree, final NodeRef node, int bufferIndex) {
        return 0;
    }

    public int getEigenCount() {
        return 1;
    }

    private final SubstitutionModel substModel;
    private final FrequencyModel frequencyModel;

    protected void handleModelChangedEvent(Model model, Object object, int index) {
        fireModelChanged();
    }

    protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
    }

    protected void storeState() {
    }

    protected void restoreState() {
    }

    protected void acceptState() {
    }

    public void updateTransitionMatrices( Beagle beagle,
            int eigenIndex,
            BufferIndexHelper bufferHelper,
            final int[] probabilityIndices,
            final int[] firstDerivativeIndices,
            final int[] secondDervativeIndices,
            final double[] edgeLengths,
            int count) {
        beagle.updateTransitionMatrices(eigenIndex, probabilityIndices, firstDerivativeIndices,
                secondDervativeIndices, edgeLengths, count);
    }

	@Override
	public int getExtraBufferCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void setFirstBuffer(int bufferCount) {
		// TODO Auto-generated method stub
		
	}
}

@@ -1377,16 +1377,51 @@
 e.Tree;%0A
+import dr.evomodel.tree.TreeModel;%0A
 import d
@@ -4705,16 +4705,465 @@
 count);%0A
+        %0A        //////////////////////////////////////////////////////%0A        %0A%09%09for(int k =0;k%3CprobabilityIndices.length;k++)%7B%0A%09%09%0A%09%09double tmp%5B%5D = new double%5B4 * 4 * 4%5D;%0A%09%09beagle.getTransitionMatrix(probabilityIndices%5Bk%5D, // matrixIndex%0A%09%09%09%09tmp // outMatrix%0A%09%09%09%09);%0A%09%09%0A%09%09System.out.println(probabilityIndices%5Bk%5D);%0A%09%09EpochBranchSubstitutionModel.printMatrix(tmp, 4, 4);%0A%09%09%7D%0A        %0A       //////////////////////////////////////////////////////%0A%09%09%0A
     %7D%0A%0A%09
@@ -5200,24 +5200,43 @@
 BufferCount(
+TreeModel treeModel
 ) %7B%0A%09%09// TOD

pass the tree as argument