/*
 * Copyright (c) 2015 Justin White <jw@justinwhite.net>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */

package net.justinwhite.score_it;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import net.justinwhite.score_model.phase_10.Phase10GameModel;
import net.justinwhite.score_model.phase_10.Phase10PlayerModel;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShowGameActivity
        extends Activity
        implements
        YesNoDialog.DialogListener,
        LineEditDialog.DialogListener,
        AdapterView.OnItemClickListener {

    @SuppressWarnings({"WeakerAccess", "unused"})
    @Bind(R.id.textGameName) TextView textGameName;
    @SuppressWarnings({"WeakerAccess", "unused"})
    @Bind(R.id.listPlayers) ListView listView;
    private Phase10GameModel game;
    private int chosenPlayer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_show_game);
        ButterKnife.bind(this);

        Intent intent = getIntent();
        int numPlayers = intent.getIntExtra(
                CreateGameActivity.EXTRA_NUM_PLAYERS,
                CreateGameActivity.DEFAULT_NUM_PLAYERS
        );

        game = new Phase10GameModel();
        game.setNumPlayers(numPlayers);
        textGameName.setText(game.getName());

        Phase10PlayerAdapter adapter = new Phase10PlayerAdapter(
                this.getBaseContext(),
                R.layout.item_phase10_player,
                game.getPlayerList()
        );
        listView.setAdapter(adapter);

        // Set OnItemClickListener so we can be notified on item clicks
        listView.setOnItemClickListener(this);

    }

    @Override
    public void onBackPressed() {
        moveTaskToBack(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                showExitDialog();
                return (true);
        }
        return (super.onOptionsItemSelected(item));
    }

    private void showExitDialog() {
        YesNoDialog endGameDialog = YesNoDialog.newInstance(
                getString(R.string.End_the_game_question)
        );
        endGameDialog.show(getFragmentManager(), "end_game_dialog");
    }

    @SuppressWarnings("unused")
    @OnClick(R.id.buttonEndGame)
    protected void EndGame(View view) {
        showExitDialog();
    }

    @Override
    public void onYesNoSubmit() {
        finish();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        chosenPlayer = position;
        LineEditDialog changeNameDialog = LineEditDialog.newInstance(
                game.getPlayer(position).getName()
        );
        changeNameDialog.show(getFragmentManager(), "change_name_dialog");
    }

    @Override
    public void onLineEditSubmit(String newName) {
        Phase10PlayerModel player = game.getPlayer(chosenPlayer);
        player.setName(newName);
        game.buildName();
        textGameName.setText(game.getName());
    }

    @SuppressWarnings({"unused", "EmptyMethod"})
    public interface OnPlayerItemInteractionListener {
        void onPlayerItemInteraction(int player);
    }
}

@@ -1861,16 +1861,45 @@
 extView;
+%0Aimport android.widget.Toast;
 %0A%0Aimport
@@ -2560,16 +2560,78 @@
 nPlayer;
+%0A    private int backButtonCount = 0;%0A    private Toast toast;
 %0A%0A    @O
@@ -3576,29 +3576,343 @@
     
-moveTaskToBack(true);
+if(backButtonCount %3E= 1)%0A        %7B%0A            backButtonCount = 0;%0A            toast.cancel();%0A            moveTaskToBack(true);%0A        %7D%0A        else%0A        %7B%0A            toast = Toast.makeText(this, %22Press the back button once again to exit.%22, Toast.LENGTH_LONG);%0A            toast.show();%0A            backButtonCount++;%0A        %7D
 %0A   

Add a two-press guard to Back button.