package com.demo.mylibrary.utils;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.demo.mylibrary.DatabaseHelper;
import com.demo.mylibrary.bean.BehaviorInfo;
import com.demo.mylibrary.bean.PageInfo;

import java.util.ArrayList;
import java.util.List;

public class DBUtils {

    /**
     * 查询PageInfo表
     *
     * @param dbHelper
     * @return
     */
    public static List<PageInfo> queryPageTable(DatabaseHelper dbHelper) {
        List<PageInfo> pageInfoList = new ArrayList<>();
        SQLiteDatabase dbQuery = dbHelper.getReadableDatabase();
        Cursor cursor = dbQuery.query("Page", (String[]) null, (String) null, (String[]) null, (String) null, (String) null, (String) null);
        if (cursor.moveToFirst()) {
            do {
                String userId = cursor.getString(1);
                String pageId = cursor.getString(2);
                String pageStatus = cursor.getString(3);
                String previous = cursor.getString(4);
                String funnelId = cursor.getString(5);
                String beginTime = cursor.getString(6);
                String endTime = cursor.getString(7);
                PageInfo page = new PageInfo(userId, pageId, pageStatus, previous, funnelId, beginTime, endTime);
                pageInfoList.add(page);
            } while (cursor.moveToNext());
        }
        cursor.close();
        return pageInfoList;
    }

    /**
     * 插入数据到PageInfo表中
     *
     * @param dbHelper
     * @param user
     * @param pageId
     * @param previous
     * @param pageStatus
     * @param funnelId
     * @param startTime
     * @param endTime
     */
    public static void insertPageTable(DatabaseHelper dbHelper,String userId, String pageId, String previous,
                                        String pageStatus, String funnelId, long startTime, long endTime) {
        SQLiteDatabase dbAdd = dbHelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("userId", userId);
        contentValues.put("pageId", pageId);
        contentValues.put("PageStatus", pageStatus);
        contentValues.put("Previous", previous);
        contentValues.put("funnelId", funnelId);
        contentValues.put("BeginTime", String.valueOf(startTime));
        contentValues.put("EndTime", String.valueOf(endTime));
        dbAdd.insert("Page", (String) null, contentValues);
    }

    /**
     * 删除缓存
     *
     * @param dbHelper
     */
    public static void deletePageCache(DatabaseHelper dbHelper) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete("Page", "1", (String[]) null);
    }

    /**
     * 插入数据到BehaviorInfo表中
     *
     * @param dbHelper
     * @param userId
     * @param eventId
     * @param value
     */
    public static void insertBehaviorTable(DatabaseHelper dbHelper, String userId,int eventId, String value) {
        SQLiteDatabase dbAdd = dbHelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("userId", userId);
        contentValues.put("eventId", eventId);
        contentValues.put("Value", value);
        contentValues.put("BeginTime", "");
        contentValues.put("EndTime", "");
        dbAdd.insert("Behavior", (String) null, contentValues);
    }

    /**
     * 查询Behavior表
     *
     * @param dbHelper
     * @return
     */
    public static List<BehaviorInfo> queryBehaviorTable(DatabaseHelper dbHelper) {
        List<BehaviorInfo> behaviors = new ArrayList<>();
        SQLiteDatabase dbQuery = dbHelper.getReadableDatabase();
        Cursor cursor = dbQuery.query("Behavior", (String[]) null, (String) null, (String[]) null, (String) null, (String) null, (String) null);
        if (cursor.moveToFirst()) {
            do {
                String userId = cursor.getString(1);
                int eventId = cursor.getInt(2);
                String value = cursor.getString(3);
                String beginTime = cursor.getString(4);
                String endTime = cursor.getString(5);
                BehaviorInfo behavior = new BehaviorInfo(userId, eventId, value, beginTime, endTime);
                behaviors.add(behavior);
            } while (cursor.moveToNext());
        }
        cursor.close();
        return behaviors;
    }

    /**
     * 上传缓存BehaviorInfo数据，删除缓存
     *
     * @param dbHelper
     */
    public static void deleteBehaviorCache(DatabaseHelper dbHelper) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete("Behavior", "1", (String[]) null);
    }

}
