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

import android.content.ContentValues;

import android.database.Cursor;

import ch.spherIC.recurvebowsight.database.scheme.data.TblRBSArcherySetup;
import ch.spherIC.recurvebowsight.model.ArcherySetup;
import ch.spherIC.recurvebowsight.model.IArcherySetup;
import ch.spherIC.recurvebowsight.model.IRiser;
import ch.spherIC.recurvebowsight.model.ISight;


/**
 * @author  FC Smilari
 */
public final class ArcherySetupDao extends DAO<IArcherySetup> {


    private static ArcherySetupDao instance;

    private ArcherySetupDao() {
        super();
    }

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


    @Override
    protected IArcherySetup cursorToEntity(final Cursor cursor) {

        ISight sight = SightDao.getInstance().loadById(cursor.getLong(2));
        IRiser riser = RiserDao.getInstance().loadById(cursor.getLong(3));

        IArcherySetup archerySetup = new ArcherySetup(cursor.getLong(0), cursor.getString(1), sight, riser, cursor.getDouble(4), //
                                                      cursor.getDouble(5), cursor.getDouble(6), cursor.getDouble(7), cursor.getDouble(8),
                                                      cursor.getDouble(9), cursor.getDouble(10), cursor.getDouble(11), cursor.getDouble(12),
                                                      cursor.getDouble(13), cursor.getDouble(14), cursor.getDouble(15), cursor.getDouble(16),
                                                      cursor.getDouble(17));
        return archerySetup;
    }

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

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

        values.put(TblRBSArcherySetup.COL_NAME, entity.getName() != null ? entity.getName() : "");
        values.put(TblRBSArcherySetup.COL_SIGHT, entity.getSight() != null ? entity.getSight().getId() : 0);
        values.put(TblRBSArcherySetup.COL_RISER, entity.getRiser() != null ? entity.getRiser().getId() : 0);
        values.put(TblRBSArcherySetup.COL_ARROWDIAMETER, entity.getArrowDiameter() != null ? entity.getArrowDiameter() : 0d);
        values.put(TblRBSArcherySetup.COL_ARROWCW, entity.getArrowCw() != null ? entity.getArrowCw() : 0d);
        values.put(TblRBSArcherySetup.COL_ARROWWEIGHT, entity.getArrowWeight() != null ? entity.getArrowWeight() : 0d);
        values.put(TblRBSArcherySetup.COL_ARROWV0, entity.getArrowV0() != null ? entity.getArrowV0() : 0d);
        values.put(TblRBSArcherySetup.COL_ARROWCENTERHEIGHT, entity.getArrowCenterHeight() != null ? entity.getArrowCenterHeight() : 0d);
        values.put(TblRBSArcherySetup.COL_BOWPULLOUT, entity.getBowPullout() != null ? entity.getBowPullout() : 0d);
        values.put(TblRBSArcherySetup.COL_ARROWNOCKHEIGHT, entity.getArrowNockHeight() != null ? entity.getArrowNockHeight() : 0d);
        values.put(TblRBSArcherySetup.COL_HNA, entity.getHNA() != null ? entity.getHNA() : 0d);
        values.put(TblRBSArcherySetup.COL_DELTATIME, entity.getDeltaTime() != null ? entity.getDeltaTime() : 0d);
        values.put(TblRBSArcherySetup.COL_CALCPRECISION, entity.getCalcPrecision() != null ? entity.getCalcPrecision() : 0d);
        values.put(TblRBSArcherySetup.COL_SIGHTVERTSKALAMIDDLE, entity.getSightVertSkalaMiddle() != null ? entity.getSightVertSkalaMiddle() : 0d);
        values.put(TblRBSArcherySetup.COL_SIGHTHORSETTING, entity.getSightHorzSetting() != null ? entity.getSightHorzSetting() : 0d);
        values.put(TblRBSArcherySetup.COL_NOCKRAISING, entity.getNockRaising() != null ? entity.getNockRaising() : 0d);
        values.put(TblRBSArcherySetup.COL_BRACEHEIGHT, entity.getBraceHeight() != null ? entity.getBraceHeight() : 0d);

        return values;
    }

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

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

}
