package edu.ucsb.cs56.projects.games.minesweeper;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Map;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import javax.swing.JToggleButton;
import javafx.scene.control.ToggleGroup;
/**
 * Created by ryanwiener on 11/15/17.
 */

public class LeaderboardFrame extends JFrame {

    private JButton backBtn;
    private JLabel title;
    private JTextArea highScoreList;
    private JScrollPane scroller;
    private JRadioButton easyBtn;
    private JRadioButton mediumBtn;
    private JRadioButton hardBtn;
    final ButtonGroup group = new ButtonGroup();

    public LeaderboardFrame() {
        super();
        setSize(650, 600);
        Container menu = getContentPane();
        menu.setLayout(new BoxLayout(menu, BoxLayout.Y_AXIS));

        backBtn = new JButton("Back");
        backBtn.setAlignmentX(Component.CENTER_ALIGNMENT);
        backBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });

        title = new JLabel("Leaderboard");
        title.setFont(new Font("Serif", Font.PLAIN, 20));
        title.setAlignmentX(Component.CENTER_ALIGNMENT);

        JPanel togglePanel = new JPanel(new FlowLayout());

        JRadioButton easyBtn = new JRadioButton("Easy");
        group.add(easyBtn);
        easyBtn.setSelected(true);
        JRadioButton mediumBtn = new JRadioButton("Medium");
        group.add(mediumBtn);
        JRadioButton hardBtn = new JRadioButton("Hard");
        group.add(hardBtn);

        ItemListener itemListener = new ItemListener() {
            String lastSelected;
            public void itemStateChanged(ItemEvent itemEvent) {
                AbstractButton aButton = (AbstractButton)itemEvent.getSource();
                int state = itemEvent.getStateChange();
                String label = aButton.getText();
                String msgStart;
                if (state == ItemEvent.SELECTED) {
                    if (label.equals("Easy")) {
                        highScoreList.setText(getHighScores(1));
                    }
                    else if (label.equals("Medium")) {
                        highScoreList.setText(getHighScores(2));
                    }
                    else {
                        highScoreList.setText(getHighScores(3));
                    }
                }
            }
        };

        easyBtn.addItemListener(itemListener);
        mediumBtn.addItemListener(itemListener);
        hardBtn.addItemListener(itemListener);

        highScoreList = new JTextArea(getHighScores(1));
        highScoreList.setEditable(false);
        scroller = new JScrollPane(highScoreList);
        scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);



        menu.add(backBtn);
        menu.add(title);
        menu.add(togglePanel);
        togglePanel.add(easyBtn);
        togglePanel.add(mediumBtn);
        togglePanel.add(hardBtn);
        menu.add(highScoreList);
        menu.add(scroller);
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    }

    public String getHighScores(int difficulty) {
        ArrayList<Map<String, String>> highScores;
        if(difficulty == 1){
            highScores = DBConnector.getTopTenEasy();
        }
        else if(difficulty == 2){
            highScores = DBConnector.getTopTenMedium();
        }
        else { //hard difficulty
            highScores = DBConnector.getTopTenHard();
        }
        String display = "";
        for (Map<String, String> row : highScores) {
            display += row.get("place") + "    ";
            display += row.get("name") + "    ";
            display += row.get("score") + "    ";
            display += row.get("attime") + "    ";
            display += '\n';
        }
        return  display;
    }


    public int getBackX() {
        return backBtn.getX();
    }

    public int getBackY() {
        return backBtn.getY();
    }
}

@@ -44,16 +44,43 @@
 eeper;%0A%0A
+import javax.swing.JTable;%0A
 import j
@@ -644,24 +644,59 @@
 on hardBtn;%0A
+    private JTable highScoreTable;%0A
     final Bu
@@ -1702,24 +1702,246 @@
 (hardBtn);%0A%0A
+        String%5B%5D columnNames = %7B%22Place%22, %22Name%22, %22Score%22, %22At Time%22%7D;%0A        Object rowData%5B%5D%5B%5D = getHighScores(1);%0A        highScoreTable = new JTable(rowData, columnNames);%0A        tableModel = new DefaultTableModel();%0A
         Item
@@ -2416,32 +2416,34 @@
                 
+//
 highScoreList.se
@@ -2560,32 +2560,34 @@
                 
+//
 highScoreList.se
@@ -2676,32 +2676,34 @@
                 
+//
 highScoreList.se
@@ -2937,24 +2937,26 @@
 ener);%0A%0A
+%0A%0A
         
 highScor
@@ -2939,32 +2939,34 @@
 er);%0A%0A%0A%0A        
+//
 highScoreList = 
@@ -2998,32 +2998,34 @@
 es(1));%0A        
+//
 highScoreList.se
@@ -3078,36 +3078,37 @@
 llPane(highScore
-List
+Table
 );%0A        scrol
@@ -3498,20 +3498,21 @@
 ighScore
-List
+Table
 );%0A     
@@ -3608,22 +3608,26 @@
  public 
-String
+Object%5B%5D%5B%5D
  getHigh
@@ -3984,32 +3984,381 @@
 rd();%0A        %7D%0A
+        Object %5B%5D%5B%5D data = new Object%5B4%5D%5B1%5D;%0A%0A        int i = 1;%0A        for (Map%3CString, String%3E row : highScores) %7B%0A            data%5Bi%5D%5B0%5D = row.get(%22place%22);%0A            data%5Bi%5D%5B1%5D = row.get(%22name%22);%0A            data%5Bi%5D%5B2%5D = row.get(%22score%22);%0A            data%5Bi%5D%5B3%5D = row.get(%22attime%22);%0A            i++;%0A        %7D%0A        return data;%0A        /*%0A
         String d
@@ -4686,16 +4686,18 @@
 display;
+*/
 %0A    %7D%0A%0A

Change JTextArea to JTable on Leaderboard frame