package com.ecep.contract.ui;

import java.util.HashMap;
import java.util.Locale;
import java.util.logging.Level;

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

import com.ecep.contract.Message;
import com.ecep.contract.MessageHolder;
import com.ecep.contract.SpringApp;
import com.ecep.contract.ds.company.service.CompanyService;
import com.ecep.contract.ds.other.service.EmployeeService;
import com.ecep.contract.ds.other.service.SysConfService;
import com.ecep.contract.model.Employee;

import lombok.Getter;
import lombok.Setter;

public abstract class Tasker<T> implements java.util.concurrent.Callable<T> {
    private static final Logger logger = LoggerFactory.getLogger(Tasker.class);
    @Setter
    protected java.util.function.Predicate<Message> messageHandler;
    @Setter
    private java.util.function.Predicate<String> titleHandler;
    @Setter
    private Locale locale = Locale.getDefault();
    private HashMap<Class<?>, Object> cachedMap = new HashMap<>();
    @Setter
    private Employee currentUser;
    @Setter
    @Getter
    private boolean cancelled = false;

    public SysConfService getConfService() {
        return getCachedBean(SysConfService.class);
    }

    public CompanyService getCompanyService() {
        return getCachedBean(CompanyService.class);
    }

    public EmployeeService getEmployeeService() {
        return getCachedBean(EmployeeService.class);
    }

    protected <K> K getBean(Class<K> requiredType) throws BeansException {
        return SpringApp.getBean(requiredType);
    }

    protected <K> K getCachedBean(Class<K> requiredType) {
        @SuppressWarnings("unchecked")
        K bean = (K) cachedMap.get(requiredType);
        if (bean == null) {
            bean = getBean(requiredType);
            cachedMap.put(requiredType, bean);
        }
        return bean;
    }

    public Employee getCurrentUser() {
        return currentUser;
    }

    @Override
    public T call() throws Exception {
        MessageHolderImpl holder = new MessageHolderImpl();
        try {
            return execute(holder);
        } catch (Exception e) {
            holder.error(e.getMessage());
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    protected abstract T execute(MessageHolder holder) throws Exception;

    protected void updateMessage(String message) {
        updateMessage(Level.INFO, message);
    }

    public void updateTitle(String title) {
        if (titleHandler != null) {
            if (titleHandler.test(title)) {
                return;
            }
        }
        updateMessage(Level.INFO, title);
    }

    protected void updateMessage(Level level, String message) {
        if (messageHandler != null) {
            if (messageHandler.test(new Message(level, message))) {
                return;
            }
        }
    }

    /**
     * 错误消息处理
     */
    public class MessageHolderImpl implements MessageHolder {
        public MessageHolderImpl() {
        }

        @Override
        public void addMessage(Level level, String message) {
            updateMessage(level, message);
        }
    }

    public Locale getLocale() {
        return locale;
    }

    public void updateProgress(double d, double total) {
    }
}
