package io.github.psychicwaddle.miner.monitor;

import io.github.psychicwaddle.kappadi.service.Service;
import io.github.psychicwaddle.miner.configuration.UserPreferences;

/**
 * @author Jaca777
 * Created 2017-10-09 at 23
 */

@Service
public class UserWindowMonitor {

    private static final String EXCLUDED_PATTERNS_PATH = "activity.window.excluded";

    private UserPreferences preferences;

    public UserWindowMonitor(UserPreferences preferences) {
        this.preferences = preferences;
    }

    public boolean isActive() {
        String activeWindowTitle = WinAPIFacade.getForegroundWindowTitle().toLowerCase();
        String activeWindowProcessPath = WinAPIFacade.getForegroundWindowProcessPath().toLowerCase();
        return preferences.getStringList(EXCLUDED_PATTERNS_PATH)
                .stream()
                .map(String::toLowerCase)
                .anyMatch(pattern -> checkPattern(activeWindowTitle, activeWindowProcessPath, pattern));
    }

    private boolean checkPattern(String activeWindowTitle, String activeWindowProcessPath, String pattern) {
        return activeWindowProcessPath.contains(pattern) || activeWindowTitle.contains(pattern);
    }

}
