package com.app.duck.database;

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

import androidx.annotation.Nullable;

import java.util.HashMap;

public class DBHelper extends SQLiteOpenHelper {
    private static final String SQL_CREATE_TABLE =
            "CREATE TABLE " + DBContract.TABLE_NAME + " (" +
                    DBContract.COLUMN_ID + " INTEGER PRIMARY KEY," +
                    DBContract.COLUMN_SUCCESS + " INTEGER," +
                    DBContract.COLUMN_PROP1 + " TEXT," +
                    DBContract.COLUMN_PROP2 + " TEXT," +
                    DBContract.COLUMN_PROP3 + " TEXT," +
                    DBContract.COLUMN_PROP4 + " TEXT," +
                    DBContract.COLUMN_PROP5 + " TEXT," +
                    DBContract.COLUMN_PROP6 + " TEXT," +
                    DBContract.COLUMN_PROP7 + " TEXT," +
                    DBContract.COLUMN_PROP8 + " TEXT," +
                    DBContract.COLUMN_PROP9 + " TEXT" +
                    ")";

    public DBHelper(@Nullable Context context) {
        super(context, DBContract.DATABASE_NAME, null, DBContract.DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(SQL_CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + DBContract.TABLE_NAME);
        onCreate(sqLiteDatabase);
    }

    // 插入数据
    public long insertData(int id, int success) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(DBContract.COLUMN_ID, id);
        contentValues.put(DBContract.COLUMN_SUCCESS, success);
        long a = db.insert(DBContract.TABLE_NAME, null, contentValues);
        db.close();
        return a;
    }

    // 修改数据的方法
    public void updateData(int id, int success) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(DBContract.COLUMN_ID, id);
        contentValues.put(DBContract.COLUMN_SUCCESS, success);
        String whereClause = DBContract.COLUMN_ID + "=?";
        String[] whereArgs = {String.valueOf(id)};
        db.update(DBContract.TABLE_NAME, contentValues, whereClause, whereArgs);
        db.close();
    }

    public void insertOrUpdateData(int id, int success) {
        long _id;
        SQLiteDatabase db = getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(DBContract.COLUMN_ID, id);
        contentValues.put(DBContract.COLUMN_SUCCESS, success);
        // 先查询数据是否存在
        Cursor cursor = db.query(DBContract.TABLE_NAME, null, DBContract.COLUMN_ID + "=?", new String[]{String.valueOf(id)}, null, null, null);
        if (cursor.moveToFirst()) {
            // 如果有数据则执行更新操作
            db.update(DBContract.TABLE_NAME, contentValues, DBContract.COLUMN_ID + "=?", new String[]{String.valueOf(id)});
            _id = 0;
        } else {
            // 否则执行插入操作
            contentValues.put(DBContract.COLUMN_ID, id);
            _id = db.insert(DBContract.TABLE_NAME, null, contentValues);
        }
        cursor.close();
        db.close();
    }

    public int checkSuccessById(int id) {
        int status = 0;
        SQLiteDatabase db = this.getReadableDatabase();
        String[] columns = {DBContract.COLUMN_SUCCESS};
        String selection = DBContract.COLUMN_ID + "=?";
        String[] selectionArgs = {String.valueOf(id)};
        Cursor cursor = db.query(DBContract.TABLE_NAME, columns, selection, selectionArgs, null, null, null);
        if (cursor.moveToFirst()) {
            int columnIndex = cursor.getColumnIndex(DBContract.COLUMN_SUCCESS);
            status = cursor.getInt(columnIndex);
        }
        cursor.close();
        db.close();
        return status;
    }
}
