package com.zzw.packunitydemo;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;

import org.json.JSONException;
import org.json.JSONObject;

import com.funcell.platform.android.game.proxy.data.ParamsContainer;


public class FuncellSDKUtils
{
	//sdk回调到客户端
	public enum CallbackType
	{
		InitCallback("InitCallback"),
		LoginCallback("LoginCallback"),
		LogoutCallback("LogoutCallback"),
		SwitchAccountCallback("SwitchAccountCallback"),
		ExitCallback("ExitCallback"),
		PayCallback("PayCallback"),
		
		//以下是可选的
		EveDataCallback("EveDataCallback"),
		PayListCallback("PayListCallback"),
		NoticeListCallback("NoticeListCallback"),
		ClientIDCallback("ClientIDCallback"),
		CallFunctionCallback("CallFunctionCallback");
		
		private final String name;
		private CallbackType(String str) {
			name = str;
		}
		public boolean equalsName(String otherName) {
			return otherName == null ? false : name.equals(otherName);
		}
		public String toString() {
			return this.name;
		}
	}
	
	//客户端请求
	public enum RequestOptType
	{
		ReqLogin("ReqLogin"),
		ReqLogout("ReqLogout"),
		ReqExit("ReqExit"),
		ReqPay("ReqPay"),
		
		ReqEveData("ReqEveData"),
		ReqPayList("ReqPayList"),
		ReqNoticeList("ReqNoticeList"),
		ReqCallFunction("ReqCallFunction");
		
		
		private final String name;
		private RequestOptType(String str) {
			name = str;
		}
		public boolean equalsName(String otherName) {
			return otherName == null ? false : name.equals(otherName);
		}
		public String toString() {
			return this.name;
		}
	}
	
	//客户端发起数据统计
	public enum DataTypes
	{
		DATA_LOGIN("DATA_LOGIN"),
		DATA_CREATE_ROLE("DATA_CREATE_ROLE"),
		DATA_ROLE_LEVELUP("DATA_ROLE_LEVELUP"),
		DATA_SERVER_ROLE_INFO("DATA_SERVER_ROLE_INFO");
		
		private final String name;
		private DataTypes(String str) {
			name = str;
		}
		public boolean equalsName(String otherName) {
			return otherName == null ? false : name.equals(otherName);
		}
		public String toString() {
			return this.name;
		}
	}
	
	//回调函数的结果
	public enum ResultCode
	{
		Success("Success"),
		Fail("Fail"),
		Cancel("Cancel"),
		Unknown("Unknown");
		
		private final String name;
		private ResultCode(String str) {
			name = str;
		}
		public boolean equalsName(String otherName) {
			return otherName == null ? false : name.equals(otherName);
		}
		public String toString() {
			return this.name;
		}
	}
	
	/***
	 * 通过反射调用方法
	 * @param instance 实例
	 * @param methodName 方法名
	 * @param arg 参数
	 */
	public static void CallMethod(Object instance, String methodName, String arg)
	{
		boolean isString = true;
		Method md = null;
		try {
			md = instance.getClass().getMethod(methodName, String.class);
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				md = instance.getClass().getMethod(methodName, Integer.class);
				isString = false;
			} catch (NoSuchMethodException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} 
		
		if(md != null)
		{
			try {
				md.invoke(instance, isString?arg : Integer.parseInt(arg));
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * Json转ParamsContainer类
	 * @param json
	 * @return
	 */
	public static ParamsContainer ParseJsonToParamsContainer(String json, String userMark){
		ParamsContainer parmas = JsonObjectCoder.jsonToParamsContainer(json, null);
		parmas.putString("usermark", userMark);
		return parmas;
	}
	
	/***
	 * 解析json中的方法和参数，并且调用方法
	 * @param obj  实例
	 * @param json 将方法和参数封装成json数据
	 */
	public static void ParseJsonAndRunMethod(Object obj, String json)
	{
		JSONObject jo;
		try {
			jo = new JSONObject(json);
			Iterator<String> itor = jo.keys();
			while (itor.hasNext()) {
				String key = itor.next();
				String value = jo.getString(key);
				CallMethod(obj, key, value);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
