package com.mjsheng.myappstore.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;


import com.mjsheng.myappstore.comm.CommonDatas;
import com.mjsheng.myappstore.database.DBHelper;
import com.mjsheng.myappstore.database.DBSqlBuilder;

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

public class MySQLData {


    public static void ClearData(Context context) {
        DBHelper helper = new DBHelper(context);
        List<String> list = new ArrayList<>();
        Cursor cursor = null;
        try {
            String sql = "select * from " + DBSqlBuilder.APPLICATION_MESSAGE  + " order by _id asc";
            cursor = helper.getCursorBySql(sql);
            while (cursor.moveToNext()) {
                String key = cursor.getString(cursor.getColumnIndex("file_name"));
                    String where = " file_name='" + key + "'";
                    list.add(where);
            }
            if (cursor != null) {
                cursor.close();
            }
            helper.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        helper.deleteRow(DBSqlBuilder.APPLICATION_MESSAGE,list);
    }

    @SuppressWarnings("static-access")
    public static void SetData(Context context, String key,
                               String value) {
        DBHelper helper = new DBHelper(context);
        Cursor cursor = null;
        try {
            String sql = "select * from " + DBSqlBuilder.APPLICATION_MESSAGE + " order by _id asc";
            cursor = helper.getCursorBySql(sql);
            int count = 0;
            String file_name = null;
            if (cursor.moveToFirst()) {
                file_name = cursor.getString(cursor.getColumnIndex("file_name"));
                if (file_name.equals(key)){
                    count ++;
                }
            }
            ContentValues values = new ContentValues();
            if (count > 0) {
                values.put("key",value);
                helper.updateValues(DBSqlBuilder.DETAIL_LOG_TABLE, values, "file_name=" + key);
            }else {
                values.put("file_name", key);
                values.put("key",value);
                helper.insertValues(DBSqlBuilder.APPLICATION_MESSAGE, values);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    public static void SetIntData(Context context, String key,
                                  int value) {
        DBHelper helper = new DBHelper(context);
        Cursor cursor = null;
        try {
            String sql = "select * from " + DBSqlBuilder.APPLICATION_MESSAGE + " order by _id asc";
            cursor = helper.getCursorBySql(sql);
            int count = 0;
            String file_name = null;
            if (cursor.moveToFirst()) {
                file_name = cursor.getString(cursor.getColumnIndex("file_name"));
                if (file_name.equals(key)){
                    count ++;
                }
            }

            ContentValues values = new ContentValues();
            if (count > 0) {
                values.put("key", String.valueOf(value));
                helper.updateValues(DBSqlBuilder.DETAIL_LOG_TABLE, values, "file_name=" + key);
            }else {
                values.put("file_name", key);
                values.put("key", String.valueOf(value));
                helper.insertValues(DBSqlBuilder.APPLICATION_MESSAGE, values);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    public static void SetBooleanData(Context context, String key,
                                      boolean value) {
        DBHelper helper = new DBHelper(context);
        Cursor cursor = null;
        try {
            String sql = "select * from " + DBSqlBuilder.APPLICATION_MESSAGE + " order by _id asc";
            cursor = helper.getCursorBySql(sql);
            int count = 0;
            String file_name = null;
            if (cursor.moveToFirst()) {
                file_name = cursor.getString(cursor.getColumnIndex("file_name"));
                if (file_name.equals(key)){
                    count ++;
                }
            }

            ContentValues values = new ContentValues();
            if (count > 0) {
                values.put("key",value + "");
                helper.updateValues(DBSqlBuilder.DETAIL_LOG_TABLE, values, "file_name=" + key);
            }else {
                values.put("file_name", key);
                values.put("key",value + "");
                helper.insertValues(DBSqlBuilder.APPLICATION_MESSAGE, values);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    @SuppressWarnings("static-access")
    public static String GetData(Context context, String key) {
        String value = "";
        DBHelper helper = new DBHelper(context);
        Cursor cursor = null;
        try {
            String sql = "select * from " + DBSqlBuilder.APPLICATION_MESSAGE
                    + " where file_name='" + key + "' order by _id asc";
            cursor = helper.getCursorBySql(sql);
            while (cursor.moveToNext()) {
                String a = cursor.getString(cursor.getColumnIndex("file_name"));
                value = cursor.getString(cursor.getColumnIndex("key"));
            }
            if (cursor != null) {
                cursor.close();
            }
            helper.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        if (key.equals(CommonDatas.SP_PARENT_PASSWORD) && (value == null || value.equals(""))){
            value = "0000";
            SetData(context, CommonDatas.SP_PARENT_PASSWORD,"0000");
        }
        if (key.equals(CommonDatas.SP_BABY_AGE_KEY) && (value == null || value.equals(""))){
            value = "3";
            SetData(context,CommonDatas.SP_BABY_AGE_KEY,"3");
        }
        return value;
    }


    public static int GetIntData(Context context, String key) {
        int value = -1;
        String key_date = "";
        DBHelper helper = new DBHelper(context);
        Cursor cursor = null;

        List<String> list = new ArrayList<>();
        try {
            String sql = "select * from " + DBSqlBuilder.APPLICATION_MESSAGE
                    + " where file_name='" + key + "' order by _id asc";
            cursor = helper.getCursorBySql(sql);
            while (cursor.moveToNext()) {
                key_date = cursor.getString(cursor.getColumnIndex("key"));
                list.add(key_date);
            }
            if (cursor != null) {
                cursor.close();
            }
            helper.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        if (key_date != null && !key_date.equals("")){
            value = Integer.parseInt(key_date.trim());
        }
        return value;
    }

    public static boolean GetBooleanData(Context context, String key) {
        boolean value = false;
        String key_date = null;
        DBHelper helper = new DBHelper(context);
        Cursor cursor = null;
        try {
            String sql = "select * from " + DBSqlBuilder.APPLICATION_MESSAGE
                    + " where file_name='" + key + "' order by _id asc";
            cursor = helper.getCursorBySql(sql);
            while (cursor.moveToNext()) {
                key_date = cursor.getString(cursor.getColumnIndex("key"));
            }
            if (cursor != null) {
                cursor.close();
            }
            helper.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        if (key_date != null && !key_date.equals("")){
            value = Boolean.parseBoolean(key_date.trim());
        }
        return value;
    }


}
