package gui.panels.edit;

import agenda.Act;
import agenda.Agenda;
import gui.panels.edit.dialogs.ActDialogPanel;

import javax.swing.*;

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;
import java.util.Calendar;

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

    private JList actsList;
    private final Agenda agenda;
    private DefaultListModel model;
    
    private JPanel detailsPanel;
    private JLabel title;

    public ActsPane(Agenda agenda, JPanel panel){
    	title = new JLabel("Acts");
    	title.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 20));
    	title.setFont(new Font("Arial", Font.PLAIN, 16));
    	title.setForeground(new Color(51,51,51));
    	this.setOpaque(false);
        this.agenda = agenda;
        this.model = new DefaultListModel();
        this.detailsPanel = panel;
        super.setLayout(new BorderLayout());
        super.setBackground(new Color(242,241,237));
        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);

        for(Act act: this.agenda.getActs()){
            this.model.addElement(act);
        }

        this.actsList = new JList();
        this.actsList.setModel(this.model);

        //the cell renderer.
        this.actsList.setCellRenderer(new ActCellRenderer());

        //the JList inside a scrollPane.
        JScrollPane scrollPane = new JScrollPane(this.actsList);
        
        //double click
        this.actsList.addMouseListener(new DoubleClickAct(this.actsList, this));

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

    private JPanel buttonRow(){
        JPanel buttonPane = new JPanel();
        buttonPane.setBorder(BorderFactory.createEmptyBorder(20, 10, 0, 20));
        buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
        buttonPane.setOpaque(false);
        this.setBackground(Color.WHITE);
        //add button
        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 ActDialogPanel(agenda, model);
                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);
        
        //remove button
        JLabel removeButton = new JLabel();
        removeButton.setIcon(new ImageIcon(getClass().getResource("/remove.png")));
        removeButton.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent e) {
				Object object = model.remove(actsList.getSelectedIndex());
                agenda.removeAct((Act) object);
                
                System.out.println(model.size());
                System.out.println(agenda.getActs().size());
			}
			@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(removeButton);

        return buttonPane;
    }
    
    public void editDialog(){
        JDialog dialog = new ActDialogPanel(ActsPane.this.agenda, model, (Act) actsList.getSelectedValue());
        dialog.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 ActCellRenderer extends JLabel implements ListCellRenderer {

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

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

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

class DoubleClickAct extends MouseAdapter{
    protected JList list;
    private ActsPane pane;

    public DoubleClickAct(JList l, ActsPane 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();
        }
    }
}
