package uk.co.pentest.psychoPATH;

import burp.ITab;
import java.awt.Color;
import java.util.Objects;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;

/**
 * The tab that is registered with Burp. The content is provided by psychoPanel.
 */
public class PsychoTab implements ITab {
    //public PsychoPanel psychoPanel = new PsychoPanel();
     public PsychoPanel2 psychoPanel = new PsychoPanel2();
    Integer tabIndex;
    JTabbedPane tabPane;
    
    /**
     * Find our tab index within Burp's JTabbedPane. Setup an activation
     * listener that resets the title colour to black.
     */
    public void findTab() {
        if(tabIndex != null)
            return;
        tabPane = (JTabbedPane) psychoPanel.getParent();
        if(tabPane == null)
            return;
        for(int i = 0; i < tabPane.getTabCount(); i++)
            if(Objects.equals(tabPane.getTitleAt(i), getTabCaption()))
                tabIndex = i;
        tabPane.addChangeListener((ChangeEvent e1) -> {
            if(tabPane.getSelectedIndex() == tabIndex)
                tabPane.setBackgroundAt(tabIndex, Color.BLACK);
        });
    }

    /**
     * Change colour of the tab handle to show it needs attention. The colour
     * matches the colour that Burp itself uses.
     */
    void alertTab() {
        tabPane.setBackgroundAt(tabIndex, new Color((float) 0.894, (float) 0.535, (float) 0.0));
    }

    @Override
    public String getTabCaption() {
        return "psychoPATH";
    }

    @Override
    //public PsychoPanel getUiComponent() {        
        public PsychoPanel2 getUiComponent() {        
        return psychoPanel;       
    }
    
}
