/*
 * Copyright (c) 2007-2013 The Broad Institute, Inc.
 * SOFTWARE COPYRIGHT NOTICE
 * This software and its documentation are the copyright of the Broad Institute, Inc. All rights are reserved.
 *
 * This software is supplied without any warranty or guaranteed support whatsoever. The Broad Institute is not responsible for its use, misuse, or functionality.
 *
 * This software is licensed under the terms of the GNU Lesser General Public License (LGPL),
 * Version 2.1 which is available at http://www.opensource.org/licenses/lgpl-2.1.php.
 */

package org.broad.igv.lists;

import org.broad.igv.feature.Locus;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author jrobinso
 * @date Sep 26, 2010
 */
public class GeneList {

    private String group;
    private boolean editable = true;
    private String name;
    private String description;
    private List<String> loci;
    private boolean showName = true;
    private static Comparator<String> POSITION_COMPARATOR;

    public GeneList(String name, String description, String group, List<String> loci) {
        this.group = group;
        this.description = description;
        this.name = name;
        //We do this to guarantee that certain operations will be supported
        this.loci = loci;
    }

    public GeneList(String name, List<String> loci) {
        this(name, null, GeneListManager.USER_GROUP, loci);
    }

    public GeneList(String name, List<String> loci, boolean showName) {
        this(name, null, GeneListManager.USER_GROUP, loci);
        this.showName = showName;
    }

    public GeneList() {
        this.group = GeneListManager.USER_GROUP;
    }

    public String getName() {
        return name;
    }


    public String getDisplayName() {
        return showName ? name : "";
    }

    public List<String> getLoci() {
        return loci;
    }

    public int size() {
        return loci == null ? 0 : loci.size();
    }

    public void add(String gene) {
        if (loci == null) {
            loci = new ArrayList<String>(1);
        }
        try {
            //Can't guarantee that list will support this operation
            loci.add(gene);
        } catch (Exception e) {
            loci = new ArrayList<String>(loci);
            loci.add(gene);
        }
    }

    public GeneList copy() {
        return new GeneList(name + " copy", loci);
    }

    public void setLoci(List<String> strings) {
        loci = strings;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isEditable() {
        return editable;
    }

    public String getGroup() {

        return group;
    }

    public void setEditable(boolean editable) {
        this.editable = editable;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }


    /**
     * Sort loci by "position".  This only sorts loci of the form chr1:100-200.
     */
    public static void sortByPosition(List<String> loci) {
        if (POSITION_COMPARATOR == null) initComparator();

        Collections.sort(loci, POSITION_COMPARATOR);
    }


    private static synchronized void initComparator() {
        POSITION_COMPARATOR = new Comparator<String>() {
            public int compare(String s1, String s2) {
                Locus l1 = Locus.fromString(s1);
                Locus l2 = Locus.fromString(s2);
                boolean l1Valid = l1 != null;
                boolean l2Valid = l2 != null;
                if (l1Valid && l2Valid) {
                    return 0;
                } else if (!l1Valid) {
                    return -1;
                } else if (!l2Valid) {
                    return 1;
                } else if (!l1.getChr().equals(l2.getChr())) {
                    return l1.getChr().compareTo(l2.getChr());
                } else {
                    return l1.getStart() - l2.getStart();
                }
            }

        };
    }

}
