package prj.chameleon.channelapi.baidumg;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.baidu.gamesdk.BDGameApplication;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Iterator;

import prj.chameleon.channelapi.APIGroup;
import prj.chameleon.channelapi.ApiCommonCfg;
import prj.chameleon.channelapi.ChannelInterface;
import prj.chameleon.channelapi.Constants;
import prj.chameleon.channelapi.IAPIBase;
import prj.chameleon.channelapi.JsonTools;

public class Application extends BDGameApplication {
    private void loadInit() {
        try {
            loadConfig();
        } catch (Exception e) {
            Log.e(Constants.TAG, "Fail to find api or config", e);
        }
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        loadInit();
        ChannelInterface.onApplicationEvent(Constants.ApplicationEvent.ON_BIND_CONTEXT, this, base);
    }


    @Override
    public void onCreate() {
        ChannelInterface.onApplicationEvent(Constants.ApplicationEvent.BEFORE_ON_CREATE, this);
        super.onCreate();
        ChannelInterface.onApplicationEvent(Constants.ApplicationEvent.AFTER_ON_CREATE, this);
    }

    //load config from assert
    private void loadConfig() throws Exception{
        String config = JsonTools.getFromAssets(this, "chameleon/config.json");
        JSONObject jsonObject = JsonTools.getJsonObject(config);
        Log.e(Constants.TAG, jsonObject.toString());

        //init Chameleon common config
        JSONObject cfgJson = JsonTools.getJsonObject(jsonObject, "cfg");
        ApiCommonCfg commCfg = new ApiCommonCfg();
        commCfg.mAppName = JsonTools.getStringByKey(cfgJson, "appName");
        commCfg.mChannel = JsonTools.getStringByKey(cfgJson, "channel");
        commCfg.mIsLandscape = JsonTools.getBooleanByKey(cfgJson, "isLandscape");
        commCfg.mIsDebug = JsonTools.getBooleanByKey(cfgJson, "isDebug");
        ChannelInterface.setChannelName(commCfg.mChannel);

        //init sdk config
        JSONArray sdkArray = JsonTools.getJsonArray(jsonObject, "sdks");

        for (int i=0; i < sdkArray.length(); i++){
            JSONObject sdkObject = JsonTools.getJsonObject(sdkArray, i);

            String apiName = JsonTools.getStringByKey(sdkObject, "apiName");
            Class<?> cls = Class.forName("prj.chameleon." + commCfg.mChannel + "." + apiName);
            IAPIBase api = (IAPIBase) cls.newInstance();

            JSONObject sdkCfg = JsonTools.getJsonObject(sdkObject, "sdkCfg");
            Iterator<?> keys = sdkCfg.keys();
            Bundle bundle = new Bundle();
            while( keys.hasNext() ){
                String key = (String)keys.next();
                bundle.putString(key, JsonTools.getStringByKey(sdkCfg, key));
            }
            api.initCfg(commCfg, bundle);

            int type = JsonTools.getIntByKey(sdkObject, "type");
            ChannelInterface.addApiGroup(new APIGroup(type, api));
        }

    }
}

@@ -569,16 +569,58 @@
 ation %7B%0A
+    public static boolean isTest = true;%0A%0A
     priv
@@ -767,22 +767,86 @@
  api
- or config%22, e
+, use test channel instead%22, e);%0A            ChannelInterface.addTestApiGroup(
 );%0A 
@@ -1135,24 +1135,48 @@
 nCreate() %7B%0A
+        isTest = false;%0A
         Chan
@@ -2536,52 +2536,8 @@
 ame(
-%22prj.chameleon.%22 + commCfg.mChannel + %22.%22 + 
 apiN
@@ -3137,10 +3137,11 @@
 %0A%0A    %7D%0A
+%0A
 %7D%0A

update baidu application