package anabalica.github.io.meowletters;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import anabalica.github.io.meowletters.storage.HighscoresContract;
import anabalica.github.io.meowletters.storage.HighscoresDbHelper;


/**
 * This is the Game Over activity. When there are no more free spots for letters to appear, the
 * game ends.
 *
 * @author Ana Balica
 */
public class GameOverActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_over);
        Intent intent = getIntent();
        String score = intent.getStringExtra(GameActivity.SCORE);

        TextView finalScore = (TextView) findViewById(R.id.finalScore);
        finalScore.setText("Score " + score);

        int points = Integer.parseInt(score);
        insertNewHighscrore(points);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_game_over, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * Launch menu on 'Back' press
     */
    public void onBackPressed() {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.startActivity(intent);
    }

    /**
     * Start a new game
     */
    public void startNewGame(View view) {
        Intent intent = new Intent(this, GameActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.startActivity(intent);
    }

    /**
     * Insert into the database highscores table a new entry. Games that have accumulated 0 points,
     * are not inserted.
     *
     * @param score int amount of game points
     */
    private void insertNewHighscrore(int score) {
        if (score > 0) {
            HighscoresDbHelper highscoresDbHelper = new HighscoresDbHelper(this);
            SQLiteDatabase db = highscoresDbHelper.getWritableDatabase();

            ContentValues values = new ContentValues();
            values.put(HighscoresContract.HighscoreEntry.COLUMN_NAME_USERNAME, "dummy username");
            values.put(HighscoresContract.HighscoreEntry.COLUMN_NAME_SCORE, score);

            db.insert(HighscoresContract.HighscoreEntry.TABLE_NAME, null, values);
            System.out.println("Inserted the entry");
        }
    }
}

@@ -388,16 +388,85 @@
 ntract;%0A
+import anabalica.github.io.meowletters.storage.HighscoresDataSource;%0A
 import a
@@ -1175,25 +1175,24 @@
 ertNewHighsc
-r
 ore(points);
@@ -2572,17 +2572,8 @@
 the 
-database 
 high
@@ -2600,72 +2600,8 @@
 ntry
-. Games that have accumulated 0 points,%0A     * are not inserted.
 %0A   
@@ -2690,17 +2690,16 @@
 ewHighsc
-r
 ore(int 
@@ -2719,190 +2719,82 @@
     
-if (score %3E 0) %7B%0A            HighscoresDbHelper highscoresDbHelper = new HighscoresDbHelper(this);%0A            SQLiteDatabase db = highscoresDbHelper.getWritableDatabase(
+HighscoresDataSource dataSource = new HighscoresDataSource(this
 );%0A
-%0A
         
     
@@ -2793,228 +2793,76 @@
     
-    ContentValues values = new ContentValues();%0A            values.put(HighscoresContract.HighscoreEntry.COLUMN_NAME_USERNAME, %22dummy username%22);%0A            values.put(HighscoresContract.HighscoreEntry.COLUMN_NAME_SCORE
+dataSource.open();%0A        dataSource.createHighscore(%22Chubby bunny%22
 , sc
@@ -2867,27 +2867,24 @@
 score);%0A
-%0A  
         
   db.ins
@@ -2879,144 +2879,27 @@
     
-  db.insert(HighscoresContract.HighscoreEntry.TABLE_NAME, null, values);%0A            System.out.println(%22Inserted the entry%22);%0A        %7D
+dataSource.close();
 %0A   

use Highscore DAO in the GameOverActivity to insert a new highscore