
package com.oo.seex.oo_statistics_lib.network;

import android.content.Context;
import android.os.Handler;

import com.oo.seex.oo_statistics_lib.bean.EventStaBean;
import com.oo.seex.oo_statistics_lib.bean.StatistivsBean;
import com.oo.seex.oo_statistics_lib.constant.OOConstant;
import com.oo.seex.oo_statistics_lib.getjsonobject.GetJson;
import com.oo.seex.oo_statistics_lib.utils.CommonUtil;

/**
 * 自定义统计事件的控制器
 */
public class EventController {
//    static final String EVENTURL =ShiNcConstant.eventUrl;

    /**
     * 将自定义统计事件发送出去
     *
     * @param handler
     * @param context
     * @param event
     * @return
     */
    public static boolean postEventInfo(final Handler handler, final Context context, EventStaBean event) {
        try {
            if (!event.verification()) {
                CommonUtil.printLog("OOAgent", "Illegal value of acc in postEventInfo");
                return false;
            }
            final StatistivsBean statistivsBean = new StatistivsBean();
            statistivsBean.setEventInfo(event);

            if (CommonUtil.isNetworkAvailable(context)) {
                NetworkUitlity.getInstance().requestPost(OOConstant.preUrl + OOConstant.eventUrl, GetJson.getEventParams(event), context,
                        new NetworkUitlity.NetListener() {
                            @Override
                            public void succee() {}

                            @Override
                            public void error() {
                                if(handler != null) {
                                    CommonUtil.saveInfoToFile(handler, "eventInfo", statistivsBean, context);
                                }
                            }
                        });
            } else {
                if(handler != null) {
                    CommonUtil.saveInfoToFile(handler, "eventInfo", statistivsBean, context);
                }
            }
        } catch (Exception e) {
            CommonUtil.printLog("OOAgent_error", "Exception occurred in postEventInfo()");
            CommonUtil.printLog(e);
            return false;
        }
        return true;
    }
}
