package visualiser.Controllers;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import mock.app.Event;
import shared.exceptions.InvalidBoatDataException;
import shared.exceptions.InvalidRaceDataException;
import shared.exceptions.InvalidRegattaDataException;
import shared.exceptions.XMLReaderException;
import visualiser.model.RaceConnection;

import javax.xml.soap.Text;
import java.io.IOException;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ResourceBundle;

//TODO it appears this view/controller was replaced by Lobby.fxml. Remove?
/**
 * Controls the connection that the VIsualiser can connect to.
 */
public class ConnectionController extends Controller {
    @FXML
    private AnchorPane connectionWrapper;
    @FXML
    private TableView<RaceConnection> 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;


    /*Title Screen fxml items*/
    @FXML
    private Button hostGameTitleBtn;
    @FXML
    private Button connectGameBtn;
    @FXML
    private RadioButton nightRadioBtn;
    @FXML
    private RadioButton dayRadioButton;

    /*Lobby fxml items*/
    @FXML
    private TableView lobbyTable;
    @FXML
    private TableColumn gameNameColumn;
    @FXML
    private TableColumn hostNameColumn;
    @FXML
    private TableColumn playerCountColumn;
    @FXML
    private TextField playerNameField;
    @FXML
    private Button joinGameBtn;

    /*Host game fxml items*/
    @FXML
    private TextField gameNameField;
    @FXML
    private TextField hostNameField;
    @FXML
    private TextField hostGameBtn;








    private ObservableList<RaceConnection> connections;



    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // TODO - replace with config file
        connections = FXCollections.observableArrayList();

        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 && 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 = connectionTable.getSelectionModel().getSelectedItem();
            Socket socket = new Socket(connection.getHostname(), connection.getPort());
            socket.setKeepAlive(true);
            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, null));
        }catch(NumberFormatException e){
            System.err.println("Port number entered is not a number");
        }
    }


}
