package de.frajul.endlessroll.sqlDatabase;

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

import de.frajul.endlessroll.entities.tools.ToolType;
import de.frajul.endlessroll.levels.Level;
import de.frajul.endlessroll.levels.LevelPack;

/**
 * Created by Julian on 10.05.2016.
 */
public class MyDatabase extends DatabaseAdapter {

    private final static int VERSION = 9;
    private final static String DATABASE_NAME = "DATABASE";
    private SQLTableColumn levelPackIdColumn, lockedColumn;
    private SQLTableColumn levelIdColumn, levelFinishedColumn, collectedStarsColumn, collectedEnergyColumn;
    private SQLTableColumn toolIdColumn, boughtColumn, toolLevelColumn;
    private SQLTable levelPackTable, levelTable, toolTable;

    public MyDatabase(Context context) {
        super(context, DATABASE_NAME, VERSION);
    }

    @Override
    protected void createColumns() {
        levelPackIdColumn = new SQLTableColumn("LEVELPACK_ID", SQLTableColumn.ColumnType.INTEGER);
        lockedColumn = new SQLTableColumn("LOCKED", SQLTableColumn.ColumnType.INTEGER);

        levelIdColumn = new SQLTableColumn("LEVEL_ID", SQLTableColumn.ColumnType.INTEGER);
        levelFinishedColumn = new SQLTableColumn("FINISHED", SQLTableColumn.ColumnType.INTEGER);
        collectedStarsColumn = new SQLTableColumn("STARS", SQLTableColumn.ColumnType.TEXT);
        collectedEnergyColumn = new SQLTableColumn("ENERGY", SQLTableColumn.ColumnType.INTEGER);

        toolIdColumn = new SQLTableColumn("TOOL_ID", SQLTableColumn.ColumnType.INTEGER);
        boughtColumn = new SQLTableColumn("BOUGHT", SQLTableColumn.ColumnType.INTEGER);
        toolLevelColumn = new SQLTableColumn("TOOL_LEVEL", SQLTableColumn.ColumnType.INTEGER);
    }

    @Override
    protected SQLTable[] createTables() {
        levelPackTable = new SQLTable("LEVELPACK_TABLE", levelPackIdColumn, lockedColumn);
        levelTable = new SQLTable("LEVEL_TABLE", levelPackIdColumn, levelIdColumn, lockedColumn,
                levelFinishedColumn, collectedStarsColumn, collectedEnergyColumn);
        toolTable = new SQLTable("TOOL_TABLE", toolIdColumn, boughtColumn, toolLevelColumn);
        return new SQLTable[]{levelPackTable, levelTable, toolTable};
    }

    public void writeLevelPackLocked(LevelPack levelPack) {
        ContentValues values = new ContentValues();
        values.put(levelPackIdColumn.getKey(), levelPack.getId());
        values.put(lockedColumn.getKey(), levelPack.isLocked() ? 1 : 0);
        super.update(levelPackTable, values, levelPackIdColumn, levelPack.getId());
    }

    public void readLevelPackLocked(LevelPack levelPack) {
        Cursor cursor = super.getCursor(levelPackTable, levelPackIdColumn, levelPack.getId());
        if (cursor.moveToFirst()) {
            boolean locked = cursor.getInt(1) == 1;
            levelPack.setLocked(locked);
        }
        cursor.close();
    }

    public void clearLevelPackLocked() {
        super.clearTable(levelPackTable);
    }

    public void writeLevelProgress(Level level) {
        ContentValues values = new ContentValues();
        values.put(levelPackIdColumn.getKey(), level.getPackId());
        values.put(levelIdColumn.getKey(), level.getId());
        values.put(lockedColumn.getKey(), level.isLocked() ? 1 : 0);
        values.put(levelFinishedColumn.getKey(), level.isFinished() ? 1 : 0);
        values.put(collectedStarsColumn.getKey(), level.getCollectedStarCodeForSQL());
        values.put(collectedEnergyColumn.getKey(), level.isEnergyCollected());
        super.update(levelTable, values, levelPackIdColumn, level.getPackId(), levelIdColumn,
                level.getId());
    }

    public void readLevelProgress(Level level) {
        Cursor cursor = super
                .getCursor(levelTable, levelPackIdColumn, level.getPackId(), levelIdColumn,
                        level.getId());
        if (cursor.moveToFirst()) {
            boolean locked = cursor.getInt(2) == 1;
            boolean finished = cursor.getInt(3) == 1;
            String stars = cursor.getString(4);
            boolean energy = cursor.getInt(5) == 1;

            level.setLocked(locked);
            level.setFinished(finished);
            level.setCollectedStarsFromSQL(stars);
            level.setEnergyCollected(energy);
        }
        cursor.close();
    }

    public void clearLevelProgess() {
        super.clearTable(levelTable);
    }

    public void writeToolData() {
        for (ToolType tool : ToolType.values()) {
            ContentValues values = new ContentValues();
            values.put(toolIdColumn.getKey(), tool.ordinal());
            values.put(boughtColumn.getKey(), tool.isBought() ? 1 : 0);
            values.put(toolLevelColumn.getKey(), tool.getCurrentUpgradeLevel());
            super.update(toolTable, values, toolIdColumn, tool.ordinal());
        }
    }

    public void readToolData() {
        Cursor cursor = super.getCursor(toolTable);
        if (cursor.moveToFirst()) {
            do {
                int id = cursor.getInt(0);
                boolean bought = cursor.getInt(1) == 1;
                int toolLevel = cursor.getInt(2);

                ToolType tool = ToolType.values()[id];
                tool.setBought(bought);
                tool.setCurrentUpgradeLevel(toolLevel);
            } while (cursor.moveToNext());
        }
        cursor.close();
    }

}
