package co.dc.aptiv.utils;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.awt.event.*;
import java.util.Enumeration;

/**
 * @author lyy
 */
public class KeyboardUtil extends JPanel {

    private static final long serialVersionUID = 4529266044762990227L;

    private JPanel monthPanel;
    private boolean isShow = false;
    private Popup pop;

    private JComponent showDate;

    public boolean isAt = false;

    /**
     * 大小写标记 默认小写
     */
    private boolean flag = true;

    /**
     * 静态初始化
     *
     * @return
     */
    public static KeyboardUtil getInstance() {
        return new KeyboardUtil();
    }

    /**
     * new此可建立工具对象
     */
    public KeyboardUtil() {
        initPanel();
    }

    public KeyboardUtil(final JComponent showDate) {
        initPanel();
        register(showDate);
    }

    public KeyboardUtil(final JComponent showDate, boolean isat) {
        initPanel();
        register(showDate, isat);
    }

    /**
     * 初始化面板
     */
    private void initPanel() {
        monthPanel = new JPanel(new BorderLayout());
        /*
         * 统一所有子窗口设置字体样式
         */
        FontUIResource fontRes = new FontUIResource(new Font("微软雅黑", Font.PLAIN, 16));
        for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements(); ) {
            Object key = keys.nextElement();
            Object value = UIManager.get(key);
            if (value instanceof FontUIResource) {
                UIManager.put(key, fontRes);
            }
        }

        monthPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
        monthPanel.add(new JP3(), BorderLayout.CENTER);
    }

    /**
     * 绑定某控件1
     *
     * @param showDate
     */
    public void register(final JComponent showDate) {
        this.showDate = showDate;
        showDate.setRequestFocusEnabled(true);
        showDate.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent me) {
                showDate.requestFocusInWindow();
            }
        });
        this.add(showDate, BorderLayout.CENTER);
        this.setPreferredSize(new Dimension(90, 25));
        this.setBorder(BorderFactory.createLineBorder(Color.GRAY));
        showDate.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent me) {
                if (showDate.isEnabled()) {
                    showDate.setForeground(Color.BLACK);
                    if (isShow) {
                        hidePanel();
                    } else {
                        showPanel(showDate);
                    }
                }
            }
        });
    }

    /**
     * 绑定某控件2
     *
     * @param showDate
     */
    public void register(final JComponent showDate, boolean isat) {
        this.isAt = isat;
        this.showDate = showDate;
        showDate.setRequestFocusEnabled(true);
        showDate.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent me) {
                showDate.requestFocusInWindow();
            }
        });
        this.add(showDate, BorderLayout.CENTER);
        this.setPreferredSize(new Dimension(90, 25));
        this.setBorder(BorderFactory.createLineBorder(Color.GRAY));
        showDate.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent me) {
                if (showDate.isEnabled()) {
                    showDate.setForeground(Color.BLACK);
                    if (isShow) {
                        hidePanel();
                    } else {
                        showPanel(showDate);
                    }
                }
            }
        });
    }


    /**
     * 隐藏选择面板
     */
    public void hidePanel() {
        if (pop != null) {
            isShow = false;
            pop.hide();
            pop = null;
        }
    }

    /**
     * 显示选择面板
     *
     * @param owner
     */
    private void showPanel(Component owner) {
        if (pop != null) {
            pop.hide();
        }
        Point show = new Point(0, showDate.getHeight());
        SwingUtilities.convertPointToScreen(show, showDate);
        Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
        int x = show.x;
        int y = show.y;
        if (x < 0) {
            x = 0;
        }
        if (x > size.width - 600) {
            x = size.width - 600;
        }
        if (y < size.height - 340) {

        } else {
            y -= (340 + owner.getHeight());
        }

        pop = PopupFactory.getSharedInstance().getPopup(owner, monthPanel, x, y);
        pop.show();
        isShow = true;
    }

    private class JP3 extends JPanel implements ActionListener {
        private static final long serialVersionUID = 43157272447522985L;
        JButton button_1 = new JButton("1");
        JButton button_2 = new JButton("2");
        JButton button_3 = new JButton("3");
        JButton button_4 = new JButton("4");
        JButton button_5 = new JButton("5");
        JButton button_del = new JButton("删除(<--)");
        JButton button_6 = new JButton("6");
        JButton button_7 = new JButton("7");
        JButton button_8 = new JButton("8");
        JButton button_9 = new JButton("9");
        JButton button_0 = new JButton("0");
        JButton button_space = new JButton("空格");
        JButton button_clear = new JButton(flag ? "大写关" : "大写开");
        JButton button_a = new JButton(flag ? "a" : "A");
        JButton button_b = new JButton(flag ? "b" : "B");
        JButton button_c = new JButton(flag ? "c" : "C");
        JButton button_d = new JButton(flag ? "d" : "D");
        JButton button_e = new JButton(flag ? "e" : "E");
        JButton button_f = new JButton(flag ? "f" : "F");
        JButton button_g = new JButton(flag ? "g" : "G");
        JButton button_h = new JButton(flag ? "h" : "H");
        JButton button_i = new JButton(flag ? "i" : "I");
        JButton button_j = new JButton(flag ? "j" : "J");
        JButton button_k = new JButton(flag ? "k" : "K");
        JButton button_l = new JButton(flag ? "l" : "L");
        JButton button_m = new JButton(flag ? "m" : "M");
        JButton button_n = new JButton(flag ? "n" : "N");
        JButton button_o = new JButton(flag ? "o" : "O");
        JButton button_p = new JButton(flag ? "p" : "P");
        JButton button_q = new JButton(flag ? "q" : "Q");
        JButton button_r = new JButton(flag ? "r" : "R");
        JButton button_s = new JButton(flag ? "s" : "S");
        JButton button_t = new JButton(flag ? "t" : "T");
        JButton button_u = new JButton(flag ? "u" : "U");
        JButton button_v = new JButton(flag ? "v" : "V");
        JButton button_w = new JButton(flag ? "w" : "W");
        JButton button_x = new JButton(flag ? "x" : "X");
        JButton button_y = new JButton(flag ? "y" : "Y");
        JButton button_z = new JButton(flag ? "z" : "Z");
        JButton button_cancel = new JButton("O K");

        public JP3() {
            super();
            setLayout(null);
            this.setPreferredSize(new Dimension(600, 340));
            updateDate();
        }

        public void updateDate() {
            /*
             * 添加button+事件
             */
            button_1.setBounds(0, 0, 85, 56);
            button_1.addActionListener(this);
            button_2.setBounds(85, 0, 85, 56);
            button_2.addActionListener(this);
            button_3.setBounds(170, 0, 85, 56);
            button_3.addActionListener(this);
            button_4.setBounds(255, 0, 85, 56);
            button_4.addActionListener(this);
            button_5.setBounds(340, 0, 85, 56);
            button_5.addActionListener(this);
            button_del.setBounds(430, 0, 170, 56);
            button_del.addActionListener(this);
            button_6.setBounds(0, 56, 85, 56);
            button_6.addActionListener(this);
            button_7.setBounds(85, 56, 85, 56);
            button_7.addActionListener(this);
            button_8.setBounds(170, 56, 85, 56);
            button_8.addActionListener(this);
            button_9.setBounds(255, 56, 85, 56);
            button_9.addActionListener(this);
            button_0.setBounds(340, 56, 85, 56);
            button_0.addActionListener(this);
            button_space.setBounds(430, 56, 85, 56);
            button_space.addActionListener(this);
            button_clear.setBounds(515, 56, 85, 56);
            button_clear.addActionListener(this);
            button_a.setBounds(0, 116, 85, 56);
            button_a.addActionListener(this);
            button_b.setBounds(85, 116, 85, 56);
            button_b.addActionListener(this);
            button_c.setBounds(170, 116, 85, 56);
            button_c.addActionListener(this);
            button_d.setBounds(255, 116, 85, 56);
            button_d.addActionListener(this);
            button_e.setBounds(340, 116, 85, 56);
            button_e.addActionListener(this);
            button_f.setBounds(425, 116, 87, 56);
            button_f.addActionListener(this);
            button_g.setBounds(512, 116, 88, 56);
            button_g.addActionListener(this);
            button_h.setBounds(0, 172, 85, 56);
            button_h.addActionListener(this);
            button_i.setBounds(85, 172, 85, 56);
            button_i.addActionListener(this);
            button_j.setBounds(170, 172, 85, 56);
            button_j.addActionListener(this);
            button_k.setBounds(255, 172, 85, 56);
            button_k.addActionListener(this);
            button_l.setBounds(340, 172, 85, 56);
            button_l.addActionListener(this);
            button_m.setBounds(425, 172, 87, 56);
            button_m.addActionListener(this);
            button_n.setBounds(512, 172, 88, 56);
            button_n.addActionListener(this);
            button_o.setBounds(0, 228, 85, 56);
            button_o.addActionListener(this);
            button_p.setBounds(85, 228, 85, 56);
            button_p.addActionListener(this);
            button_q.setBounds(170, 228, 85, 56);
            button_q.addActionListener(this);
            button_r.setBounds(255, 228, 85, 56);
            button_r.addActionListener(this);
            button_s.setBounds(340, 228, 85, 56);
            button_s.addActionListener(this);
            button_t.setBounds(425, 228, 87, 56);
            button_t.addActionListener(this);
            button_u.setBounds(512, 228, 88, 56);
            button_u.addActionListener(this);
            button_v.setBounds(0, 284, 85, 56);
            button_v.addActionListener(this);
            button_w.setBounds(85, 284, 85, 56);
            button_w.addActionListener(this);
            button_x.setBounds(170, 284, 85, 56);
            button_x.addActionListener(this);
            button_y.setBounds(255, 284, 85, 56);
            button_y.addActionListener(this);
            button_z.setBounds(340, 284, 85, 56);
            button_z.addActionListener(this);
            button_cancel.setBounds(426, 285, 174, 55);
            button_cancel.setFont(new Font("微软雅黑", Font.BOLD, 20));
            button_cancel.addActionListener(this);

            add(button_1);
            add(button_2);
            add(button_3);
            add(button_4);
            add(button_5);
            add(button_del);
            add(button_6);
            add(button_7);
            add(button_8);
            add(button_9);
            add(button_0);
            add(button_space);
            add(button_clear);
            add(button_a);
            add(button_b);
            add(button_c);
            add(button_d);
            add(button_e);
            add(button_f);
            add(button_g);
            add(button_h);
            add(button_i);
            add(button_j);
            add(button_k);
            add(button_l);
            add(button_m);
            add(button_n);
            add(button_o);
            add(button_p);
            add(button_q);
            add(button_r);
            add(button_s);
            add(button_t);
            add(button_u);
            add(button_v);
            add(button_w);
            add(button_x);
            add(button_y);
            add(button_z);
            add(button_cancel);

        }

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == button_1) {
                uploadData("1");
            }
            if (e.getSource() == button_2) {
                uploadData("2");
            }
            if (e.getSource() == button_3) {
                uploadData("3");
            }
            if (e.getSource() == button_4) {
                uploadData("4");
            }
            if (e.getSource() == button_5) {
                uploadData("5");
            }
            if (e.getSource() == button_del) {
                delData();
            }
            if (e.getSource() == button_6) {
                uploadData("6");
            }
            if (e.getSource() == button_7) {
                uploadData("7");
            }
            if (e.getSource() == button_8) {
                uploadData("8");
            }
            if (e.getSource() == button_9) {
                uploadData("9");
            }
            if (e.getSource() == button_0) {
                uploadData("0");
            }
            if (e.getSource() == button_space) {
                uploadData(" ");
            }
            if (e.getSource() == button_clear) {
                /*clearData();*/
                flag = flag ? false : true;
                daxiaoxie();
            }

            if (e.getSource() == button_a) {
                uploadData(flag ? "a" : "A");
            }
            if (e.getSource() == button_b) {
                uploadData(flag ? "b" : "B");
            }
            if (e.getSource() == button_c) {
                uploadData(flag ? "c" : "C");
            }
            if (e.getSource() == button_d) {
                uploadData(flag ? "d" : "D");
            }
            if (e.getSource() == button_e) {
                uploadData(flag ? "e" : "E");
            }
            if (e.getSource() == button_f) {
                uploadData(flag ? "f" : "F");
            }
            if (e.getSource() == button_g) {
                uploadData(flag ? "g" : "G");
            }
            if (e.getSource() == button_h) {
                uploadData(flag ? "h" : "H");
            }
            if (e.getSource() == button_i) {
                uploadData(flag ? "i" : "I");
            }
            if (e.getSource() == button_j) {
                uploadData(flag ? "j" : "J");
            }
            if (e.getSource() == button_k) {
                uploadData(flag ? "k" : "K");
            }
            if (e.getSource() == button_l) {
                uploadData(flag ? "l" : "L");
            }
            if (e.getSource() == button_m) {
                uploadData(flag ? "m" : "M");
            }
            if (e.getSource() == button_n) {
                uploadData(flag ? "n" : "N");
            }
            if (e.getSource() == button_o) {
                uploadData(flag ? "o" : "O");
            }
            if (e.getSource() == button_p) {
                uploadData(flag ? "p" : "P");
            }
            if (e.getSource() == button_q) {
                uploadData(flag ? "q" : "Q");
            }
            if (e.getSource() == button_r) {
                uploadData(flag ? "r" : "R");
            }
            if (e.getSource() == button_s) {
                uploadData(flag ? "s" : "S");
            }
            if (e.getSource() == button_t) {
                uploadData(flag ? "t" : "T");
            }
            if (e.getSource() == button_u) {
                uploadData(flag ? "u" : "U");
            }
            if (e.getSource() == button_v) {
                uploadData(flag ? "v" : "V");
            }
            if (e.getSource() == button_w) {
                uploadData(flag ? "w" : "W");
            }
            if (e.getSource() == button_x) {
                uploadData(flag ? "x" : "X");
            }
            if (e.getSource() == button_y) {
                uploadData(flag ? "y" : "Y");
            }
            if (e.getSource() == button_z) {
                uploadData(flag ? "z" : "Z");
            }
            if (e.getSource() == button_cancel) {
                if (isAt) {
//					JTextField jt = (JTextField)showDate;
//					jt.setText(jt.getText()+KeyEvent.VK_ENTER);
//					jt.keyDown(null, KeyEvent.VK_ENTER);
                    //注册回车事件
                    try {
                        Robot r = new Robot();
                        r.keyPress(KeyEvent.VK_ENTER);
                    } catch (AWTException e1) {
                        e1.printStackTrace();
                    }
                }
                hidePanel();
            }
        }

        /**
         * 更新控件内容
         *
         * @param data
         */
        public void uploadData(String data) {
            if (showDate instanceof JTextField) {
                JTextField jt = (JTextField) showDate;
                jt.setText(jt.getText() + data);
            } else if (showDate instanceof JLabel) {
                JLabel jl = (JLabel) showDate;
                jl.setText(jl.getText() + data);
            }
        }

        /**
         * 清空控件内容
         */
        @SuppressWarnings("unused")
        public void clearData() {
            if (showDate instanceof JTextField) {
                ((JTextField) showDate).setText("");
            } else if (showDate instanceof JLabel) {
                ((JLabel) showDate).setText("");
            }
        }

        /**
         * 删除控件内容
         */
        public void delData() {
            if (showDate instanceof JTextField) {
                JTextField jt = (JTextField) showDate;
                if (jt.getText().length() > 0) {
                    jt.setText(jt.getText().substring(0, jt.getText().length() - 1));
                } else {
                    jt.setText("");
                }
            } else if (showDate instanceof JLabel) {
                JLabel jl = (JLabel) showDate;
                if (jl.getText().length() > 0) {
                    jl.setText(jl.getText().substring(0, jl.getText().length() - 1));
                } else {
                    jl.setText("");
                }
            }
        }

        /**
         * 设置大小写
         */
        public void daxiaoxie() {
            button_clear.setText(flag ? "大写关" : "大写开");
            button_a.setText(flag ? "a" : "A");
            button_b.setText(flag ? "b" : "B");
            button_c.setText(flag ? "c" : "C");
            button_d.setText(flag ? "d" : "D");
            button_e.setText(flag ? "e" : "E");
            button_f.setText(flag ? "f" : "F");
            button_g.setText(flag ? "g" : "G");
            button_h.setText(flag ? "h" : "H");
            button_i.setText(flag ? "i" : "I");
            button_j.setText(flag ? "j" : "J");
            button_k.setText(flag ? "k" : "K");
            button_l.setText(flag ? "l" : "L");
            button_m.setText(flag ? "m" : "M");
            button_n.setText(flag ? "n" : "N");
            button_o.setText(flag ? "o" : "O");
            button_p.setText(flag ? "p" : "P");
            button_q.setText(flag ? "q" : "Q");
            button_r.setText(flag ? "r" : "R");
            button_s.setText(flag ? "s" : "S");
            button_t.setText(flag ? "t" : "T");
            button_u.setText(flag ? "u" : "U");
            button_v.setText(flag ? "v" : "V");
            button_w.setText(flag ? "w" : "W");
            button_x.setText(flag ? "x" : "X");
            button_y.setText(flag ? "y" : "Y");
            button_z.setText(flag ? "z" : "Z");
            button_clear.updateUI();
            button_a.updateUI();
            button_b.updateUI();
            button_c.updateUI();
            button_d.updateUI();
            button_e.updateUI();
            button_f.updateUI();
            button_g.updateUI();
            button_h.updateUI();
            button_i.updateUI();
            button_j.updateUI();
            button_k.updateUI();
            button_l.updateUI();
            button_m.updateUI();
            button_n.updateUI();
            button_o.updateUI();
            button_p.updateUI();
            button_q.updateUI();
            button_r.updateUI();
            button_s.updateUI();
            button_t.updateUI();
            button_u.updateUI();
            button_v.updateUI();
            button_w.updateUI();
            button_x.updateUI();
            button_y.updateUI();
            button_z.updateUI();
        }
    }


    public static void main(String[] args) {
        /*
         * 以下三个步骤，顺序不可颠倒，初始化和注册的步骤必须紧跟着建立好的控件后面
         * 其他面板引用此插件的方式只需要以下三步即可
         */
        //第一步：建立输入框或者label
        JTextField showDate1 = new JTextField();
        //第二步：初始化插件
        KeyboardUtil dateChooser1 = KeyboardUtil.getInstance();
        //第三步：注册控件
        dateChooser1.register(showDate1, true);

        JFrame jf = new JFrame("测试键盘选择器");
        jf.add(showDate1, BorderLayout.NORTH);
        jf.pack();
        jf.setLocationRelativeTo(null);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}