package com.oo.seex.oo_statistics_lib.getjsonobject;

import android.text.TextUtils;

import com.oo.seex.oo_statistics_lib.bean.ActivityStaBean;
import com.oo.seex.oo_statistics_lib.bean.ErrorStaBean;
import com.oo.seex.oo_statistics_lib.bean.EventStaBean;
import com.oo.seex.oo_statistics_lib.bean.FragmentBean;
import com.oo.seex.oo_statistics_lib.bean.PostClientBean;
import com.oo.seex.oo_statistics_lib.utils.CommonUtil;

import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by efort on 15/8/3.
 * 获取我们传递信息的具体json串
 *
 * @author efort
 */
public class GetJson {
    /**
     * 获取我们传递信息的具体json串_clentFirst
     *
     * @param postClientBean
     * @return
     */
    public static JSONObject getClentJsonObject(PostClientBean postClientBean) {
        JSONObject clientData = null;
        if (postClientBean == null) {
            return null;
        }
        try {
            clientData = new JSONObject();
            clientData.put("device_name", postClientBean.getDevicename());
            clientData.put("have_bt", postClientBean.getHavebt());
            clientData.put("have_gps", postClientBean.getHavegps());
            clientData.put("have_gravity", postClientBean.getHavegravity());
            clientData.put("channel", postClientBean.getChannel());
            clientData.put("resolution", postClientBean.getResolution());
            clientData.put("platform", postClientBean.getPlatform());
            clientData.put("os", postClientBean.getOs());
            clientData.put("os_version", postClientBean.getOs_version());
            clientData.put("language", postClientBean.getLanguage());
            clientData.put("javasupport", postClientBean.getJavasupport());
            clientData.put("wifimac", postClientBean.getWifimac());
            clientData.put("network", postClientBean.getNetwork());
            clientData.put("laititude", postClientBean.getLaititude());
            clientData.put("longtitude", postClientBean.getLongtitude());
            clientData.put("service_supplier", postClientBean.getService_supplier());
            clientData.put("country", postClientBean.getCountry());
            clientData.put("province", postClientBean.getProvence());
            clientData.put("city", postClientBean.getCity());
            clientData.put("address", postClientBean.getAddress());
            clientData.put("client_date", postClientBean.getClientData());
            clientData.put("isjailbroken", postClientBean.getIsjailbroken());

        } catch (JSONException e) {
            clientData = null;
            CommonUtil.printLog("OOAgent", "json error in emitCustomLogReport");
            CommonUtil.printLog(e);
        }
        return clientData;
    }

    /**
     * 获取我们传递信息的具体json串_fragments
     *
     * @param fragment
     * @return
     */
    public static JSONObject getFragmentJSONObject(FragmentBean fragment) {
        JSONObject info = null;
        if (fragment == null) {
            return null;
        }
        try {
            info = new JSONObject();

            info.put("fragment", fragment.getFragments());
            info.put("start_time", fragment.getFragment_start_millis());
            info.put("end_time", fragment.getFragment_end_millis());
            info.put("duration", fragment.getFragment_duration());
        } catch (JSONException e) {
            info = null;
            CommonUtil.printLog("OOAgent", "json error in emitCustomLogReport");
            CommonUtil.printLog(e);
        }
        return info;
    }

//    /**
//     * 获取我们传递信息的HttpEntity_fragments
//     *
//     * @param fragment
//     * @return
//     */
//    public static List<NameValuePair> getFragmentParams(FragmentBean fragment) {
//
//        List<NameValuePair> params = null;
//        if (fragment == null) {
//            return null;
//        }
//        try {
//
//            params = new ArrayList<>();
//            params.add(new BasicNameValuePair("fragment", fragment.getFragments()));
//            params.add(new BasicNameValuePair("start_time", fragment.getFragment_start_millis()));
//            params.add(new BasicNameValuePair("end_time", fragment.getFragment_end_millis()));
//            params.add(new BasicNameValuePair("duration", fragment.getFragment_duration()));
//        } catch (Exception e) {
//            CommonUtil.printLog("OOAgent", "params error in emitCustomLogReport");
//            CommonUtil.printLog(e);
//        }
//        return params;
//    }

//    /**
//     * 获取我们传递信息的HttpEntity_fragments
//     *
//     * @param fragment
//     * @return
//     */
//    public static HashMap<String,String> getFragmentParams(FragmentBean fragment) {
//
//        HashMap<String,String> params = null;
//        if (fragment == null) {
//            return null;
//        }
//        try {
//
//            params = new HashMap<String,String>();
//            params.put("fragment", fragment.getFragments());
//            params.put("start_time", fragment.getFragment_start_millis());
//            params.put("end_time", fragment.getFragment_end_millis());
//            params.put("duration", fragment.getFragment_duration());
//        } catch (Exception e) {
//            CommonUtil.printLog("OOAgent", "params error in emitCustomLogReport");
//            CommonUtil.printLog(e);
//        }
//        return params;
//    }

    /**
     * 获取我们传递信息的HttpEntity_fragments
     *
     * @param fragment
     * @return
     */
    public static HashMap<String,String> getFragmentParams(FragmentBean fragment) {

        HashMap<String,String> params = null;
        if (fragment == null) {
            return null;
        }
        try {

            params = new HashMap<String,String>();
            params.put("fragment", fragment.getFragments());
            params.put("start_time", fragment.getFragment_start_millis());
            params.put("end_time", fragment.getFragment_end_millis());
            params.put("duration", fragment.getFragment_duration());
        } catch (Exception e) {
            CommonUtil.printLog("OOAgent", "params error in emitCustomLogReport");
            CommonUtil.printLog(e);
        }
        return params;
    }

    /**
     * 获取我们传递信息Get请求的具体参数_fragments
     *
     * @param fragment
     * @return
     */
    public static String getFragmentGetParams(FragmentBean fragment) {
        String params = "";
        if (fragment == null) {
            return null;
        }
        try {

            StringBuffer sb = new StringBuffer();

            sb.append("?fragment=" + fragment.getFragments());
            sb.append("&startTime=" + fragment.getFragment_start_millis());
            sb.append("&endTime=" + fragment.getFragment_end_millis());
            sb.append("&duration=" + fragment.getFragment_duration());
            params = sb.toString();

            sb.delete(0, sb.length() - 1);
            sb = null;

        } catch (Exception e) {
            CommonUtil.printLog("OOAgent", "json error in emitCustomLogReport");
            CommonUtil.printLog(e);
        }
        return params;
    }

    /**
     * 获取我们传递信息的具体json串_activity
     *
     * @param staBean
     * @return
     */
    public static JSONObject getActivityJSONObject(ActivityStaBean staBean) {
        JSONObject info = null;
        if (staBean == null) {
            return null;
        }
        try {
            info = new JSONObject();
            info.put("activity", staBean.getActivities());
            info.put("parent_activity", staBean.getParent_activity());
            info.put("start_time", staBean.getStart_millis());
            info.put("end_time", staBean.getEnd_millis());
            info.put("duration", staBean.getDuration());
        } catch (JSONException e) {
            info = null;
            CommonUtil.printLog("OOAgent", "json error in emitCustomLogReport");
            CommonUtil.printLog(e);
        }
        return info;
    }

//    /**
//     * 获取我们传递信息的Params_activity;
//     *
//     * @param staBean
//     * @return
//     */
//    public static List<NameValuePair> getActivityParams(ActivityStaBean staBean) {
//
//        List<NameValuePair> params = null;
//        if (staBean == null) {
//            return null;
//        }
//        try {
//
//            params = new ArrayList<>();
//            params.add(new BasicNameValuePair("activity", staBean.getActivities()));
//            params.add(new BasicNameValuePair("parent_activity", staBean.getParent_activity()));
//            params.add(new BasicNameValuePair("start_time", staBean.getStart_millis()));
//            params.add(new BasicNameValuePair("end_time", staBean.getEnd_millis()));
//            params.add(new BasicNameValuePair("duration", staBean.getDuration()));
//        } catch (Exception e) {
//            CommonUtil.printLog("OOAgent", "params error in emitCustomLogReport");
//            CommonUtil.printLog(e);
//        }
//        return params;
//    }
    /**
     * 获取我们传递信息的Params_activity;
     *
     * @param staBean
     * @return
     */
    public static HashMap<String ,String > getActivityParams(ActivityStaBean staBean) {

        HashMap<String ,String > params = null;
        if (staBean == null) {
            return null;
        }
        try {

            params = new HashMap<>();
            params.put("activity", staBean.getActivities());
            params.put("parent_activity", staBean.getParent_activity());
            params.put("start_time", staBean.getStart_millis());
            params.put("end_time", staBean.getEnd_millis());
            params.put("duration", staBean.getDuration());
        } catch (Exception e) {
            CommonUtil.printLog("OOAgent", "params error in emitCustomLogReport");
            CommonUtil.printLog(e);
        }
        return params;
    }

    /**
     * 获取我们传递信息Get请求的具体参数_activity
     *
     * @param staBean
     * @return
     */
    public static String getActivityGetParams(ActivityStaBean staBean) {
        String params = "";
        if (staBean == null) {
            return null;
        }
        try {

            StringBuffer sb = new StringBuffer();

            sb.append("?fragment=" + staBean.getActivities());
            sb.append("&startTime=" + staBean.getStart_millis());
            sb.append("&endTime=" + staBean.getEnd_millis());
            sb.append("&duration" + staBean.getDuration());
            params = sb.toString();

            sb.delete(0, sb.length() - 1);
            sb = null;

        } catch (Exception e) {
            CommonUtil.printLog("OOAgent", "json error in emitCustomLogReport");
            CommonUtil.printLog(e);
        }
        return params;
    }

    /**
     * 自定义事件的JSONObject对象
     *
     * @param event
     * @return
     */
    public static JSONObject getEventJOSNobj(EventStaBean event) {
        JSONObject localJSONObject = null;
        if (event == null) {
            return null;
        }
        try {
            localJSONObject = new JSONObject();
            localJSONObject.put("time", event.getTime());
            //自定义的事件类型；每个应用都由自己命名相关event—id
            localJSONObject.put("aname", event.getEvent_id());
            if (!TextUtils.isEmpty(event.getLabel()))
                localJSONObject.put("label", event.getLabel());//自定义标签
            if (!TextUtils.isEmpty(event.getRemarks()) && event.getRemarkByMap() != null && event.getRemarkByMap().size() > 0) {
//                localJSONObject.put("provider", event.getRemarks());//内容
                if (event.getRemarkByMap() != null && event.getRemarkByMap().size() > 0) {
                    Iterator iter = event.getRemarkByMap().entrySet().iterator();
                    while (iter.hasNext()) {
                        Map.Entry entry = (Map.Entry) iter.next();
                        String key = (String) entry.getKey();
                        String val = (String) entry.getValue();

                        localJSONObject.put(key, val);
                    }
                }

            }
            localJSONObject.put("acc", event.getAcc());//次数

        } catch (JSONException localJSONException) {
            localJSONObject = null;
            CommonUtil.printLog("OOAgent", "json error in emitCustomLogReport");
            CommonUtil.printLog(localJSONException);
        }
        return localJSONObject;
    }

//    /**
//     * 自定义事件的JSONObject对象
//     *
//     * @param event
//     * @return
//     */
//    public static List<NameValuePair> getEventParams(EventStaBean event) {
//        List<NameValuePair> params = null;
//        JSONObject localJSONObject = null;
//        if (event == null) {
//            return null;
//        }
//        try {
//
//            params = new ArrayList<>();
//            localJSONObject = new JSONObject();
//            //localJSONObject.put("time", event.getTime());
//
//            params.add(new BasicNameValuePair("aname", event.getEvent_id()));
//
//            params.add(new BasicNameValuePair("label", event.getLabel()));
//
//            params.add(new BasicNameValuePair("acc", event.getAcc()));
//
//            //自定义的事件类型；每个应用都由自己命名相关event—id
//            //localJSONObject.put("aname", event.getEvent_id());
////            if (!TextUtils.isEmpty(event.getLabel()))
////                localJSONObject.put("label", event.getLabel());//自定义标签
//            if (!TextUtils.isEmpty(event.getRemarks()) && event.getRemarkByMap() != null && event.getRemarkByMap().size() > 0) {
////                localJSONObject.put("provider", event.getRemarks());//内容
//                if (event.getRemarkByMap() != null && event.getRemarkByMap().size() > 0) {
//                    Iterator iter = event.getRemarkByMap().entrySet().iterator();
//                    while (iter.hasNext()) {
//                        Map.Entry entry = (Map.Entry) iter.next();
//                        String key = (String) entry.getKey();
//                        String val = (String) entry.getValue();
//
//                        localJSONObject.put(key, val);
//                    }
//                }
//
//            }
//            //localJSONObject.put("acc", event.getAcc());//次数
//
//            params.add(new BasicNameValuePair("remark", localJSONObject.length() > 0 ? localJSONObject.toString() : ""));
//        } catch (JSONException localJSONException) {
//            //localJSONObject = null;
//            CommonUtil.printLog("OOAgent", "json error in emitCustomLogReport");
//            CommonUtil.printLog(localJSONException);
//        }
//        return params;
//    }
    /**
     * 自定义事件的JSONObject对象
     *
     * @param event
     * @return
     */
    public static HashMap<String,String> getEventParams(EventStaBean event) {
        HashMap<String,String> params = null;
        JSONObject localJSONObject = null;
        if (event == null) {
            return null;
        }
        try {

            params = new HashMap<String,String>();
            localJSONObject = new JSONObject();

            params.put("aname", event.getEvent_id());
            params.put("label", event.getLabel());
            params.put("acc", event.getAcc());

            //自定义的事件类型；每个应用都由自己命名相关event—id
            //localJSONObject.put("aname", event.getEvent_id());
//            if (!TextUtils.isEmpty(event.getLabel()))
//                localJSONObject.put("label", event.getLabel());//自定义标签
            if (!TextUtils.isEmpty(event.getRemarks()) && event.getRemarkByMap() != null && event.getRemarkByMap().size() > 0) {
//                localJSONObject.put("provider", event.getRemarks());//内容
                if (event.getRemarkByMap() != null && event.getRemarkByMap().size() > 0) {
                    Iterator iter = event.getRemarkByMap().entrySet().iterator();
                    while (iter.hasNext()) {
                        Map.Entry entry = (Map.Entry) iter.next();
                        String key = (String) entry.getKey();
                        String val = (String) entry.getValue();

                        localJSONObject.put(key, val);
                    }
                }

            }
            //localJSONObject.put("acc", event.getAcc());//次数

            params.put("remark", localJSONObject.length() > 0 ? localJSONObject.toString() : "");
        } catch (JSONException localJSONException) {
            CommonUtil.printLog("OOAgent", "json error in emitCustomLogReport");
            CommonUtil.printLog(localJSONException);
        }
        return params;
    }

    /**
     * 自定义事件的Get请求的参数字符串；
     *
     * @param event
     * @return
     */
    public static String getEventGetParams(EventStaBean event) {
        String params = "";
        JSONObject localJSONObject = null;
        if (event == null) {
            return null;
        }
        try {

            StringBuffer sb = new StringBuffer();

            sb.append("?aname=" + event.getEvent_id());
            sb.append("&label=" + (TextUtils.isEmpty(event.getLabel()) ? "" : event.getLabel()));
            sb.append("&acc=" + event.getAcc());
            localJSONObject = new JSONObject();
            if (!TextUtils.isEmpty(event.getRemarks()) && event.getRemarkByMap() != null && event.getRemarkByMap().size() > 0) {
//                localJSONObject.put("provider", event.getRemarks());//内容
                if (event.getRemarkByMap() != null && event.getRemarkByMap().size() > 0) {
                    Iterator iter = event.getRemarkByMap().entrySet().iterator();
                    while (iter.hasNext()) {
                        Map.Entry entry = (Map.Entry) iter.next();
                        String key = (String) entry.getKey();
                        String val = (String) entry.getValue();

                        localJSONObject.put(key, val);
                    }
                }

            }
//            localJSONObject.put("acc", event.getAcc());//次数
            if (localJSONObject.length() > 0)
                sb.append("&remark=" + (localJSONObject.length() > 0 ? URLEncoder.encode(localJSONObject.toString(), HTTP.UTF_8) : ""));


            sb.delete(0, sb.length() - 1);
            sb = null;
        } catch (Exception localJSONException) {
            localJSONObject = null;
            CommonUtil.printLog("OOAgent", "json error in emitCustomLogReport");
            CommonUtil.printLog(localJSONException);
        }
        return params;
    }

    /**
     * 获取错误信息的JSONObject对象
     *
     * @param error
     * @return
     */
    public static JSONObject getErrorInfoJSONObj(ErrorStaBean error) {
        JSONObject errorInfo = null;
        if (error == null) {
            return null;
        }
        try {
            errorInfo = new JSONObject();
            errorInfo.put("time", error.getTime());
            errorInfo.put("stacktrace", error.getStacktrace());
            errorInfo.put("activity", error.getActivity());

            errorInfo.put("os_version", error.getOs_version());
            errorInfo.put("version", error.getVersion());
            errorInfo.put("chennel", error.getChannel());

        } catch (JSONException e) {
            errorInfo = null;
            CommonUtil.printLog("OOAgent", "json error in emitCustomLogReport");
            CommonUtil.printLog(e);
        }
        return errorInfo;
    }

//    /**
//     * 获取错误信息的params-error
//     *
//     * @param error
//     * @return
//     */
//    public static List<NameValuePair> getErrorInfoParams(ErrorStaBean error) {
//
//        List<NameValuePair> params = null;
//
//        JSONObject errorInfo = null;
//        if (error == null) {
//            return null;
//        }
//        try {
//
//            params = new ArrayList<>();
//
//            params.add(new BasicNameValuePair("stack_trace", error.getStacktrace()));
//            params.add(new BasicNameValuePair("activity", error.getActivity()));
//            params.add(new BasicNameValuePair("os_version", error.getOs_version()));
//            params.add(new BasicNameValuePair("version", error.getVersion()));
//            params.add(new BasicNameValuePair("channel", error.getChannel()));
//            params.add(new BasicNameValuePair("time", error.getTime()));
//        } catch (Exception e) {
//            CommonUtil.printLog("OOAgent", "json error in emitCustomLogReport");
//            CommonUtil.printLog(e);
//        }
//        return params;
//    }
    /**
     * 获取错误信息的params-error
     *
     * @param error
     * @return
     */
    public static HashMap<String,String> getErrorInfoParams(ErrorStaBean error) {
        HashMap<String,String> params = null;
        if (error == null) {
            return null;
        }
        try {
            params = new HashMap<String,String>();
            params.put("stack_trace", error.getStacktrace());
            params.put("activity", error.getActivity());
            params.put("os_version", error.getOs_version());
            params.put("version", error.getVersion());
            params.put("channel", error.getChannel());
            params.put("time", error.getTime());
        } catch (Exception e) {
            CommonUtil.printLog("OOAgent", "json error in emitCustomLogReport");
            CommonUtil.printLog(e);
        }
        return params;
    }

    /**
     * 获取错误信息的Get请求的参数字符串；
     *
     * @param error
     * @return
     */
    public static String getErrorInfoGetParams(ErrorStaBean error) {
        String params = "";
        if (error == null) {
            return null;
        }
        try {
            StringBuffer sb = new StringBuffer();

            sb.append("?stacktrace=" + error.getStacktrace());
            sb.append("&activity=" + error.getActivity());
            sb.append("&os_version=" + error.getOs_version());
            sb.append("&channel=" + error.getChannel());
            sb.append("&time=" + error.getTime());
            params = sb.toString();

        } catch (Exception e) {
            CommonUtil.printLog("OOAgent", "json error in emitCustomLogReport");
            CommonUtil.printLog(e);
        }
        return params;
    }
}
