package com.asha.vrlib.strategy;

import android.app.Activity;

import com.asha.vrlib.MDVRLibrary;
import com.asha.vrlib.common.MDGLHandler;
import com.asha.vrlib.common.MDMainHandler;

import java.util.Arrays;

/**
 * Created by hzqiujiadi on 16/3/19.
 * hzqiujiadi ashqalcn@gmail.com
 */
public abstract class ModeManager<T extends IModeStrategy> {
    private int mMode;
    private T mStrategy;
    private MDVRLibrary.INotSupportCallback mCallback;
    private MDGLHandler mGLHandler;

    public ModeManager(int mode, MDGLHandler handler) {
        this.mGLHandler = handler;
        this.mMode = mode;
    }

    /**
     * must call after new instance
     * @param activity activity
     */
    public void prepare(Activity activity, MDVRLibrary.INotSupportCallback callback){
        mCallback = callback;
        initMode(activity,mMode);
    }

    abstract protected T createStrategy(int mode);

    abstract protected int[] getModes();

    private void initMode(Activity activity, final int mode){
        if (mStrategy != null){
            off(activity);
        }
        mStrategy = createStrategy(mode);
        if (!mStrategy.isSupport(activity)){
            MDMainHandler.sharedHandler().post(new Runnable() {
                @Override
                public void run() {
                    if (mCallback != null) mCallback.onNotSupport(mode);
                }
            });
        } else {
            on(activity);
        }
    }

    public void switchMode(final Activity activity){
        mGLHandler.post(new Runnable() {
            @Override
            public void run() {
                int[] modes = getModes();
                int mode = getMode();
                int index = Arrays.binarySearch(modes, mode);
                int nextIndex = (index + 1) %  modes.length;
                int nextMode = modes[nextIndex];

                switchMode(activity, nextMode);
            }
        });
    }

    public void switchMode(final Activity activity, final int mode){
        mGLHandler.post(new Runnable() {
            @Override
            public void run() {
                if (mode == getMode()) return;
                mMode = mode;

                initMode(activity, mMode);
            }
        });
    }

    public void on(Activity activity) {
        if (mStrategy.isSupport(activity))
            mStrategy.on(activity);
    }

    public void off(Activity activity) {
        if (mStrategy.isSupport(activity))
            mStrategy.off(activity);
    }

    protected T getStrategy() {
        return mStrategy;
    }

    public int getMode() {
        return mMode;
    }

    public MDGLHandler getGLHandler() {
        return mGLHandler;
    }
}

@@ -1512,111 +1512,8 @@
 y)%7B%0A
-        mGLHandler.post(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A        
     
@@ -1546,32 +1546,24 @@
 ();%0A        
- 
-       
 int mode = g
@@ -1576,32 +1576,24 @@
 ();%0A        
-    
-    
 int index = 
@@ -1622,32 +1622,24 @@
 des, mode);%0A
-        
         int 
@@ -1679,24 +1679,16 @@
 length;%0A
-        
         
@@ -1733,71 +1733,210 @@
     
-        switchMode(activity, nextMode);%0A            %7D%0A        %7D
+switchMode(activity, nextMode);%0A    %7D%0A%0A    public void switchMode(final Activity activity, final int mode)%7B%0A        if (mode == getMode()) return;%0A        mMode = mode;%0A%0A        initMode(activity, mMode
 );%0A 
@@ -1953,34 +1953,26 @@
 public void 
-switchMode
+on
 (final Activ
@@ -1983,32 +1983,104 @@
 activity
-, final int mode
+) %7B%0A        final T tmpStrategy = mStrategy;%0A        if (tmpStrategy.isSupport(activity)
 )%7B%0A     
@@ -2082,17 +2082,23 @@
         
+ 
-m
+   get
 GLHandle
@@ -2098,16 +2098,18 @@
 LHandler
+()
 .post(ne
@@ -2127,32 +2127,36 @@
 ) %7B%0A            
+  
+  
 @Override%0A      
@@ -2141,32 +2141,36 @@
       @Override%0A
+    
             publ
@@ -2205,138 +2205,81 @@
     
-if (mode == getMode()) return;%0A                mMode = mode;%0A%0A                initMode(activity, mMode);%0A            %7D%0A        %7D);
+    tmpStrategy.on(activity);%0A                %7D%0A            %7D);%0A        %7D
 %0A   
@@ -2299,18 +2299,25 @@
 c void o
-n(
+ff(final 
 Activity
@@ -2321,32 +2321,73 @@
 ity activity) %7B%0A
+        final T tmpStrategy = mStrategy;%0A
         if (mStr
@@ -2374,33 +2374,35 @@
 gy;%0A        if (
-m
+tmp
 Strategy.isSuppo
@@ -2406,32 +2406,33 @@
 pport(activity))
+%7B
 %0A            mSt
@@ -2432,159 +2432,197 @@
     
-mStrategy.on(activity);%0A    %7D%0A%0A    public void off(Activity activity) %7B%0A        if (mStrategy.isSupport(activity))%0A            mStrategy.off(activity);
+getGLHandler().post(new Runnable() %7B%0A                @Override%0A                public void run() %7B%0A                    tmpStrategy.off(activity);%0A                %7D%0A            %7D);%0A        %7D
 %0A   

switch mode immediately.