package fr.insa.clavardator.client.ui.chat;

import com.jfoenix.controls.JFXButton;
import fr.insa.clavardator.client.users.CurrentUser;
import fr.insa.clavardator.lib.message.FileMessage;
import fr.insa.clavardator.lib.message.Message;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import org.kordamp.ikonli.javafx.FontIcon;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.DateFormat;
import java.util.ResourceBundle;

public class MessageListItemController implements Initializable {

	private Message currentMessage;

	@FXML
	private VBox container;
	@FXML
	private JFXButton button;
	@FXML
	private Label timestamp;
	@FXML
	private FontIcon attachmentIcon;

	/**
	 * Sets the message to display
	 *
	 * @param message The message to display
	 */
	public void setMessage(Message message) {
		if (!message.equals(currentMessage)) {
			currentMessage = message;
			if (message instanceof FileMessage) {
				FileMessage fileMessage = ((FileMessage) message);
				displayFile(fileMessage);
			} else {
				displayClassicMessage();
			}
			styleMessage();
		}
	}

	private void setMessageText(String text) {
		button.setText(text);
		timestamp.setText(DateFormat.getTimeInstance().format(currentMessage.getDate()));
	}

	private void displayFile(FileMessage fileMessage) {
		String text = currentMessage.getText();
		if (text != null && !text.isEmpty()) {
			text += "\n";
		}
		text += "<" + fileMessage.getFileName() + ">";
		setMessageText(text);
		button.setOnMouseClicked(event -> openFile(fileMessage.getPath()));
		button.setButtonType(JFXButton.ButtonType.RAISED);
		attachmentIcon.setVisible(true);
		// using set managed doesn't always work, so set the icon size as a workaround
		attachmentIcon.setIconSize(24);
	}

	private void displayClassicMessage() {
		setMessageText(currentMessage.getText());
		button.setOnMouseClicked(null);
		button.setButtonType(JFXButton.ButtonType.FLAT);
		attachmentIcon.setVisible(false);
		// using set managed doesn't always work, so set the icon size as a workaround
		attachmentIcon.setIconSize(1);
	}

	private void styleMessage() {
		clearBackground();
		if (CurrentUser.getInstance().getId().equals(currentMessage.getSender().id)) {
			container.setAlignment(Pos.CENTER_RIGHT);
			button.getStyleClass().add("message-self");
		} else {
			container.setAlignment(Pos.CENTER_LEFT);
			button.getStyleClass().add("message-other");
		}
	}

	public void openFile(String path) {
		if (Desktop.isDesktopSupported()) {
			new Thread(() -> {
				try {
					Desktop.getDesktop().open(new File(path));
				} catch (IOException e) {
					e.printStackTrace();
				}
			}).start();
		}
	}

	/**
	 * Removes any style applied to the background
	 */
	private void clearBackground() {
		button.getStyleClass().remove("message-self");
		button.getStyleClass().remove("message-other");
	}

	@Override
	public void initialize(URL url, ResourceBundle rb) {
	}
}