package cn.forevermc.launcher.service;

import cn.forevermc.launcher.bean.APISetting;
import javafx.beans.property.ReadOnlyStringProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import lombok.extern.slf4j.Slf4j;
import com.imyeyu.inject.annotation.Inject;
import com.imyeyu.inject.annotation.InvokeForInjected;
import com.imyeyu.inject.annotation.Service;
import com.imyeyu.java.TimiJava;

import java.util.Collections;

/**
 * 动态闪烁标语服务
 *
 * @author 夜雨
 * @since 2022-11-22 10:45
 */
@Slf4j
@Service
public class SplashService {

	@Inject
	private APISettingService apiSettingService;

	private final StringProperty value;

	public SplashService() {
		value = new SimpleStringProperty();
		value.set("Forever MC!");
	}

	@InvokeForInjected
	private void injected() {
		apiSettingService.valueProperty().addListener((obs, o, setting) -> {
			if (setting == null) {
				return;
			}
			APISetting.DynamicList splashes = setting.getSplashes();
			if (TimiJava.isNotEmpty(splashes.getActive())) {
				value.set(splashes.getActive());
				return;
			}
			Collections.shuffle(splashes.getList());
			value.set(splashes.getList().get(0));
		});
	}

	public ReadOnlyStringProperty valueProperty() {
		return value;
	}
}
