package com.fr.design.gui.ibutton;

import com.fr.design.constants.UIConstants;
import com.fr.stable.Constants;
import com.fr.design.utils.gui.GUICoreUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Author : daisy
 * Date: 13-8-1
 * Time: 下午3:41
 */
public class UIPreviewButton extends JPanel {
	private static final int START_X = -7;
	protected UIButton upButton;
	protected UIButton downButton;

	protected void upButtonClickEvent() {

	}

	protected void downButtonClickEvent() {

	}

	public UIPreviewButton() {
		this(new UIButton(), new UIButton());
	}

	public UIPreviewButton(UIButton up, UIButton down) {
		upButton = up;
		downButton = down;

		upButton.setRoundBorder(true, Constants.CENTER);
		downButton.setRoundBorder(true, Constants.CENTER);

		upButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				downButton.getModel().setPressed(true);
				downButton.getModel().setSelected(true);
				downButton.repaint();
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				downButton.getModel().setPressed(false);
				downButton.getModel().setSelected(false);
				downButton.repaint();
			}

			@Override
			public void mouseClicked(MouseEvent e) {
				upButtonClickEvent();
			}
		});


		downButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				upButton.getModel().setPressed(true);
				upButton.getModel().setSelected(true);
				upButton.repaint();
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				upButton.getModel().setPressed(false);
				upButton.getModel().setSelected(false);
				upButton.repaint();
			}

			@Override
			public void mouseClicked(MouseEvent e) {
				downButtonClickEvent();
			}
		});
		this.setLayout(new FlowLayout(FlowLayout.CENTER,5,0));
		this.add(upButton);
		this.add(downButton);
	}


	public UIPreviewButton(Icon left, Icon right) {
		this();
		upButton.setIcon(left);
		downButton.setIcon(right);
	}

	public UIButton getUpButton() {
		return upButton;
	}

	public void setExtraPainted(boolean isExtraPainted) {
		if (!isExtraPainted) {
			upButton.setBackground(null);
			downButton.setBackground(null);
			upButton.setOpaque(false);
			downButton.setOpaque(false);
		}
	}

	public UIButton getDownButton() {
		return downButton;
	}

	public void set4Toolbar() {
		upButton.setNormalPainted(false);
		downButton.setNormalPainted(false);
		upButton.setBorderPaintedOnlyWhenPressed(true);
		downButton.setBorderPaintedOnlyWhenPressed(true);
	}

	protected void showPopWindow(JPopupMenu menu) {
		GUICoreUtils.showPopupMenu(menu, this, START_X, getY() + getHeight() - 3);
	}

	public static void main(String... args) {
		JFrame jf = new JFrame("test");
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel content = (JPanel) jf.getContentPane();
		content.setLayout(null);

		UIPreviewButton bb = new UIPreviewButton(UIConstants.PAGE_BIG_ICON, UIConstants.PREVIEW_DOWN);
		bb.setBounds(20, 20, bb.getPreferredSize().width, bb.getPreferredSize().height);
		content.add(bb);
		GUICoreUtils.centerWindow(jf);
		jf.setSize(100, 100);
		jf.setVisible(true);
	}
}