package cn.forevermc.launcher.ctrl.pane;

import cn.forevermc.launcher.service.FMCLoginService;
import cn.forevermc.launcher.view.pages.PlayerSelectPane;
import com.google.gson.Gson;
import javafx.collections.ObservableList;
import lombok.Getter;
import lombok.Setter;
import com.imyeyu.inject.annotation.Component;
import com.imyeyu.inject.annotation.Inject;
import com.imyeyu.java.bean.Callback;

/**
 * @author 夜雨
 * @since 2024-05-21 19:40
 */
@Component
public class PlayerSelect extends PlayerSelectPane {

	/**
	 *
	 *
	 * @author 夜雨
	 * @since 2024-05-21 23:14
	 */
	public enum Action {

		CONFIRM,

		CANCEL
	}

	@Inject
	private Gson gson;

	@Getter
	private Action action;

	@Setter
	private Callback onCloseEvent;

	public PlayerSelect() {
		// 确认
		confirm.setOnAction(e -> {
			action = Action.CONFIRM;
			onCloseEvent.handler();
		});
		// 取消
		cancel.setOnAction(e -> {
			action = Action.CANCEL;
			onCloseEvent.handler();
		});
	}

	@Override
	protected void onShow() {
		list.getItems().sort((o1, o2) -> {
			if (o1.getLastLoginAt() == null || o2.getLastLoginAt() == null) {
				return -1;
			}
			return o2.getLastLoginAt().compareTo(o1.getLastLoginAt());
		});
		list.getSelectionModel().select(0);
	}

	/** @return 列表数据 */
	public ObservableList<FMCLoginService.MinecraftPlayer> getItems() {
		return list.getItems();
	}

	/** @return 选中的玩家 */
	public FMCLoginService.MinecraftPlayer getSelected() {
		return list.getSelectionModel().getSelectedItem();
	}
}
