package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import listeners.OpenXMLFileButtonListener;
import repository.Project;

public class MainGUI extends JFrame{	
	JPanel gridButtonBar = new JPanel(new GridLayout(1, 3, 5, 0));
	JPanel TopButtonBar = new JPanel(new FlowLayout(FlowLayout.LEFT));
	ListGUI list;
	ActionListener OpenXMLFileButtonListener;
	JButton openXMLJButton;
	JButton saveXMLJButton;
	
	public static JTabbedPane editorPane;
	DrawboxEditor drawBoxPanel;
	HitboxEditor hitboxPanel;


	public MainGUI() {
		
		setTitle("Hitbox/Drawbox Editor");
		setSize(1000,650);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		setMinimumSize(new Dimension(800,600));
		list = new ListGUI(); 
		add(list,BorderLayout.WEST);
		list.setVisible(true);
        
		drawBoxPanel = new DrawboxEditor(list);
		hitboxPanel = new HitboxEditor(list);
		list.registerJListListener(drawBoxPanel); 
		list.registerJListListener(hitboxPanel);
		
        OpenXMLFileButtonListener = new OpenXMLFileButtonListener(list);
		
		openXMLJButton = createButton("XML", OpenXMLFileButtonListener,"res/xml.png");
		saveXMLJButton = createButton("Save",(e)-> Project.getInstance().writeXML(),"res/download.png");
		
		gridButtonBar.add(openXMLJButton);
		gridButtonBar.add(saveXMLJButton);
		//gridButtonBar.add(clearLinesJButton);
		TopButtonBar.add(gridButtonBar);
		TopButtonBar.setBorder(BorderFactory.createLoweredBevelBorder());
		add(TopButtonBar,BorderLayout.NORTH);

		editorPane = new JTabbedPane();
		editorPane.setVisible(true);
		add(editorPane,BorderLayout.CENTER);		
		
        editorPane.addTab("Drawbox", drawBoxPanel);
        editorPane.addTab("Hitbox", hitboxPanel);
        repaint();
       
	}
	@Override
	public void paint(Graphics g){
       super.paint(g);
    }
	
	private JButton createButton(String text,ActionListener listener,String pathImage) {
		JButton button = new JButton(new ImageIcon(pathImage));
		button.addActionListener(listener); 
		button.setContentAreaFilled(false);
		button.setFocusPainted(false);
		button.setPreferredSize(new Dimension(72, 38));
		return button;
	}
}
