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

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import ch.spherIC.recurvebowsight.database.dao.ArcherySetupDao;
import ch.spherIC.recurvebowsight.database.dao.RiserDao;
import ch.spherIC.recurvebowsight.database.dao.SightDao;
import ch.spherIC.recurvebowsight.database.scheme.data.TblRBSArcherySetup;
import ch.spherIC.recurvebowsight.database.scheme.masterdata.TblRBSRiser;
import ch.spherIC.recurvebowsight.database.scheme.masterdata.TblRBSSight;
import ch.spherIC.recurvebowsight.model.ArcherySetup;
import ch.spherIC.recurvebowsight.model.IArcherySetup;
import ch.spherIC.recurvebowsight.model.IRiser;
import ch.spherIC.recurvebowsight.model.ISight;
import ch.spherIC.recurvebowsight.model.Riser;
import ch.spherIC.recurvebowsight.model.Sight;

import java.util.ArrayList;
import java.util.List;


/**
 * Siehe http://www.sqlite.org/lang.html.
 *
 * @author  FC Smilari
 */
public final class RBSDatabaseHelper extends SQLiteOpenHelper {

    public static final String DB_NAME = "RecurveBowSight.db";
    public static final int DB_VERSION = 1;

    private static RBSDatabaseHelper instance;
    private static Context context;
    private static String actualDBName = DB_NAME;
    private boolean databaseCreated = false;


    /**
     * Konstruktor.
     *
     * @param  context {@link Context}
     */
    private RBSDatabaseHelper(final Context context) {
        super(context, actualDBName, null, DB_VERSION);
    }


    public static RBSDatabaseHelper getInstance() {
        if (instance == null) {
            instance = new RBSDatabaseHelper(context);
        }

        return instance;
    }

    /**
     * @param  context  the context to set
     */
    public static void setContext(final Context context) {
        RBSDatabaseHelper.context = context;
    }

    /**
     * @param  dbName  the database name to set
     */
    public static void setDB(final String dbName) {
        RBSDatabaseHelper.actualDBName = dbName;
    }


    public void initializeDB() {
        this.databaseCreated = false;
        getWritableDatabase();

        if (this.databaseCreated) {
            insertMasterData();
        }
    }

    @Override
    public void onOpen(final SQLiteDatabase db) {
        super.onOpen(db);
        if (!db.isReadOnly()) {
            db.execSQL("PRAGMA foreign_keys=ON;");
        }
    }

    @Override
    public void onCreate(final SQLiteDatabase db) {

        // Tabellen erstellen
        db.execSQL(TblRBSRiser.getCreateStatement());
        db.execSQL(TblRBSSight.getCreateStatement());
        db.execSQL(TblRBSArcherySetup.getCreateStatement());

        this.databaseCreated = true;
    }

    /**
     * Masterdaten einfügen.
     */
    public void insertMasterData() {

        IRiser riserFS;
        ISight sightFS;
        IArcherySetup archerySetup = getArcherySetupMasterData();

        for (IRiser riser : getRiserMasterData()) {
            riser = RiserDao.getInstance().insert(riser);
        }

        for (ISight sight : getSightMasterData()) {
            sight = SightDao.getInstance().insert(sight);
        }

        riserFS = RiserDao.getInstance().loadById(1);
        sightFS = SightDao.getInstance().loadById(1);
        archerySetup.setRiser(riserFS);
        archerySetup.setSight(sightFS);
        ArcherySetupDao.getInstance().insert(archerySetup);
    }


    @Override
    public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {

        /** TODO Upgrade tasks */
    }


    private List<IRiser> getRiserMasterData() {

        List<IRiser> risers = new ArrayList<IRiser>();

        risers.add(new Riser(0, "Hoyt", "HPX", 27d, 0d, 61d));

        return risers;
    }

    private List<ISight> getSightMasterData() {

        List<ISight> sights = new ArrayList<ISight>();

        sights.add(new Sight(0, "Avalon", "Tec One", 17.5, -12d, "260,250,240,230,220,210,200,190,180,170,160", 5d, 105d));

        return sights;
    }

    private IArcherySetup getArcherySetupMasterData() {
        return new ArcherySetup(0, "FITA Indoor FS", null, null, 5.8, 3.10, 21d, 57.60, 1.30, 71d, 1.57, 135d, 0.001, 0.1, 55d, 260d, 11d, 195d);
    }
}
