package com.oo.seex.oo_statistics_lib.utils;

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.bean.PostClientBean;
import com.oo.seex.oo_statistics_lib.utils.xutils_dbutils.DbUtils;
import com.oo.seex.oo_statistics_lib.utils.xutils_dbutils.exception.DbException;

import java.util.List;

/**
 * Created by efort on 15/7/31.
 */
public class MyDbUtils {
    private DbUtils dbUtils;
    private static MyDbUtils myDbUtils;
    private static int i;

    private MyDbUtils(Context context) {
        try {
            DbUtils.DaoConfig config = new DbUtils.DaoConfig(context);
            config.setDbDir(context.getApplicationContext().getCacheDir().getPath() + "/db");
            config.setDbName("shinc_statistics_db"); //db名
            config.setDbVersion(i);  //db版本

            dbUtils = DbUtils.create(config);//db还有其他的一些构造方法，比如含有更新表版本的监听器的

            dbUtils.createTableIfNotExist(PostClientBean.class);
            dbUtils.createTableIfNotExist(ErrorStaBean.class);
            dbUtils.createTableIfNotExist(ActivityStaBean.class);
            dbUtils.createTableIfNotExist(FragmentBean.class);
            dbUtils.createTableIfNotExist(EventStaBean.class);
        } catch (DbException e) {
            CommonUtil.printLog(e);
        }
    }

    public void clearDb(Context context) {
        try {
            dbUtils.dropDb();
            i++;
            myDbUtils = null;
        } catch (DbException e) {
            CommonUtil.printLog(e);
        }
    }

    public static MyDbUtils getInstance(Context context) {
        if (myDbUtils == null) {
            myDbUtils = new MyDbUtils(context);
        }
        return myDbUtils;
    }

    public void saveStatistics(Context context, ErrorStaBean errorStaBean) throws DbException {
        if (errorStaBean != null)
            dbUtils.saveOrUpdate(errorStaBean);
    }

    public void saveStatistics(Context context, ActivityStaBean activityStaBean) throws DbException {
        if (activityStaBean != null)

            dbUtils.saveBindingId(activityStaBean);
    }

    public void saveStatistics(Context context, FragmentBean fragmentBean) throws DbException {
        if (fragmentBean != null)

            dbUtils.saveOrUpdate(fragmentBean);
    }

    public void saveStatistics(Context context, EventStaBean eventStaBean) throws DbException {
        if (eventStaBean != null)

            dbUtils.saveOrUpdate(eventStaBean);
    }

    public void saveStatistics(Context context, PostClientBean postClientBean) throws DbException {
        if (postClientBean != null)

            dbUtils.saveOrUpdate(postClientBean);
    }

    public List<ErrorStaBean> getErrorStatistics() throws DbException {
        if (!dbUtils.tableIsExist(ErrorStaBean.class)) {
            return null;
        }
        List<ErrorStaBean> errorStaBeanList = dbUtils.findAll(ErrorStaBean.class);
        return errorStaBeanList;
    }

    public List<ActivityStaBean> getActivityStatistics() throws DbException {
        if (!dbUtils.tableIsExist(ActivityStaBean.class)) {
//                Log.e("save", "saveFailed");
            return null;
        }
        List<ActivityStaBean> activityStaBeanList = dbUtils.findAll(ActivityStaBean.class);
        return activityStaBeanList;
    }

    public List<FragmentBean> getFragmentStatistics() throws DbException {
        if (!dbUtils.tableIsExist(FragmentBean.class)) {
            return null;
        }
        List<FragmentBean> fragmentBeanList = dbUtils.findAll(FragmentBean.class);
        return fragmentBeanList;
    }

    public List<EventStaBean> getEventStatistics() throws DbException {
        if (!dbUtils.tableIsExist(EventStaBean.class)) {
            return null;
        }
        List<EventStaBean> eventStaBeanList = dbUtils.findAll(EventStaBean.class);
        return eventStaBeanList;
    }

    public List<PostClientBean> getPostClientStatistics() throws DbException {
        if (!dbUtils.tableIsExist(PostClientBean.class)) {
            return null;
        }
        List<PostClientBean> postClientBeanList = dbUtils.findAll(PostClientBean.class);
        return postClientBeanList;
    }

    public void deletPostClient(PostClientBean client){
        try{
            if (client != null)
                dbUtils.delete(client);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void deletActivitySta(ActivityStaBean activityStaBean) {
        try {
            if (activityStaBean != null)
                dbUtils.delete(activityStaBean);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void deletErrorSta(ErrorStaBean errorStaBean){
        try {
            if (errorStaBean != null)
                dbUtils.delete(errorStaBean);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void deletEventSta(EventStaBean eventStaBean) {
        try {
            if (eventStaBean != null)
                dbUtils.delete(eventStaBean);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void deletFragmentSta(FragmentBean fragmentBean){
        try {
            if (fragmentBean != null)
                dbUtils.delete(fragmentBean);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
