package com.utopiaindustries.qualitychecker.db;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.utopiaindustries.qualitychecker.models.InspectionCheckPoint;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CheckpointRepository {

    private final DatabaseHelper dbHelper;
    private final SQLiteDatabase database;
    private final ExecutorService executorService;

    public CheckpointRepository( Context context ) {
        dbHelper = new DatabaseHelper ( context );
        database = dbHelper.getWritableDatabase();
        executorService = Executors.newSingleThreadExecutor();
    }

    /*
     *   Insert list of checkpoints in batch
     * */
    public void insert( List<InspectionCheckPoint> checkPoints ) {
        executorService.execute(() -> {
            database.beginTransaction();
            try {
                for ( InspectionCheckPoint checkPoint : checkPoints ) {
                    ContentValues values = new ContentValues();
                    values.put(DatabaseHelper.CHECKPOINT_COLUMN_ID, checkPoint.getId() );
                    values.put(DatabaseHelper.CHECKPOINT_COLUMN_TITLE, checkPoint.getTitle() );
                    values.put(DatabaseHelper.CHECKPOINT_COLUMN_CATEGORY, checkPoint.getCategory() );
                    database.insertWithOnConflict(DatabaseHelper.CHECKPOINT_TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_REPLACE);
                }
                database.setTransactionSuccessful();
            } finally {
                database.endTransaction();
                close();
            }
        });
    }


    @SuppressLint("Range")
    public List<InspectionCheckPoint> findAll( ) {
        Cursor cursor = database.query( DatabaseHelper.CHECKPOINT_TABLE_NAME,
                null,
               null,
                null,
                null,
                null,
                null);

        List<InspectionCheckPoint> checkPoints = new ArrayList<>();
        if (cursor != null && cursor.moveToFirst()) {
            do {
                InspectionCheckPoint checkPoint = new InspectionCheckPoint();
                checkPoint.setId(cursor.getLong( cursor.getColumnIndex(DatabaseHelper.CHECKPOINT_COLUMN_ID )));
                checkPoint.setTitle(cursor.getString(cursor.getColumnIndex(DatabaseHelper.CHECKPOINT_COLUMN_TITLE) ));
                checkPoint.setCategory(cursor.getString(cursor.getColumnIndex(DatabaseHelper.CHECKPOINT_COLUMN_CATEGORY )));
                checkPoints.add( checkPoint );
            } while ( cursor.moveToNext() );
            cursor.close();
        }
        close();
        return checkPoints;
    }

    /*
     *  Close the database
     */
    public void close() {
        dbHelper.close();
        executorService.shutdown();
    }

}
