package com.example.map.database;

import android.annotation.SuppressLint;
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 com.example.map.enity.Class_demo;

import java.util.ArrayList;

public class ClassDBHelper extends SQLiteOpenHelper {

    private  static final String DB_NAME="class.db";
    private  static final String ClassTABLE_NAME="class_table";
    private  static final  int DB_VERSION=1;
    private  static ClassDBHelper mHelper = null;
    private SQLiteDatabase mRDB=null;//用来读
    private  SQLiteDatabase mWDB=null;//用来写
    public SQLiteDatabase db;

    //参数为上下文context，所谓上下文就是这个类所在包的路径
    public ClassDBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        db = getReadableDatabase();
    }


    public ClassDBHelper(@Nullable Context context,
                         @Nullable String name,
                         @Nullable SQLiteDatabase.CursorFactory factory,
                         int version) {

        super(context, name, factory, version);
    }

    //利用单例模式获取数据库帮助器的唯一实例
    public static ClassDBHelper getInstance(Context context){
        if(mHelper==null){
            mHelper=new ClassDBHelper(context);
        }
        return  mHelper;
    }

    //打开数据库的读连接
    public SQLiteDatabase openReadLink(){
        if (mRDB==null || !mRDB.isOpen()){
            mRDB= mHelper.getReadableDatabase();
        }
        return mRDB;
    }

    //打开数据库的写连接
    public SQLiteDatabase openWriteLink(){
        if (mWDB==null || !mWDB.isOpen()){
            mWDB= mHelper.getWritableDatabase();
        }
        return mWDB;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        String sql="CREATE TABLE IF NOT EXISTS "+ ClassTABLE_NAME + " (" +
                "c_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"+
                " c_subject VARCHAR NOT NULL,"+
                " c_time VARCHAR NOT NULL,"+
                " c_day VARCHAR NOT NULL,"+
                " c_teacher VARCHAR NOT NULL);";

        ContentValues values=new ContentValues();
        values.put("c_subject","0");
        values.put("c_time","0");
        values.put("c_day","0");
        values.put("c_teacher","0");
        //插入id 1-36 个课程数据，以便添加课程
        for (int i = 1; i < 37; i++) {
            values.put("c_id", i);
            db.insert(ClassTABLE_NAME, "", values);
        }

        db.execSQL(sql);
    }

    public long insert(Class_demo classes){
        ContentValues values=new ContentValues();
        values.put("c_subject",classes.getC_subject());
        values.put("c_time",classes.getC_time());
        values.put("c_day",classes.getC_day());
        values.put("c_teacher",classes.getC_teacher());
        return mWDB.insert(ClassTABLE_NAME,null,values);
    }

    public long delete(Class_demo classes){
        String subject=classes.getC_subject();
        String teacher=classes.getC_teacher();
        return mWDB.delete(ClassTABLE_NAME,"c_subject=? and c_teacher=?",new String[]{subject,teacher});
    }
    public ArrayList<Class_demo> getAllData(){
        ArrayList<Class_demo> list = new ArrayList<>();
        @SuppressLint("Recycle") Cursor cursor = db.query(ClassTABLE_NAME,null,null,null,null,null,null);
        while(cursor.moveToNext()){
            @SuppressLint("Range") String c_subject = cursor.getString(cursor.getColumnIndex("c_subject"));
            @SuppressLint("Range") String c_time = cursor.getString(cursor.getColumnIndex("c_time"));
            @SuppressLint("Range") String c_day = cursor.getString(cursor.getColumnIndex("c_day"));
            @SuppressLint("Range") String c_teacher = cursor.getString(cursor.getColumnIndex("c_teacher"));
            list.add(new Class_demo(c_subject,c_time,c_day,c_teacher));
        }
        return list;
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

}