package seng302.Controllers;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import seng302.RaceConnection;

import java.io.IOException;
import java.net.Socket;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * Created by cbt24 on 3/05/17.
 */
public class ConnectionController extends Controller {
    @FXML
    private AnchorPane connectionWrapper;
    @FXML
    private TableView connectionTable;
    @FXML
    private TableColumn<RaceConnection, String> hostnameColumn;
    @FXML
    private TableColumn<RaceConnection, String> statusColumn;
    @FXML
    private Button connectButton;

    @FXML
    private TextField urlField;
    @FXML
    private TextField portField;

    private ObservableList<RaceConnection> connections;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // TODO - replace with config file
        connections = FXCollections.observableArrayList();
        connections.add(new RaceConnection("livedata.americascup.com", 4941));
        connections.add(new RaceConnection("localhost", 4942));

        connectionTable.setItems(connections);
        hostnameColumn.setCellValueFactory(cellData -> cellData.getValue().hostnameProperty());
        statusColumn.setCellValueFactory(cellData -> cellData.getValue().statusProperty());

        connectionTable.getSelectionModel().selectedItemProperty().addListener((obs, prev, curr) -> {
            if (curr != null && ((RaceConnection)curr).check()) connectButton.setDisable(false);
            else connectButton.setDisable(true);
        });
        connectButton.setDisable(true);
    }

    /**
     * Sets current status of all connections.
     */
    public void checkConnections() {
        for(RaceConnection connection: connections) {
            connection.check();
        }
    }

    public AnchorPane startWrapper(){
        return connectionWrapper;
    }

    /**
     * Connects to host currently selected in table. Button enabled only if host is ready.
     */
    public void connectSocket() {
        try{
            RaceConnection connection = (RaceConnection)connectionTable.getSelectionModel().getSelectedItem();
            Socket socket = new Socket(connection.getHostname(), connection.getPort());
            connectionWrapper.setVisible(false);
            parent.enterLobby(socket);
        } catch (IOException e) { /* Never reached */ }
    }

    /**
     * adds a new connection
     */
    public void addConnection(){
        String hostName = urlField.getText();
        String portString = portField.getText();
        try{
            int port = Integer.parseInt(portString);
            connections.add(new RaceConnection(hostName, port));
        }catch(NumberFormatException e){
            System.err.println("Port number entered is not a number");
        }

    }
}
