///////////////////////////////////////////////////////////////////////////////
// For information as to what this class does, see the Javadoc, below.       //
// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,       //
// 2007, 2008, 2009, 2010, 2014, 2015, 2022 by Peter Spirtes, Richard        //
// Scheines, Joseph Ramsey, and Clark Glymour.                               //
//                                                                           //
// This program is free software; you can redistribute it and/or modify      //
// it under the terms of the GNU General Public License as published by      //
// the Free Software Foundation; either version 2 of the License, or         //
// (at your option) any later version.                                       //
//                                                                           //
// This program 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 General Public License for more details.                              //
//                                                                           //
// You should have received a copy of the GNU General Public License         //
// along with this program; if not, write to the Free Software               //
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA //
///////////////////////////////////////////////////////////////////////////////
package edu.cmu.tetrad.data;

import edu.cmu.tetrad.graph.Node;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Stores a list of data models and keeps track of which one is selected.
 *
 * @author josephramsey
 * @see DataModel
 */
public final class DataModelList extends AbstractList<DataModel>
        implements DataModel {

    static final long serialVersionUID = 23L;

    /**
     * The list of models.
     *
     * @serial
     */
    private List<DataModel> modelList = new LinkedList<>();

    /**
     * The selected model (may be null).
     *
     * @serial
     */
    private DataModel selectedModel;

    /**
     * The name of the DataModelList.
     *
     * @serial
     */
    private String name;

    /**
     * The knowledge for this data.
     *
     * @serial
     */
    private Knowledge knowledge = new Knowledge();

    //===========================CONSTRUCTORS============================//
    public DataModelList() {
    }

    public DataModelList(DataModelList dataModelList) {

        try {
            throw new NullPointerException();
        } catch (Exception e) {
            e.printStackTrace();
        }

        this.modelList = new ArrayList<>(dataModelList);
        this.selectedModel = dataModelList.selectedModel;
        this.name = dataModelList.name;
        this.knowledge = dataModelList.knowledge.copy();
    }

    /**
     * Generates a simple exemplar of this class to test serialization.
     */
    public static DataModelList serializableInstance() {
        return new DataModelList();
    }

    //===========================PUBLIC METHODS (see AbstractList too)===//

    /**
     * @return this model, as an Object.
     */
    public DataModel get(int index) {
        return this.modelList.get(index);
    }

    /**
     * @return the size of the getModel list. Required for AbstractList.
     */
    @Override
    public int size() {
        return this.modelList.size();
    }

    public List<Node> getVariables() {
        if (getSelectedModel() == null) {
            throw new NullPointerException();
        }
        return getSelectedModel().getVariables();
    }

    public Knowledge getKnowledge() {
        return this.knowledge.copy();
    }

    public void setKnowledge(Knowledge knowledge) {
        if (knowledge == null) {
            throw new NullPointerException();
        }

        this.knowledge = knowledge.copy();
    }

    /**
     * @return the list of variable names for columns, in order.
     */
    public List<String> getVariableNames() {
        if (getSelectedModel() == null) {
            throw new NullPointerException();
        }
        return getSelectedModel().getVariableNames();
    }

    /**
     * Adds the given DataModel to the list at the given index. Required for
     * AbstractList.
     *
     * @param index   the index at which the DataModel is to be added.
     * @param element the DataModel to be added. (Note that this must be a
     *                DataModel.)
     */
    public void add(int index, DataModel element) {
        this.modelList.add(index, element);
    }

    /**
     * Check if the modeList is empty Need to override this since this class is
     * extending AbstractList.
     */
    @Override
    public boolean isEmpty() {
        return this.modelList.isEmpty();
    }

    /**
     * Use this to check if the dataModelList only contains the default empty
     * dataset that is being used to populat the empty spreadsheet - Added by
     * Kevin
     */
    public boolean containsEmptyData() {
        if (this.modelList.isEmpty()) {
            return true;
        } else {
            return this.modelList.get(0).getVariableNames().isEmpty();
        }
    }

    public List<DataModel> getModelList() {
        return this.modelList;
    }

    /**
     * Removes the DataModel at the given index. Required for AbstractList.
     * Required for AbstractList.
     *
     * @param index the index of the DataModel to remove.
     * @return the DataModel just removed.
     */
    public DataModel remove(int index) {
        DataModel removedObject = this.modelList.remove(index);

        if (removedObject == this.selectedModel) {
            this.selectedModel = null;
        }

        return removedObject;
    }

    /**
     * @return the model that is currently selected. The default is the first
     * model. If there are no models in the list, null is returned.
     */
    public DataModel getSelectedModel() {
        if (this.selectedModel != null) {
            return this.selectedModel;
        } else if (this.modelList.size() > 0) {
            return this.modelList.get(0);
        } else {
            return null;
        }
    }

    public void setSelectedModel(DataModel model) {
        if (model == null) {
            throw new NullPointerException();
        }

        if (this.modelList.contains(model)) {
            this.selectedModel = model;
        }
    }

    /**
     * Gets the name of the data model list.
     */
    public String getName() {
        return this.name;
    }

    /**
     * Sets the name of the data model list..
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return a string representation of the data model list.
     */
    public String toString() {
        StringBuilder buf = new StringBuilder();
        buf.append("Data Model List <");
        for (Object aModelList : this.modelList) {
            buf.append(aModelList).append(", ");
        }
        buf.append(">");
        return buf.toString();
    }

    @Override
    public boolean isContinuous() {
        return false;
    }

    @Override
    public boolean isDiscrete() {
        return false;
    }

    @Override
    public boolean isMixed() {
        return false;
    }

    @Override
    public Node getVariable(String name) {
        return null;
    }

    @Override
    public DataModel copy() {
        return null;
    }

    public int hashCode() {
        int hashcode = 17;
        hashcode += 17 * this.name.hashCode();
        return hashcode;
    }

    public boolean equals(Object o) {
        if (o == null) {
            return false;
        }

        if (!(o instanceof DataModelList)) {
            return false;
        }

        DataModelList list = (DataModelList) o;

        return this.name.equals(list.name) && this.modelList.equals(list.modelList) && this.knowledge.equals(list.knowledge) && this.selectedModel.equals(list.selectedModel);

    }

    /**
     * Adds semantic checks to the default deserialization method. This method
     * must have the standard signature for a readObject method, and the body of
     * the method must begin with "s.defaultReadObject();". Other than that, any
     * semantic checks can be specified and do not need to stay the same from
     * version to version. A readObject method of this form may be added to any
     * class, even if Tetrad sessions were previously saved out using a version
     * of the class that didn't include it. (That's what the
     * "s.defaultReadObject();" is for. See J. Bloch, Effective Java, for help.
     */
    private void readObject(ObjectInputStream s)
            throws IOException, ClassNotFoundException {
        s.defaultReadObject();

        if (this.modelList == null) {
            throw new NullPointerException();
        }

        if (this.knowledge == null) {
            throw new NullPointerException();
        }
    }
}
