package gui.panels.edit;

import agenda.Agenda;
import agenda.Artist;
import agenda.Stage;
import gui.panels.edit.dialogs.StageDialogPanel;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * Created by gjoosen on 19/02/15.
 */
public class StagesPane extends JPanel {

    private JList stagesList;
    private final Agenda agenda;
    private DefaultListModel model;
    
    private JPanel detailsPanel;
    private JLabel title;
    
    public StagesPane(Agenda agenda, JPanel detailsPanel){
    	title = new JLabel("Stages");
    	title.setFont(new Font("Arial", Font.PLAIN, 16));
    	title.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 20));
    	title.setForeground(new Color(51,51,51));
    	this.setOpaque(false);
        this.setBackground(Color.WHITE);
        this.agenda = agenda;
        this.detailsPanel = detailsPanel;
        super.setBackground(new Color(242,241,237));
        super.setLayout(new BorderLayout());
        super.add(title, BorderLayout.NORTH);
        JPanel randomPane = new JPanel();
        randomPane.setOpaque(false);
        super.add(randomPane, BorderLayout.EAST);
        super.add(randomPane, BorderLayout.WEST);
        super.add(this.buttonRow(), BorderLayout.SOUTH);

        //the JList needs an array, so you have to go from the List to an array.

        this.model = new DefaultListModel();

        for(Stage stage: this.agenda.getStages()){
            this.model.addElement(stage);
        }
        
        this.stagesList = new JList();
        this.stagesList.setModel(this.model);

        //the cell renderer.
        this.stagesList.setCellRenderer(new StageCellRenderer());
        
        //double click
        this.stagesList.addMouseListener(new DoubleClickStage(this.stagesList, this));
        
        //the JList inside a scrollPane.
        JScrollPane scrollPane = new JScrollPane(this.stagesList);

        super.add(scrollPane, BorderLayout.CENTER);
    }

    private JPanel buttonRow(){
        JPanel buttonPane = new JPanel();
        buttonPane.setBorder(BorderFactory.createEmptyBorder(20, 10, 0, 20));
        buttonPane.setOpaque(false);
        buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
        
        JLabel addButton = new JLabel();
        addButton.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20));
        addButton.setIcon(new ImageIcon(getClass().getResource("/add.png")));
        addButton.setForeground(new Color(51,51,51));
        addButton.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent e) {
				JDialog dialog = new StageDialogPanel(agenda, model);
                dialog.setLocationRelativeTo(null);
                dialog.pack();
                dialog.setLocation(getCenterOfScreen(dialog));
                dialog.setVisible(true);
			}
			@Override
			public void mouseEntered(MouseEvent arg0) {
				
			}
			@Override
			public void mouseExited(MouseEvent arg0) {
				
			}
			@Override
			public void mousePressed(MouseEvent arg0) {
				
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {

			}
		});
        buttonPane.add(addButton);
        
        JLabel remove = new JLabel();
        remove.setIcon(new ImageIcon(getClass().getResource("/remove.png")));
        remove.setForeground(new Color(51,51,51));
        remove.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent e) {
				Object object = model.remove(stagesList.getSelectedIndex());
                agenda.removeStage(object);
                
                System.out.println(model);
                System.out.println(agenda.getStages());
			}
			@Override
			public void mouseEntered(MouseEvent arg0) {
				
			}
			@Override
			public void mouseExited(MouseEvent arg0) {
				
			}
			@Override
			public void mousePressed(MouseEvent arg0) {
				
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {

			}
		});        
        buttonPane.add(remove);
        return buttonPane;
    }
    
    public void editDialog(){
        JDialog dialog = new StageDialogPanel(StagesPane.this.agenda, model, (Stage) stagesList.getSelectedValue());
        dialog.getContentPane().setBackground(Color.WHITE);
        dialog.setLocation(getCenterOfScreen(dialog));
        dialog.setLocationRelativeTo(null);
        dialog.pack();
        dialog.setVisible(true);
    }

    private Point getCenterOfScreen(JDialog dialog){
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension screenSize = toolkit.getScreenSize();

        int x = (int) ((screenSize.getWidth() - dialog.getWidth()) / 2);
        int y = (int) ((screenSize.getHeight() - dialog.getHeight()) / 2);

        Point center = new Point(x, y);
        System.out.println(center.getX() + " - " + center.getY());
        return center;
    }
}

class StageCellRenderer extends JLabel implements ListCellRenderer {

    private static final Color HIGHLIGHT_COLOR = new Color(0, 0, 128);

    public StageCellRenderer() {
        setOpaque(true);
        setIconTextGap(12);
    }

    public Component getListCellRendererComponent(JList list, Object value,
                                                  int index, boolean isSelected, boolean cellHasFocus) {
        Stage stage = (Stage) value;
        setText(stage.getName());
        if (isSelected) {
            setBackground(HIGHLIGHT_COLOR);
            setForeground(Color.white);
        } else {
            setBackground(Color.white);
            setForeground(Color.black);
        }
          return this;
    }   
}

class DoubleClickStage extends MouseAdapter {
    protected JList list;
    private StagesPane pane;

    public DoubleClickStage(JList l, StagesPane pane){
        this.pane = pane;
        list = l;
    }

    public void mouseClicked(MouseEvent e){
        if(e.getClickCount() == 2){
            int index = list.locationToIndex(e.getPoint());
            ListModel dlm = list.getModel();
            Object item = dlm.getElementAt(index);;
            list.ensureIndexIsVisible(index);
            System.out.println("Double clicked on " + item);
            this.pane.editDialog();
        }
    }
}
