package com.literallyjosh.mtg_counter;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import java.util.ArrayList;

import petrov.kristiyan.colorpicker.ColorPicker;

public class MainActivity extends AppCompatActivity {
    private int players = 2;
    private Game game;

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

        setContentView(R.layout.activity_main);

        StartGame();
    }

    private void StartGame() {
        ImageButton turnButton = (ImageButton)findViewById(R.id.turn_button);
        turnButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                game.ChangeTurns();

                ImageButton turnButton = (ImageButton)findViewById(R.id.turn_button);

                if(game.CurrentTurn.Player == 1) {
                    turnButton.setImageResource(R.drawable.ic_navigation_arrow_drop_up);
                }
                else {
                    turnButton.setImageResource(R.drawable.ic_navigation_arrow_drop_down);
                }
            }
        });

        game = new Game();
        game.Players = players;

        CounterWidget p1 = (CounterWidget)findViewById(R.id.player1_widget);
        p1.Player = 1;
        p1.SetLife(20);

        CounterWidget p2 = (CounterWidget)findViewById(R.id.player2_widget);
        p2.Player = 2;
        p1.SetLife(20);

        adjustSizeForPlayers();
    }

    private void adjustSizeForPlayers() {
        CounterWidget p3 = (CounterWidget)findViewById(R.id.player3_widget);
        CounterWidget p4 = (CounterWidget)findViewById(R.id.player4_widget);

        LinearLayout p34_layout = (LinearLayout)findViewById(R.id.p34_layout);

        if(players == 2) {
            // Just collapse players 3 + 4
            p3.setVisibility(View.GONE);
            p4.setVisibility(View.GONE);
            p34_layout.setVisibility(View.GONE);
        }

        if(players == 3) {
            p4.setVisibility(View.INVISIBLE);
        }
    }

    public void CheckLifeTotals(int Player) {
        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which){
                    case DialogInterface.BUTTON_POSITIVE:
                        finish();
                        startActivity(getIntent());
                        break;

                    case DialogInterface.BUTTON_NEGATIVE:
                        //No button clicked
                        break;
                }
            }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("End Game");
        builder.setMessage(String.format("Player %s has fallen below 1 life!\nEnd game?", Player))
                .setPositiveButton("Yes", dialogClickListener)
                .setNegativeButton("No", dialogClickListener)
                .show();
    }

    public void ShowColorPicker(final int player) {
        final ColorPicker colorPicker = new ColorPicker(MainActivity.this);
        colorPicker.setFastChooser(new ColorPicker.OnFastChooseColorListener() {
            @Override
            public void setOnFastChooseColorListener(int position, int color) {
                colorPicker.dismissDialog();

                if(player == 1) {
                    CounterWidget p1 = (CounterWidget) findViewById(R.id.player1_widget);
                    p1.SetBackground(color);
                }
                else {
                    CounterWidget p2 = (CounterWidget) findViewById(R.id.player2_widget);
                    p2.SetBackground(color);
                }
            }
        }).setColors(Color.parseColor("#F44336"),
                     Color.parseColor("#2196F3"),
                     Color.parseColor("#000000"),
                     Color.parseColor("#FFFFFF"),
                     Color.parseColor("#4CAF50"))
          .setRoundButton(true)
          .setTitle("Select background colour")
          .show();
    }
}

@@ -215,33 +215,8 @@
 ty;%0A
-import android.util.Log;%0A
 impo
@@ -313,37 +313,8 @@
 t;%0A%0A
-import java.util.ArrayList;%0A%0A
 impo
@@ -1043,281 +1043,26 @@
     
-if(game.CurrentTurn.Player == 1) %7B%0A                    turnButton.setImageResource(R.drawable.ic_navigation_arrow_drop_up);%0A                %7D%0A                else %7B%0A                    turnButton.setImageResource(R.drawable.ic_navigation_arrow_drop_down);%0A                %7D
+UpdateTurnArrow();
 %0A   
@@ -1371,33 +1371,33 @@
 r = 2;%0A        p
-1
+2
 .SetLife(20);%0A%0A 
@@ -1403,17 +1403,17 @@
         
-a
+A
 djustSiz
@@ -1435,29 +1435,421 @@
     
-%7D%0A%0A    private void a
+    UpdateTurnArrow();%0A    %7D%0A%0A    private void UpdateTurnArrow() %7B%0A        ImageButton turnButton = (ImageButton)findViewById(R.id.turn_button);%0A%0A        if(game.CurrentTurn.Player == 1) %7B%0A            turnButton.setImageResource(R.drawable.ic_navigation_arrow_drop_up);%0A        %7D%0A        else %7B%0A            turnButton.setImageResource(R.drawable.ic_navigation_arrow_drop_down);%0A        %7D%0A    %7D%0A%0A    private void A
 djus
@@ -2762,70 +2762,277 @@
     
-finish();%0A                        startActivity(getIntent()
+// Fix for https://github.com/josh-perry/MTG-Counter/issues/7%0A                        runOnUiThread(new Runnable() %7B%0A                            public void run() %7B%0A                                StartGame();%0A                            %7D%0A                        %7D
 );%0A
+%0A
     

Fix for https://github.com/josh-perry/MTG-Counter/issues/7