/*
 * Copyright (C) 2022 - Florindo Smilari (spherIC)
 */
package ch.spherIC.recurvebowsight.database.dao;

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

import ch.spherIC.recurvebowsight.database.scheme.masterdata.TblRBSArrow;
import ch.spherIC.recurvebowsight.model.IArrow;
import ch.spherIC.recurvebowsight.model.impl.Arrow;


/**
 * @author F. Smilari
 */
public final class ArrowDao extends DAO<IArrow> {


    private static ArrowDao instance;

    private ArrowDao() {
        super();
    }

    private ArrowDao(SQLiteDatabase db) {
        super(db);
    }

    public static ArrowDao getInstance() {
        if (instance == null) {
            instance = new ArrowDao();
        }
        return instance;
    }

    public static ArrowDao getInstance(SQLiteDatabase db) {
        if (instance == null) {
            instance = new ArrowDao(db);
        } else {
            instance.setDb(db);
        }
        return instance;
    }

    @Override
    protected IArrow cursorToEntity(final Cursor cursor) {
        return new Arrow(cursor.getLong(0), cursor.getString(1), cursor.getString(2), cursor.getDouble(3), cursor.getDouble(4),
                         cursor.getDouble(5), cursor.getDouble(6), cursor.getString(7));
    }

    @Override
    protected ContentValues getContentValues(final IArrow entity) {
        ContentValues values = new ContentValues();

        if (entity.getId() > 0) {
            values.put(TblRBSArrow.COL_ID, entity.getId());
        }

        values.put(TblRBSArrow.COL_MANUFACTURER, !entity.getManufacturer().isEmpty() ? entity.getManufacturer() : "");
        values.put(TblRBSArrow.COL_MODEL, !entity.getModel().isEmpty() ? entity.getModel() : "");
        values.put(TblRBSArrow.COL_DIAMETER, entity.getDiameter() != null ? entity.getDiameter() : 0d);
        values.put(TblRBSArrow.COL_WEIGHT, entity.getWeight() != null ? entity.getWeight() : 0d);
        values.put(TblRBSArrow.COL_CW, entity.getCw() != null ? entity.getCw() : 0d);
        values.put(TblRBSArrow.COL_V0, entity.getV0() != null ? entity.getV0() : 0d);
        values.put(TblRBSArrow.COL_COLORS, entity.getColors() != null ? entity.getColors() : "");
        return values;
    }

    @Override
    protected String getTableName() {
        return TblRBSArrow.NAME;
    }

    @Override
    protected String getIdColumn() {
        return TblRBSArrow.COL_ID;
    }

    @Override
    protected String getOrderBy() {
        return TblRBSArrow.COL_MANUFACTURER + " asc, " + TblRBSArrow.COL_MODEL + " asc, " + TblRBSArrow.COL_DIAMETER + " asc";
    }
}
