package highscores;
import credentials.Credentials;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
public class SubmitScorePane extends TitledPane{
private Label scoreLabel;
private TextField nameTf;
private Button submitButton;
private Button closeButton;
public SubmitScorePane(){
setText("Submit Score");
setContent(createBodyPane());
setCollapsible(false);
}
private VBox createBodyPane(){
submitButton= new Button("Submit Score");
submitButton.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent t) {
try{
submit();
setVisible(false);
}
catch(Exception e){
e.printStackTrace();
}
}
});
closeButton= new Button("Close");
closeButton.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent t) {
setVisible(false);
}
});
HBox hbox= new HBox();
hbox.setAlignment(Pos.CENTER_RIGHT);
hbox.setSpacing(10);
hbox.getChildren().addAll(submitButton, closeButton);
VBox vbox= new VBox();
vbox.setPadding(new Insets(10,10,10,10));
vbox.setSpacing(10);
vbox.getChildren().addAll(createGridPane(), hbox);
return vbox;
}
private GridPane createGridPane(){
GridPane gridpane= new GridPane();
gridpane.setHgap(10);
gridpane.setVgap(10);
scoreLabel= new Label();
nameTf= new TextField();
gridpane.add(new Label("Score"), 0, 0);
gridpane.add(scoreLabel, 1, 0);
gridpane.add(new Label("Name"), 0, 1);
gridpane.add(nameTf, 1, 1);
return gridpane;
}
private void submit() throws Exception{
String name= nameTf.getText();
String score= scoreLabel.getText();
String credentials= Credentials.getCredentials(name, score);
submit(name, score, credentials);
}
private static void submit(String name, String score, String credentials) throws Exception{
String host= "http://www.barvaz.net/";
String urlString= host+"submitscore.php";
String query= "name="+name+"&score="+score+"&credentials="+credentials;
String fullUrlString= urlString+"?"+query;
System.out.println(fullUrlString);
URL url = new URL(fullUrlString);
URLConnection connection = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String answer;
while ((answer = in.readLine()) != null) {
System.out.println(answer);
}
in.close();
}
public void setScore(String score){
scoreLabel.setText(score);
}
public TextField getNameTf(){
return nameTf;
}
}