/**
 * Cobub Razor
 * <p>
 * An open source analytics android sdk for mobile applications
 *
 * @package Cobub Razor
 * @author WBTECH Dev Team
 * @copyright Copyright (c) 2011 - 2012, NanJing Western Bridge Co.,Ltd.
 * @license http://www.cobub.com/products/cobub-razor/license
 * @link http://www.cobub.com/products/cobub-razor/
 * @filesource
 * @since Version 0.1
 */
package com.oo.seex.oo_statistics_lib.dao;

import android.content.Context;

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.constant.OOConstant;
import com.oo.seex.oo_statistics_lib.getjsonobject.GetJson;
import com.oo.seex.oo_statistics_lib.network.NetworkUitlity;
import com.oo.seex.oo_statistics_lib.utils.CommonUtil;
import com.oo.seex.oo_statistics_lib.utils.MyDbUtils;
import com.oo.seex.oo_statistics_lib.utils.xutils_dbutils.exception.DbException;

import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 获取存储的信息并且发送到服务端 如果发送成功那么极度删除调本地存储
 */
public class UploadNativeData{

    private static final String TAG = "UploadNativeData";

    private static UploadNativeData instance = new UploadNativeData();

    public Context context;

    private Timer mTimer;

    private UploadTimerTask mTimerTask;

    private UploadNativeData() {

    }

    public static UploadNativeData getInstance(){
        return instance;
    }

    public void synNativeData(Context context){
        this.context = context;
        close();
        try {
            if (mTimer == null) {
                mTimer = new Timer();
            }
            if (mTimerTask == null) {
                mTimerTask = new UploadTimerTask();
            }
            mTimer.schedule(mTimerTask, 0, 10 * 60 * 1000);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void close(){
        try {
            if (mTimer != null) {
                mTimer.purge();
                mTimer.cancel();
                mTimer = null;
            }
            if(mTimerTask != null){
                mTimerTask.cancel();
                mTimerTask = null;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private class UploadTimerTask extends TimerTask {
        @Override
        public void run() {
            try {
                if (CommonUtil.isNetworkAvailable(context)) {
                    //activity data
                    List<ActivityStaBean> activityInfo = MyDbUtils.getInstance(context).getActivityStatistics();
                    if (activityInfo != null && activityInfo.size() > 0) {
                        CommonUtil.printLog(TAG,"activity:"+activityInfo.size());
                        for (int i = 0; i < activityInfo.size(); i++) {
                            final ActivityStaBean bean = activityInfo.get(i);
                            HashMap<String, String> info = GetJson.getActivityParams(bean);
                            if (info != null) {
                                NetworkUitlity.getInstance().requestPost(OOConstant.preUrl + OOConstant.activityUrl, info, context,
                                        new NetworkUitlity.NetListener() {
                                            @Override
                                            public void succee() {
                                                MyDbUtils.getInstance(context).deletActivitySta(bean);
                                            }

                                            @Override
                                            public void error() {
                                            }
                                        });

                            }
                        }
                    }

                    //errorInfo
                    List<ErrorStaBean> errors = MyDbUtils.getInstance(context).getErrorStatistics();
                    if (errors != null && errors.size() > 0) {
                        CommonUtil.printLog(TAG,"error:"+errors.size());
                        for (int i = 0; i < errors.size(); i++) {
                            final ErrorStaBean bean = errors.get(i);
                            HashMap<String, String> info = GetJson.getErrorInfoParams(bean);
                            if (info != null) {
                                NetworkUitlity.getInstance().requestPost(OOConstant.preUrl + OOConstant.errorUrl, info, context,
                                        new NetworkUitlity.NetListener() {
                                            @Override
                                            public void succee() {
                                                MyDbUtils.getInstance(context).deletErrorSta(bean);
                                            }

                                            @Override
                                            public void error() {
                                            }
                                        });
                            }
                        }
                    }

                    //event
                    List<EventStaBean> events = MyDbUtils.getInstance(context).getEventStatistics();
                    if (events != null && events.size() > 0) {
                        CommonUtil.printLog(TAG,"event:"+events.size());
                        for (int i = 0; i < events.size(); i++) {
                            final EventStaBean bean = events.get(i);
                            HashMap infoParams = GetJson.getEventParams(bean);
                            if (infoParams != null) {
                                NetworkUitlity.getInstance().requestPost(OOConstant.preUrl + OOConstant.eventUrl, infoParams, context,
                                        new NetworkUitlity.NetListener() {
                                            @Override
                                            public void succee() {
                                                MyDbUtils.getInstance(context).deletEventSta(bean);
                                            }

                                            @Override
                                            public void error() {
                                            }
                                        });
                            }
                        }
                    }

                    //fragment data
                    List<FragmentBean> fragmentBeans = MyDbUtils.getInstance(context).getFragmentStatistics();
                    if (fragmentBeans != null && fragmentBeans.size() > 0) {
                        CommonUtil.printLog(TAG,"fragment:"+fragmentBeans.size());
                        for (int i = 0; i < fragmentBeans.size(); i++) {
                            final FragmentBean fragmentBean = fragmentBeans.get(i);
                            HashMap<String, String> infoParams = GetJson.getFragmentParams(fragmentBean);
                            if (infoParams != null) {
                                NetworkUitlity.getInstance().requestPost(OOConstant.preUrl + OOConstant.fragmentUrl,
                                        infoParams, context,
                                        new NetworkUitlity.NetListener() {
                                            @Override
                                            public void succee() {
                                                MyDbUtils.getInstance(context).deletFragmentSta(fragmentBean);
                                            }

                                            @Override
                                            public void error() {
                                            }
                                        });//访问网络
                            }
                        }
                    }
                }
            } catch (DbException e) {
                CommonUtil.printLog("OOAgent", "get Cache For ShiNcDb error");
                CommonUtil.printLog(e);
            }
        }
    }
}
