/**
   * Shows/hides the security panel.
   *
   * @param isVisible <tt>true</tt> to show the security panel, <tt>false</tt> to hide it
   */
  public void setSecurityPanelVisible(final boolean isVisible) {
    if (!SwingUtilities.isEventDispatchThread()) {
      SwingUtilities.invokeLater(
          new Runnable() {
            public void run() {
              setSecurityPanelVisible(isVisible);
            }
          });
      return;
    }

    final JFrame callFrame = callRenderer.getCallContainer().getCallWindow().getFrame();

    final JPanel glassPane = (JPanel) callFrame.getGlassPane();

    if (!isVisible) {
      // Need to hide the security panel explicitly in order to keep the
      // fade effect.
      securityPanel.setVisible(false);
      glassPane.setVisible(false);
      glassPane.removeAll();
    } else {
      glassPane.setLayout(null);
      glassPane.addMouseListener(
          new MouseListener() {
            public void mouseClicked(MouseEvent e) {
              redispatchMouseEvent(glassPane, e);
            }

            public void mouseEntered(MouseEvent e) {
              redispatchMouseEvent(glassPane, e);
            }

            public void mouseExited(MouseEvent e) {
              redispatchMouseEvent(glassPane, e);
            }

            public void mousePressed(MouseEvent e) {
              redispatchMouseEvent(glassPane, e);
            }

            public void mouseReleased(MouseEvent e) {
              redispatchMouseEvent(glassPane, e);
            }
          });

      Point securityLabelPoint = securityStatusLabel.getLocation();

      Point newPoint =
          SwingUtilities.convertPoint(
              securityStatusLabel.getParent(),
              securityLabelPoint.x,
              securityLabelPoint.y,
              callFrame);

      securityPanel.setBeginPoint(new Point((int) newPoint.getX() + 15, 0));
      securityPanel.setBounds(0, (int) newPoint.getY() - 5, this.getWidth(), 130);

      glassPane.add(securityPanel);
      // Need to show the security panel explicitly in order to keep the
      // fade effect.
      securityPanel.setVisible(true);
      glassPane.setVisible(true);

      glassPane.addComponentListener(
          new ComponentAdapter() {
            /** Invoked when the component's size changes. */
            @Override
            public void componentResized(ComponentEvent e) {
              if (glassPane.isVisible()) {
                glassPane.setVisible(false);
                callFrame.removeComponentListener(this);
              }
            }
          });
    }
  }
  /** Instantiates a new game panel. */
  public GamePanel() {
    super();
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

    CARD_BACK.add(new CardPanel("img/cards/BACK.png"));

    // The code below is just for reference
    //        dealerCards = new ArrayList<>();
    //        for (int i = 0; i < dealerInHand.size(); i++)
    //        {
    //            dealerCards.add(new CardPanel("img/cards/" + dealerInHand.get(i) + ".png"));
    //        }
    //
    //        playerCardsOne = new ArrayList<>();
    //        for (int i = 0; i < playerInHandOne.size(); i++)
    //        {
    //            playerCardsOne.add(new CardPanel("img/cards/" + playerInHandOne.get(i) + ".png"));
    //        }
    //
    //        playerCardsTwo = new ArrayList<>();
    //        for (int i = 0; i < playerInHandTwo.size(); i++)
    //        {
    //            playerCardsTwo.add(new CardPanel("img/cards/" + playerInHandTwo.get(i) + ".png"));
    //        }
    // The code above is just for reference

    dealerDeckContainer = new CardDeckContainer();
    dealerStatContainer = new JPanel(new BorderLayout());
    dealerStatContainer.setOpaque(false);
    JLabel dealerStatTitle = new JLabel("Dealer in Hand");
    dealerStatTitle.setForeground(Color.WHITE);
    dealerStatTitle.setHorizontalAlignment(JLabel.CENTER);
    dealerStatTitle.setFont(new Font("", Font.PLAIN, 12));
    dealerStatPoint.setForeground(Color.WHITE);
    dealerStatPoint.setHorizontalAlignment(JLabel.CENTER);
    dealerStatPoint.setFont(new Font("", Font.PLAIN, 12));
    dealerStatContainer.add(dealerStatTitle, BorderLayout.NORTH);
    dealerStatContainer.add(dealerStatPoint, BorderLayout.CENTER);

    playerDeckOneContainer = new CardDeckContainer();
    playerStatOneContainer = new JPanel(new BorderLayout());
    playerStatOneContainer.setOpaque(false);
    JLabel playerStatOneTitle = new JLabel("Player in Hand");
    playerStatOneTitle.setForeground(Color.WHITE);
    playerStatOneTitle.setHorizontalAlignment(JLabel.CENTER);
    playerStatOneTitle.setFont(new Font("", Font.PLAIN, 12));
    playerStatOnePoint.setForeground(Color.WHITE);
    playerStatOnePoint.setHorizontalAlignment(JLabel.CENTER);
    playerStatOnePoint.setFont(new Font("", Font.PLAIN, 12));
    playerStatOneDescription.setForeground(Color.WHITE);
    playerStatOneDescription.setHorizontalAlignment(JLabel.CENTER);
    playerStatOneDescription.setFont(new Font("", Font.BOLD, 12));
    playerStatOneContainer.add(playerStatOneTitle, BorderLayout.NORTH);
    playerStatOneContainer.add(playerStatOnePoint, BorderLayout.CENTER);
    playerStatOneContainer.add(playerStatOneDescription, BorderLayout.SOUTH);

    playerDeckTwoContainer = new CardDeckContainer(new CardDeckPanel(CARD_BACK));
    playerStatTwoContainer = new JPanel(new BorderLayout());
    playerStatTwoContainer.setOpaque(false);
    JLabel playerStatTwoTitle = new JLabel("Player Hand 2");
    playerStatTwoTitle.setForeground(Color.WHITE);
    playerStatTwoTitle.setHorizontalAlignment(JLabel.CENTER);
    playerStatTwoTitle.setFont(new Font("", Font.PLAIN, 12));
    playerStatTwoPoint.setForeground(Color.WHITE);
    playerStatTwoPoint.setHorizontalAlignment(JLabel.CENTER);
    playerStatTwoPoint.setFont(new Font("", Font.PLAIN, 12));
    playerStatTwoDescription.setForeground(Color.WHITE);
    playerStatTwoDescription.setHorizontalAlignment(JLabel.CENTER);
    playerStatTwoDescription.setFont(new Font("", Font.BOLD, 12));
    playerStatTwoContainer.add(playerStatTwoTitle, BorderLayout.NORTH);
    playerStatTwoContainer.add(playerStatTwoPoint, BorderLayout.CENTER);
    playerStatTwoContainer.add(playerStatTwoDescription, BorderLayout.SOUTH);

    gameStatPanel = new JPanel();
    gameStatPanelPlayerName = new JLabel();
    gameStatPanelCurrentChips = new JLabel();
    gameStatPanelCurrentBet = new JLabel();
    gameStatPanelPlayerName.setFont(new Font("", Font.PLAIN, 14));
    gameStatPanelPlayerName.setForeground(Color.WHITE);
    gameStatPanelPlayerName.setBorder(new EmptyBorder(0, 0, 0, 5));
    gameStatPanelCurrentChips.setFont(new Font("", Font.PLAIN, 14));
    gameStatPanelCurrentChips.setForeground(Color.WHITE);
    gameStatPanelCurrentChips.setBorder(new EmptyBorder(0, 5, 0, 5));
    gameStatPanelCurrentBet.setFont(new Font("", Font.PLAIN, 14));
    gameStatPanelCurrentBet.setForeground(Color.WHITE);
    gameStatPanelCurrentBet.setBorder(new EmptyBorder(0, 5, 0, 0));
    gameStatPanel.add(gameStatPanelPlayerName);
    gameStatPanel.add(gameStatPanelCurrentChips);
    gameStatPanel.add(gameStatPanelCurrentBet);
    gameStatPanel.setOpaque(false);

    gameButtonPanel = new JPanel(cardLayout);
    betButtonPanel = new JPanel();
    playButtonPanel = new JPanel();
    JLabel pleaseBet = new JLabel("Please bet: ");
    pleaseBet.setFont(new Font("", Font.PLAIN, 14));
    pleaseBet.setForeground(Color.WHITE);
    betButtonPanel.add(pleaseBet);
    betField = new JTextField();
    betField.setFont(new Font("", Font.PLAIN, 14));
    betField.setPreferredSize(new Dimension(80, 28));
    betButtonPanel.add(betField);
    JButton betButton = new JButton("Bet");
    JButton backButton = new JButton("Back");
    betButtonPanel.add(betButton);
    betButtonPanel.add(backButton);
    betButtonPanel.setOpaque(false);

    hitButton = new JButton("Hit");
    standButton = new JButton("Stand");
    doubleButton = new JButton("Double");
    // JButton splitButton = new JButton("Split");
    // splitButton.setEnabled(false);
    playButtonPanel.add(hitButton);
    playButtonPanel.add(standButton);
    playButtonPanel.add(doubleButton);
    // playButtonPanel.add(splitButton);
    playButtonPanel.setOpaque(false);
    gameButtonPanel.add("betbutton", betButtonPanel);
    gameButtonPanel.add("playbutton", playButtonPanel);
    gameButtonPanel.setOpaque(false);

    add(gameStatPanel);
    add(dealerDeckContainer);
    add(playerDeckTwoContainer);
    add(playerDeckOneContainer);
    add(gameButtonPanel);

    this.addComponentListener(
        new ComponentAdapter() {
          @Override
          public void componentShown(ComponentEvent e) {
            Game.initGame();
          }
        });

    betButtonPanel.addComponentListener(
        new ComponentAdapter() {
          @Override
          public void componentShown(ComponentEvent e) {
            betField.setText("");
            if (BlackJack.player.getChip() <= 0) {
              JOptionPane.showMessageDialog(
                  null, "You are penniless!", "Information", JOptionPane.INFORMATION_MESSAGE);
              User.deleteUserByName(BlackJack.player.getName());
              BlackJack.player = new Player(true);
              BlackJack.dealer = new Player(false);
              BlackjackFrame.cardLayout.show(getParent(), "welcome");
            }
            hitButton.setEnabled(true);
            standButton.setEnabled(true);
            doubleButton.setEnabled(true);
            BlackJack.player.setBet(0);
            BlackJack.player.getHandOne().clear();
            BlackJack.player.getHandTwo().clear();
            BlackJack.dealer.getHandOne().clear();
            GamePanel.gameStatPanelPlayerName.setText("Player: " + BlackJack.player.getName());
            GamePanel.gameStatPanelCurrentChips.setText("Chips: " + BlackJack.player.getChip());
            GamePanel.gameStatPanelCurrentBet.setText("Bet: 0");
            GamePanel.gameStatPanel.repaint();
          }
        });

    betField.addKeyListener(
        new KeyAdapter() {
          @Override
          public void keyTyped(KeyEvent e) {
            int keyChar = e.getKeyChar();
            if (keyChar < KeyEvent.VK_0 || keyChar > KeyEvent.VK_9) {
              e.consume();
            }
          }
        });

    betButton.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            Game.bet(Integer.parseInt(betField.getText()));
          }
        });

    backButton.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            int choice =
                JOptionPane.showConfirmDialog(
                    null,
                    "Do you want to go back to main menu?\nYour record will be saved.",
                    "Go Back",
                    JOptionPane.YES_NO_OPTION);
            if (choice == JOptionPane.YES_OPTION) {
              User.updateUser();
              BlackJack.player = new Player(true);
              BlackJack.dealer = new Player(false);
              BlackjackFrame.cardLayout.show(getParent(), "welcome");
            }
          }
        });

    hitButton.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            doubleButton.setEnabled(false);
            Game.hit();
          }
        });

    standButton.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            hitButton.setEnabled(false);
            standButton.setEnabled(false);
            doubleButton.setEnabled(false);
            playerStatOneDescription.setText("Stand");
            playerStatOneDescription.repaint();
            Game.dealerGame();
          }
        });

    doubleButton.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            hitButton.setEnabled(false);
            standButton.setEnabled(false);
            doubleButton.setEnabled(false);
            if (!Game.doubleDown()) {
              hitButton.setEnabled(true);
              standButton.setEnabled(true);
              doubleButton.setEnabled(false);
            }
          }
        });
  }