package com.insa.app.controller;

import com.insa.app.model.User;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;

import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;

public class GuiController {

    @FXML
    private TextField input_text;

    @FXML
    private Button button_send;

    @FXML
    private ListView<User> list_users;

    @FXML
    private ListView<String> list_message;

    @FXML
    private Label user_name;

    private Controller controller;

    public void setUsers(ObservableList<User> users) {
        list_users.setItems(users);
    }

    public void setConversation(ObservableList<String> messages) {
        list_message.setItems(messages);
    }

    public User getFocusedRecipient() {
        return list_users.getFocusModel().getFocusedItem();
    }

    public void setup(Controller ctrl) {
        controller = ctrl;
        input_text.setEditable(false);
        button_send.setDisable(true);
    }

    @FXML
    void onMessageEnter(ActionEvent a) {
        sendMessage();
    }

    public void sendMessage() {
        String mess = input_text.getText();
        input_text.clear();
        User recipient = getFocusedRecipient();
        controller.sendMessage(recipient,mess);
    }

    public void initializeConnection() {
        TextInputDialog ipDialog = new TextInputDialog();
        ipDialog.setTitle("Connect to new user");
        ipDialog.setHeaderText("Connect to new user");
        ipDialog.setContentText("Enter the user ip:");

        Optional<String> ipOption = ipDialog.showAndWait();
        ipOption.ifPresent(ip -> controller.connectToUser(ip));
    }

    public void changeUserName() {
        TextInputDialog userNameDialog = new TextInputDialog();
        userNameDialog.setTitle("Change your name");
        userNameDialog.setHeaderText("Change your name");
        userNameDialog.setContentText("Enter your new name:");

        Optional<String> nameOption = userNameDialog.showAndWait();
        nameOption.ifPresent(name -> controller.changeUserName(name));
    }

    public void showErrorPopup(String error) {
        Alert alert = new Alert(Alert.AlertType.ERROR, error, ButtonType.CLOSE);
        alert.showAndWait();
    }

    public void quit() {
        Logger.getAnonymousLogger().log(Level.INFO, "Exit");
        System.exit(0);
    }

    public void conversationSelected() {
        User recipient = getFocusedRecipient();
        if (recipient != null && controller.isConnected(recipient)) {
            input_text.setEditable(true);
            button_send.setDisable(false);
            setConversation(controller.retrieveConversation(recipient));
        } else {
            input_text.setEditable(false);
            button_send.setDisable(true);
        }
    }

    public void userLeft(User user) {
        User recipient = getFocusedRecipient();
        if (recipient != null && recipient.equals(user)) {
            input_text.setEditable(false);
            button_send.setDisable(true);
        }
    }

    public void setUserName(String name) {
        user_name.setText(name);
    }


}
