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.InspectionDimension;

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

public class DimensionRepository {

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

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

    /*
     *   Insert list of dimension in batch
     * */
    public void insert( List<InspectionDimension> dimensions) {
        executorService.execute(() -> {
            database.beginTransaction();
            try {
                for ( InspectionDimension dimension : dimensions ) {
                    ContentValues values = new ContentValues();
                    values.put(DatabaseHelper.DIMENSION_COLUMN_ID, dimension.getId() );
                    values.put(DatabaseHelper.DIMENSION_COLUMN_TITLE, dimension.getTitle() );
                    values.put(DatabaseHelper.DIMENSION_COLUMN_CATEGORY, dimension.getCategory() );
                    database.insertWithOnConflict(DatabaseHelper.INSPECTION_DIMENSION_TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_REPLACE);
                }
                database.setTransactionSuccessful();
            } finally {
                database.endTransaction();
                close();
            }
        });
    }


    @SuppressLint("Range")
    public List<InspectionDimension> findByCategory(String category) {

        if( category == null ){
            category = "OTHER";
        }

        String selection = DatabaseHelper.DIMENSION_COLUMN_CATEGORY + "=?";
        String[] selectionArgs = {category};

        Cursor cursor = database.query(DatabaseHelper.INSPECTION_DIMENSION_TABLE_NAME,
                null,
                selection,
                selectionArgs,
                null,
                null,
                null);

        List<InspectionDimension> dimensions = new ArrayList<>();
        if ( cursor != null && cursor.moveToFirst() ) {
            do {
                InspectionDimension dimension = new InspectionDimension();
                dimension.setId(cursor.getLong( cursor.getColumnIndex(DatabaseHelper.DIMENSION_COLUMN_ID )));
                dimension.setTitle(cursor.getString(cursor.getColumnIndex(DatabaseHelper.DIMENSION_COLUMN_TITLE)));
                dimension.setCategory(cursor.getString(cursor.getColumnIndex(DatabaseHelper.DIMENSION_COLUMN_CATEGORY)));
                dimensions.add( dimension );
            } while ( cursor.moveToNext() );
            cursor.close();
        }
        //
        if ( dimensions.isEmpty() && !"OTHER".equalsIgnoreCase( category ) ) {
            return findByCategory( "OTHER" );
        }
        close();
        return dimensions;
    }

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

}
