package h2o.common;

import h2o.common.Tools;
import h2o.common.exception.ExceptionUtil;
import h2o.common.collections.CollectionUtil;
import org.apache.commons.configuration.PropertiesConfiguration;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Mode {
	
	public static final String PROD     = "PROD";
	public static final String TEST     = "TEST";
    public static final String DEV      = "DEV";
	
	private static final String mode;
	
	public static final boolean prodMode;
	public static final boolean testMode;
    public static final boolean devMode;

    public static final boolean debugMode;
	
	private static final Set<String> userModeSet = new HashSet<String>();
	
	static {
		
		boolean p = false;
		boolean t = false;
        boolean d = false;

        boolean debug = false;

		String m;
		
		try {

			PropertiesConfiguration config = new PropertiesConfiguration("mode.properties");
			
			m = config.getString("mode", PROD ).trim().toUpperCase();
			try {
                debug  = config.getBoolean("debug" , false );
            } catch ( Exception e ) {

            }

			if ( PROD.equals( m ) ) {
			    p = true;
            } else if ( TEST.equals( m ) ) {
				t = true;				
			} else if ( DEV.equals( m ) ) {
				d = true;				
			} else {				
                throw new IllegalArgumentException(m);
			}
			
			String userModes = config.getString("userMode","").trim().toUpperCase();
			
			if( !"".equals(userModes) ) {

                List<String> ums =  CollectionUtil.string2List( false , userModes, new String[] {":",",",";"," ", "\t"} , null );
			    userModeSet.addAll( ums );

			}
			
			
			
		} catch (Throwable e) {

		    e.fillInStackTrace();
		    Tools.log.error( e );

		    throw ExceptionUtil.toRuntimeException(e);

		}
		
		prodMode    = p;
		devMode     = d;
		testMode    = t;

		debugMode   = debug;

		mode = m;
		
		Tools.log.info("Mode : {}"       , mode );
        Tools.log.info("Debug Mode : {}" , debugMode );
		Tools.log.info("User Mode : {}"  , userModeSet );
	}
	
	private Mode() {}
	
	
	public static boolean isMode( String m ) {

		if(m == null) {
			return false;
		}
		
		return m.trim().toUpperCase().equals(mode);
	}

    public static boolean isDebugMode() {
	    return debugMode;
    }
	
	public static boolean isUserMode( String m ) {

		if(m == null) {
			return false;
		}
		
		return userModeSet.contains( m.trim().toUpperCase() );
	}
	
	

}

@@ -657,27 +657,24 @@
 c final 
-Set%3C
 String
-%3E
+%5B%5D
  userMod
@@ -678,35 +678,14 @@
 Mode
-Set = new HashSet%3CString%3E()
+Arrays
 ;%0A%09%0A
@@ -799,16 +799,55 @@
 false;%0A%0A
+        String%5B%5D uma = new String%5B0%5D;%0A%0A
 %09%09String
@@ -1487,25 +1487,24 @@
 rModes) ) %7B%0A
-%0A
             
@@ -1625,43 +1625,69 @@
  );%0A
-%09%09%09    userModeSet.addAll( ums
+                uma = ums.toArray( new String%5Bums.size()%5D
  );%0A
-%0A
 %09%09%09%7D
@@ -1922,16 +1922,41 @@
 debug;%0A%0A
+%09%09userModeArrays = uma;%0A%0A
 %09%09mode =
@@ -2108,19 +2108,22 @@
 userMode
-Set
+Arrays
  );%0A%09%7D%0A%09
@@ -2448,66 +2448,183 @@
 %09%09%7D%0A
-%09%09
 %0A%09%09
-return userModeSet.contains( m.trim().toUpperCase() )
+String um = m.trim().toUpperCase();%0A%0A%09%09for ( String u : userModeArrays  ) %7B%0A%09%09    if ( u.equals(um) ) %7B%0A%09%09        return true;%0A            %7D%0A        %7D%0A%0A        return false
 ;%0A%09%7D

fix bug Mode的线程不安全