package com.utopiaindustries.qualitychecker.db;

import static com.utopiaindustries.qualitychecker.db.DatabaseHelper.INSPECTION_LABEL_COLUMN_ID;
import static com.utopiaindustries.qualitychecker.db.DatabaseHelper.PRODUCT_COLUMN_ASIN;
import static com.utopiaindustries.qualitychecker.db.DatabaseHelper.QUALITY_LABEL_COLUMN_ID;

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

import com.utopiaindustries.qualitychecker.models.InspectionLabel;
import com.utopiaindustries.qualitychecker.models.Product;
import com.utopiaindustries.qualitychecker.models.QualityLabel;

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

public class InspectionLabelRepository {
    private final DatabaseHelper dbHelper;
    private final SQLiteDatabase database;
    private final ExecutorService executorService;


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

    /*
     *   Insert list of inspection labels in batch
     * */
    public void insertInspectionLabels( List<InspectionLabel> inspectionLabels, List<QualityLabel> qualityLabels) {
        executorService.execute(() -> {
            database.beginTransaction();
            try {
                //Inspection Label
                for (InspectionLabel inspectionLabel : inspectionLabels) {
                    ContentValues ivalues = new ContentValues();
                    ivalues.put(INSPECTION_LABEL_COLUMN_ID, inspectionLabel.getId());
                    ivalues.put(DatabaseHelper.INSPECTION_LABEL_COLUMN_MIN_LOT_SIZE, inspectionLabel.getMinLotSize());
                    ivalues.put(DatabaseHelper.INSPECTION_LABEL_COLUMN_MAX_LOT_SIZE, inspectionLabel.getMaxLotSize());
                    ivalues.put(DatabaseHelper.INSPECTION_LABEL_COLUMN_INSPECTION_LEVEL, inspectionLabel.getInspectionLevel());
                    ivalues.put(DatabaseHelper.INSPECTION_LABEL_COLUMN_SAMPLE_CODE, inspectionLabel.getSampleCode());

                    database.insertWithOnConflict(DatabaseHelper.INSPECTION_LABEL_TABLE_NAME, null, ivalues, SQLiteDatabase.CONFLICT_REPLACE);
                }

                //Quality Label
                for (QualityLabel qualityLabel : qualityLabels) {
                    ContentValues qvalues = new ContentValues();
                    qvalues.put(QUALITY_LABEL_COLUMN_ID, qualityLabel.getId());
                    qvalues.put(DatabaseHelper.QUALITY_LABEL_COLUMN_SAMPLE_CODE, qualityLabel.getSampleCode());
                    qvalues.put(DatabaseHelper.QUALITY_LABEL_COLUMN_SAMPLE_SIZE, qualityLabel.getSampleSize());
                    qvalues.put(DatabaseHelper.QUALITY_LABEL_COLUMN_QUALITY_LEVEL, qualityLabel.getQualityLevel());
                    qvalues.put(DatabaseHelper.QUALITY_LABEL_COLUMN_NO_ACCEPTED_DEFECTS, qualityLabel.getNoOfAcceptedDefects());

                    database.insertWithOnConflict(DatabaseHelper.QUALITY_LABEL_TABLE_NAME, null, qvalues, SQLiteDatabase.CONFLICT_REPLACE);
                }

                database.setTransactionSuccessful();
            } finally {
                database.endTransaction();
                close();
            }
        });
    }


    /*
     *  Get list of inspection labels from database
     * */
    public List<InspectionLabel> getInspectionLabels() {
        List<InspectionLabel> inspectionLabels = new ArrayList<>();
        Cursor cursor = database.query(DatabaseHelper.INSPECTION_LABEL_TABLE_NAME, new String[]{INSPECTION_LABEL_COLUMN_ID,
                        DatabaseHelper.INSPECTION_LABEL_COLUMN_MIN_LOT_SIZE, DatabaseHelper.INSPECTION_LABEL_COLUMN_MAX_LOT_SIZE,
                        DatabaseHelper.INSPECTION_LABEL_COLUMN_INSPECTION_LEVEL, DatabaseHelper.INSPECTION_LABEL_COLUMN_SAMPLE_CODE},
                null, null, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            do {
                InspectionLabel inspectionLabel = new InspectionLabel();
                inspectionLabel.setId(cursor.getInt(0));
                inspectionLabel.setMinLotSize(cursor.getInt(1));
                inspectionLabel.setMaxLotSize(cursor.getInt(2));
                inspectionLabel.setInspectionLevel(cursor.getInt(3));
                inspectionLabel.setSampleCode(cursor.getString(4));
                inspectionLabels.add(inspectionLabel);
            } while (cursor.moveToNext());
            cursor.close();
        }
        return inspectionLabels;
    }

    /*
     *  Get list of quality labels from database
     * */
    public List<QualityLabel> getQualityLabels() {
        List<QualityLabel> qualityLabels = new ArrayList<>();
        Cursor cursor = database.query(DatabaseHelper.QUALITY_LABEL_TABLE_NAME, new String[]{QUALITY_LABEL_COLUMN_ID,
                        DatabaseHelper.QUALITY_LABEL_COLUMN_SAMPLE_CODE, DatabaseHelper.QUALITY_LABEL_COLUMN_SAMPLE_SIZE,
                        DatabaseHelper.QUALITY_LABEL_COLUMN_QUALITY_LEVEL, DatabaseHelper.QUALITY_LABEL_COLUMN_NO_ACCEPTED_DEFECTS},
                null, null, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            do {
                QualityLabel qualityLabel = new QualityLabel();
                qualityLabel.setId(cursor.getInt(0));
                qualityLabel.setSampleCode(cursor.getString(1));
                qualityLabel.setSampleSize(cursor.getInt(2));
                qualityLabel.setQualityLevel(cursor.getDouble(3));
                qualityLabel.setNoOfAcceptedDefects(cursor.getInt(4));
                qualityLabels.add(qualityLabel);
            } while (cursor.moveToNext());
            cursor.close();
        }
        return qualityLabels;
    }

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