package com.ecep.contract.controller.tab;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;

import com.ecep.contract.Desktop;
import com.ecep.contract.SpringApp;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ScheduledExecutorService;

public interface Skin {
    Logger logger = LoggerFactory.getLogger(Skin.class);

    void install();

    /**
     * 处理错误
     *
     * @param ex 错误信息
     * @return Void
     */
    default Void handleException(Throwable ex) {
        handleException(ex.getMessage(), ex);
        return null;
    }

    void handleException(String message, Throwable ex);

    /**
     * 工具方法， 用于获取 spring 管理的 bean
     *
     * @see org.springframework.beans.factory.BeanFactory#getBean(Class)
     */
    default <TT> TT getBean(Class<TT> requiredType) throws BeansException {
        return SpringApp.getBean(requiredType);
    }

    default CompletableFuture<Void> runAsync(Runnable runnable) {
        return CompletableFuture.runAsync(runnable, getExecutorService()).exceptionally(this::handleException);
    }

    default ScheduledExecutorService getExecutorService() {
        return Desktop.instance.getExecutorService();
    }
}
