package cn.forevermc.launcher.view.components.control;

import cn.forevermc.launcher.util.Resources;
import cn.forevermc.launcher.util.Sound;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Border;
import com.imyeyu.fx.ui.TimiFXUI;
import com.imyeyu.fx.utils.BorderStroke;
import com.imyeyu.network.Network;

/**
 * MC 链接
 *
 * @author 夜雨
 * @since 2022-11-24 17:46
 */
public class MCLink extends Label implements TimiFXUI {

	private static final Border BORDER_DEFAULT = new BorderStroke(Colorful.TRANSPARENT).width(0, 0, 2, 0).build();
	private static final Border BORDER_HOVER = new BorderStroke(Colorful.LIGHT_GRAY).width(0, 0, 2, 0).build();

	private final StringProperty url;

	public MCLink(String text) {
		this(text, text);
	}

	public MCLink(String text, String url) {
		super(text);

		this.url = new SimpleStringProperty(url);

		setEffect(Resources.TEXT_SHADOW);
		setTextFill(Colorful.WHITE);
		borderProperty().bind(Bindings.when(hoverProperty()).then(BORDER_HOVER).otherwise(BORDER_DEFAULT));
		addEventFilter(MouseEvent.MOUSE_PRESSED, e -> {
			Network.openURIInBrowser(this.url.get());
			Sound.CLICK.play();
		});
	}

	/** @return 获取 URL */
	public String getUrl() {
		return url.get();
	}

	/** @param url 设置 URL */
	public void setUrl(String url) {
		this.url.set(url);
	}

	/** @return URL 监听 */
	public StringProperty urlProperty() {
		return url;
	}
}
