package org.broad.igv.track;

import com.google.gson.Gson;
import org.broad.igv.data.DataTile;
import org.broad.igv.event.DataLoadedEvent;
import org.broad.igv.event.IGVEventBus;
import org.broad.igv.feature.PSLRecord;
import org.broad.igv.feature.genome.Genome;
import org.broad.igv.feature.genome.GenomeManager;
import org.broad.igv.feature.tribble.PSLCodec;
import org.broad.igv.prefs.Constants;
import org.broad.igv.prefs.PreferencesManager;
import org.broad.igv.renderer.BarChartRenderer;
import org.broad.igv.renderer.IGVFeatureRenderer;
import org.broad.igv.renderer.SpliceJunctionRenderer;
import org.broad.igv.session.IGVSessionReader;
import org.broad.igv.session.SubtlyImportant;
import org.broad.igv.ui.IGV;
import org.broad.igv.ui.color.ColorUtilities;
import org.broad.igv.ui.panel.IGVPopupMenu;
import org.broad.igv.ui.util.MessageUtils;
import org.broad.igv.util.blat.BlatClient;
import org.broad.igv.util.blat.BlatQueryWindow;
import org.w3c.dom.Element;
import sun.plugin2.util.ColorUtil;

import javax.swing.*;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.*;
import java.util.List;

@XmlType(factoryMethod = "getNextTrack")

public class BlatTrack extends FeatureTrack {


    protected Class clazz = BlatTrack.class;

    String species;

    String db;

    String sequence;

    Genome genome;

    List<PSLRecord> features;

    public BlatTrack() {
        super("", "");
    }

    public BlatTrack(String species, String sequence, String db, Genome genome) throws IOException {

        super(sequence, "Blat");

        this.sequence = sequence;
        this.db = db;
        this.species = species;
        this.genome = genome;

        setUseScore(true);
        setDisplayMode(Track.DisplayMode.SQUISHED);

        init();
    }

    private void init() throws IOException {

        List<String> tokensList = BlatClient.blat(species, db, sequence);

        // Convert tokens to features
        PSLCodec codec = new PSLCodec(genome, true);

        features = new ArrayList<PSLRecord>(tokensList.size());
        for (String tokens : tokensList) {
            PSLRecord f = codec.decode(tokens);
            if (f != null) {
                features.add(f);
            }
        }

        if (features.isEmpty()) {
            MessageUtils.showMessage("No features found");
        }

        this.source = new FeatureCollectionSource(features, genome);
        ;

        this.renderer = new IGVFeatureRenderer();

        IGVEventBus.getInstance().subscribe(DataLoadedEvent.class, this);
    }

    private void openTableView() {

        BlatQueryWindow win = new BlatQueryWindow(IGV.getMainFrame(), sequence, features);
        win.setVisible(true);
    }

    public List<PSLRecord> getFeatures() {
        return features;
    }


    public IGVPopupMenu getPopupMenu(final TrackClickEvent te) {
        IGVPopupMenu menu = TrackMenuUtils.getPopupMenu(Arrays.asList(this), "Menu", te);

        JMenuItem item = new JMenuItem("Open table view");
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                openTableView();
            }
        });

        menu.addSeparator();
        menu.add(item);

        return menu;
    }


    public Map<String, String> getPersistentState() {

        Map<String, String> state = new LinkedHashMap<String, String>();

        state.put("name", getName());
        state.put("db", db);
        state.put("sequence", sequence);
        state.put("species", species);
        state.put("displayMode", getDisplayMode().toString());

        Color c = getColor();
        if (c != null) {
            String cs = ColorUtilities.colorToString(c);
            state.put("color", cs);
        }

        return state;
    }


    public BlatTrack(Element element) throws IOException {

        super(element);

        String sequence = element.getAttribute("sequence");
        String species = element.getAttribute("species");
        String db = element.getAttribute("db");
        Genome genome = GenomeManager.getInstance().getCurrentGenome();

        this.id = sequence;
        this.sequence = sequence;
        this.db = db;
        this.species = species;
        this.genome = genome;

        setUseScore(true);

        init();


    }

}

@@ -27,73 +27,8 @@
 k;%0A%0A
-import com.google.gson.Gson;%0Aimport org.broad.igv.data.DataTile;%0A
 impo
@@ -314,315 +314,35 @@
 igv.
-prefs.Constants;%0Aimport org.broad.igv.prefs.PreferencesManager;%0Aimport org.broad.igv.renderer.BarChartRenderer;%0Aimport org.broad.igv.renderer.IGVFeatureRenderer;%0Aimport org.broad.igv.renderer.SpliceJunctionRenderer;%0Aimport org.broad.igv.session.IGVSessionReader;%0Aimport org.broad.igv.session.SubtlyImportant
+renderer.IGVFeatureRenderer
 ;%0Aim
@@ -624,111 +624,29 @@
 nt;%0A
-import sun.plugin2.util.ColorUtil;%0A%0Aimport javax.swing.*;%0Aimport javax.xml.bind.annotation.XmlAttribute
+%0Aimport javax.swing.*
 ;%0Aim
@@ -853,49 +853,8 @@
 t;%0A%0A
-@XmlType(factoryMethod = %22getNextTrack%22)%0A
 %0Apub
@@ -1058,63 +1058,8 @@
 s;%0A%0A
-    public BlatTrack() %7B%0A        super(%22%22, %22%22);%0A    %7D%0A%0A
     

fix import bug