package useful;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.plaf.basic.BasicButtonUI;

import main.ChronoWindow;
import main.Main;


public class TabButton  extends JButton implements MouseListener {
	
	private static final long serialVersionUID = 1L;
	private ButtonTabComponent comp;
	
    public TabButton(ButtonTabComponent comp) {
        int size = 17;
        this.comp=comp;
        setPreferredSize(new Dimension(size, size));
        //Make the button looks the same for all Laf's
        setUI(new BasicButtonUI());
        //Make it transparent
        setContentAreaFilled(false);
        //No need to be focusable
        setFocusable(false);
        setBorder(BorderFactory.createEtchedBorder());
        setBorderPainted(false);
        //Making nice rollover effect
        //we use the same listener for all buttons
        addMouseListener(this);
        setRolloverEnabled(true);
    }
 
    //paint the cross
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g.create();
        //shift the image for pressed buttons
        if (getModel().isPressed()) {
            g2.translate(1, 1);
        }
        g2.setStroke(new BasicStroke(2));
        g2.setColor(new Color(50,50,50));
        if (getModel().isRollover()) {
            g2.setColor(Color.RED);
        }
        int delta = 6;
        g2.drawLine(delta, delta, getWidth() - delta, getHeight() - delta);
        g2.drawLine(getWidth() - delta, delta, delta, getHeight() - delta);
        g2.dispose();
    }

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		int i = ChronoWindow.tabs.indexOfTabComponent(comp); 
        if (i != -1) {
        	if (ChronoWindow.chrono.get(i).modifToSave){
        		int option = JOptionPane.showConfirmDialog(null, "Save chronogram before closing ?", "Closing file", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
        		if(option != JOptionPane.CANCEL_OPTION && option != JOptionPane.CLOSED_OPTION){
        			if (option == JOptionPane.OK_OPTION){
        				Main.window.save(false, i);
        			}
        			close(i);
        		}
        		
        	}
        	else{
        		close(i);
        	}
        }
	}
	
	public void close(int i){
		ChronoWindow.tabs.remove(i);
    	ChronoWindow.chrono.remove(i);
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
