package com.satsumasoftware.timetable.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.provider.BaseColumns;

import com.satsumasoftware.timetable.framework.ClassDetail;
import com.satsumasoftware.timetable.framework.ClassTime;

import java.util.ArrayList;

public final class ClassesUtils {

    public static ArrayList<Integer> getClassDetailIds(Context context, int classId) {
        TimetableDbHelper dbHelper = TimetableDbHelper.getInstance(context);

        Cursor cursor = dbHelper.getReadableDatabase().query(
                ClassDetailsMapSchema.TABLE_NAME,
                null,
                ClassDetailsMapSchema.COL_CLASS_ID + "=?",
                new String[] {String.valueOf(classId)},
                null, null, null);
        cursor.moveToFirst();

        ArrayList<Integer> classDetailIds = new ArrayList<>();
        while (!cursor.isAfterLast()) {
            classDetailIds.add(cursor.getInt(
                    cursor.getColumnIndex(ClassDetailsMapSchema.COL_CLASS_DETAIL_ID)));
            cursor.moveToNext();
        }
        cursor.close();

        return classDetailIds;
    }

    public static ArrayList<ClassDetail> getClassDetailsFromIds(Context context, ArrayList<Integer> classDetailIds) {
        TimetableDbHelper dbHelper = TimetableDbHelper.getInstance(context);
        ArrayList<ClassDetail> classDetails = new ArrayList<>();

        for (int classDetailId : classDetailIds) {
            Cursor cursor = dbHelper.getReadableDatabase().query(
                    ClassDetailsSchema.TABLE_NAME,
                    null,
                    ClassDetailsSchema._ID + "=?",
                    new String[]{String.valueOf(classDetailId)},
                    null, null, null);
            cursor.moveToFirst();
            String room = cursor.getString(cursor.getColumnIndex(ClassDetailsSchema.COL_ROOM));
            String teacher = cursor.getString(cursor.getColumnIndex(ClassDetailsSchema.COL_TEACHER));
            ArrayList<Integer> classTimeIds = getClassTimeIds(context, classDetailId);
            classDetails.add(new ClassDetail(classDetailId, room, teacher, classTimeIds));
            cursor.close();
        }

        return classDetails;
    }

    public static ArrayList<Integer> getClassTimeIds(Context context, int classDetailId) {
        TimetableDbHelper dbHelper = TimetableDbHelper.getInstance(context);

        Cursor cursor = dbHelper.getReadableDatabase().query(
                ClassDetailTimesMapSchema.TABLE_NAME,
                null,
                ClassDetailTimesMapSchema.COL_CLASS_DETAIL_ID + "=?",
                new String[] {String.valueOf(classDetailId)},
                null, null, null);
        cursor.moveToFirst();

        ArrayList<Integer> classTimeIds = new ArrayList<>();
        while (!cursor.isAfterLast()) {
            classTimeIds.add(cursor.getInt(
                    cursor.getColumnIndex(ClassDetailTimesMapSchema.COL_CLASS_TIME_ID)));
            cursor.moveToNext();
        }
        cursor.close();

        return classTimeIds;
    }

    public static ArrayList<ClassTime> getClassTimesFromIds(Context context, ArrayList<Integer> classTimeIds) {
        TimetableDbHelper dbHelper = TimetableDbHelper.getInstance(context);
        ArrayList<ClassTime> classTimes = new ArrayList<>();

        for (int classTimeId : classTimeIds) {
            Cursor cursor = dbHelper.getReadableDatabase().query(
                    ClassTimesSchema.TABLE_NAME,
                    null,
                    ClassTimesSchema._ID,
                    new String[] {String.valueOf(classTimeId)},
                    null, null, null);
            cursor.moveToFirst();
            classTimes.add(new ClassTime(cursor));
            cursor.close();
        }

        return classTimes;
    }

    public static int getHighestClassTimeId(Context context) {
        SQLiteDatabase db = TimetableDbHelper.getInstance(context).getReadableDatabase();
        Cursor cursor = db.query(
                ClassTimesSchema.TABLE_NAME, null, null, null, null, null, null);
        int count = cursor.getCount();
        cursor.close();
        return count;
    }

    public static void addClassTime(Context context, ClassTime classTime) {
        ContentValues values = new ContentValues();
        values.put(ClassTimesSchema._ID, classTime.getId());
        values.put(ClassTimesSchema.COL_DAY, classTime.getDay().getValue());
        values.put(ClassTimesSchema.COL_START_TIME_HRS, classTime.getStartTime().getHour());
        values.put(ClassTimesSchema.COL_START_TIME_MINS, classTime.getStartTime().getMinute());
        values.put(ClassTimesSchema.COL_START_TIME_HRS, classTime.getEndTime().getHour());
        values.put(ClassTimesSchema.COL_START_TIME_MINS, classTime.getEndTime().getMinute());

        SQLiteDatabase db = TimetableDbHelper.getInstance(context).getWritableDatabase();
        db.insert(ClassTimesSchema.TABLE_NAME, null, values);
    }

    public static void deleteClassTime(Context context, int classTimeId) {
        SQLiteDatabase db = TimetableDbHelper.getInstance(context).getWritableDatabase();
        db.delete(ClassTimesSchema.TABLE_NAME,
                ClassTimesSchema._ID + "=?",
                new String[] {String.valueOf(classTimeId)});
    }

    public static void replaceClassTime(Context context, int oldClassTimeId, ClassTime newClassTime) {
        deleteClassTime(context, oldClassTimeId);
        addClassTime(context, newClassTime);
    }
}

@@ -188,45 +188,8 @@
 ase;
-%0Aimport android.provider.BaseColumns;
 %0A%0Aim
@@ -5562,11 +5562,1236 @@
 );%0A    %7D
+%0A%0A    public static void addClassDetailToTimesLinks(Context context, int classDetailId, ArrayList%3CInteger%3E classTimeIds) %7B%0A        for (int classTimeId : classTimeIds) %7B%0A            ContentValues values = new ContentValues();%0A            values.put(ClassDetailTimesMapSchema.COL_CLASS_DETAIL_ID, classDetailId);%0A            values.put(ClassDetailTimesMapSchema.COL_CLASS_TIME_ID, classTimeId);%0A%0A            SQLiteDatabase db = TimetableDbHelper.getInstance(context).getWritableDatabase();%0A            db.insert(ClassDetailTimesMapSchema.TABLE_NAME, null, values);%0A        %7D%0A    %7D%0A%0A    public static void deleteClassDetailToTimesLinks(Context context, int classDetailId) %7B%0A        SQLiteDatabase db = TimetableDbHelper.getInstance(context).getWritableDatabase();%0A        db.delete(ClassDetailTimesMapSchema.TABLE_NAME,%0A                ClassDetailTimesMapSchema.COL_CLASS_DETAIL_ID + %22=?%22,%0A                new String%5B%5D %7BString.valueOf(classDetailId)%7D);%0A    %7D%0A%0A    public static void replaceClassDetailToTimesLinks(Context context, int classDetailId, ArrayList%3CInteger%3E classTimeIds) %7B%0A        deleteClassDetailToTimesLinks(context, classDetailId);%0A        addClassDetailToTimesLinks(context, classDetailId, classTimeIds);%0A    %7D
 %0A%7D%0A

Create helper methods to add and remove db values mapping class detail and times