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 com.utopiaindustries.qualitychecker.models.InspectionCheckpointSku;
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 InspectionCheckpointSkuRepository {

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

    public InspectionCheckpointSkuRepository( Context context ) {
        dbHelper = new DatabaseHelper ( context );
        database = dbHelper.getWritableDatabase();
        executorService = Executors.newSingleThreadExecutor();
    }
    public void insert( List<InspectionCheckpointSku> checkPoints ) {
        executorService.execute(() -> {
            database.beginTransaction();
            try {
                for ( InspectionCheckpointSku checkPoint : checkPoints ) {
                    ContentValues values = new ContentValues();
                    values.put(DatabaseHelper.CHECKPOINT_SKU_SKU, checkPoint.getSku() );
                    values.put(DatabaseHelper.CHECKPOINT_SKU_MARKETPLACE, checkPoint.getMarketplace() );
                    values.put(DatabaseHelper.CHECKPOINT_SKU_CHECKPOINT_ID, checkPoint.getCheckpointId() );
                    values.put(DatabaseHelper.CHECKPOINT_SKU_STANDARD, checkPoint.getStandard() );
                    database.insertWithOnConflict(DatabaseHelper.CHECKPOINT_SKU_TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_REPLACE);
                }
                database.setTransactionSuccessful();
            } finally {
                database.endTransaction();
                close();
            }
        });
    }

    @SuppressLint("Range")
    public List<InspectionCheckpointSku> findBySkuAndMarketplace(String sku, String marketplace ){

        String selection = DatabaseHelper.CHECKPOINT_SKU_SKU + "=? AND " + DatabaseHelper.CHECKPOINT_SKU_MARKETPLACE + "=?";
        String[] selectionArgs = {sku,marketplace};

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

        List<InspectionCheckpointSku> checkpointSkus = new ArrayList<>();
        if ( cursor != null && cursor.moveToFirst() ) {
            do {
                InspectionCheckpointSku checkpointSku = new InspectionCheckpointSku();
                checkpointSku.setSku( cursor.getString( cursor.getColumnIndex( DatabaseHelper.CHECKPOINT_SKU_SKU )));
                checkpointSku.setMarketplace( cursor.getString( cursor.getColumnIndex( DatabaseHelper.CHECKPOINT_SKU_MARKETPLACE )));
                checkpointSku.setCheckpointId( cursor.getLong(cursor.getColumnIndex( DatabaseHelper.CHECKPOINT_SKU_CHECKPOINT_ID )));
                checkpointSku.setStandard( cursor.getString(cursor.getColumnIndex(DatabaseHelper.CHECKPOINT_SKU_STANDARD )));
                checkpointSkus.add( checkpointSku );
            } while ( cursor.moveToNext() );
            cursor.close();
        }
        close();
        return checkpointSkus;
    }


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

        List<InspectionCheckpointSku> checkPoints = new ArrayList<>();
        if (cursor != null && cursor.moveToFirst()) {
            do {
                InspectionCheckpointSku checkPoint = new InspectionCheckpointSku();
                checkPoint.setSku(cursor.getString( cursor.getColumnIndex(DatabaseHelper.CHECKPOINT_SKU_SKU )));
                checkPoint.setMarketplace( cursor.getString( cursor.getColumnIndex( DatabaseHelper.CHECKPOINT_SKU_MARKETPLACE )));
                checkPoint.setCheckpointId(cursor.getLong(cursor.getColumnIndex(DatabaseHelper.CHECKPOINT_SKU_CHECKPOINT_ID) ));
                checkPoint.setStandard(cursor.getString(cursor.getColumnIndex(DatabaseHelper.CHECKPOINT_SKU_STANDARD )));
                checkPoints.add( checkPoint );
            } while ( cursor.moveToNext() );
            cursor.close();
        }
        close();
        return checkPoints;
    }

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