/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.system;

import java.util.EnumSet;
import java.util.Locale;
import java.util.Set;
import net.ranides.assira.text.StringUtils;
import net.ranides.assira.trace.TraceUtils;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public enum HostSystem {
    AIX("aix"),
    HP_UX("hp-ux"),
    IRIX("irix"),
    MAC("mac"),
    MAC_OSX("mac os x"),
    OS_2("os/2"),
    SOLARIS("solaris"),
    SUN_OS("sunos"),
    LINUX("linux"),
    UNIX("aix|hp-ux|irix|linux|mac os x|solaris|sunos"),
    WINDOWS("windows"),
    WINDOWS_NT("windows nt"),
    WINDOWS_9X("windows 9", "4"),
    WINDOWS_95("windows 95"),
    WINDOWS_98("windows 98"),
    WINDOWS_ME("windows me"),
    WINDOWS_2000("windows 2000"),
    WINDOWS_XP("windows xp"),
    WINDOWS_VISTA("windows vista"),
    WINDOWS_7("windows 7"),
    WINDOWS_8("windows 8"),
    WINDOWS_10("windows 10"),
    MAVEN( TraceUtils.getNames().stream().anyMatch(c -> c.startsWith("org.apache.maven.surefire.")) ),
    JUNIT( null!=RuntimeUtils.getProperty("test") )
    ;
    
    private static final class Li { // NOPMD - lazy init idiom
        
        public static final String OS_NAME = RuntimeUtils.getProperty("os.name", "").toLowerCase(Locale.ROOT);
    
        public static final String OS_VERSION = RuntimeUtils.getProperty("os.version", "").toLowerCase(Locale.ROOT);
    }
    
    private static final EnumSet<HostSystem> CURRENT = EnumSet.noneOf(HostSystem.class);
    
    static {
        for(HostSystem host : values()) {
            if(host.detected()) {
                CURRENT.add(host);
            }
        }
    }
    
    private final boolean detected;

    HostSystem(String name) {
        this.detected = StringUtils.split(name,"|").matchAny(Li.OS_NAME::startsWith);
    }
    
    HostSystem(String name, String version) {
        this.detected = StringUtils.split(name,"|").matchAny(Li.OS_NAME::startsWith) && Li.OS_VERSION.startsWith(version);
    }

    private HostSystem(boolean detected) {
        this.detected = detected;
    }
    
    public boolean detected() {
        return detected;
    }
    
    public static Set<HostSystem> current() {
        return EnumSet.copyOf(CURRENT);
    }
    
}
