package com.literallyjosh.mtg_counter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import petrov.kristiyan.colorpicker.ColorPicker;

public class CounterWidget extends FrameLayout {
    private int Life = 20;
    public int Player;

    public CounterWidget(Context context) {
        super(context);
    }

    private CountDownTimer AlertTimer;

    public CounterWidget(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.counter_widget, this);

        setBackgroundColor(Color.parseColor("#0099cc"));

        FrameLayout life_frame = (FrameLayout)findViewById(R.id.life_frame);
        life_frame.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                ShowColorPicker();
                return false;
            }
        });

        Button minusButton = (Button)findViewById(R.id.minus_life_button);
        minusButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Life--;
                UpdateLifeDisplay();
            }
        });

        Button addButton = (Button)findViewById(R.id.add_life_button);
        addButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Life++;
                UpdateLifeDisplay();
            }
        });

        UpdateLifeDisplay();
    }

    private void ShowColorPicker() {
        try {
            MainActivity ma = (MainActivity) getContext();
            ma.ShowColorPicker(Player);
        }
        catch(Exception ex) {

        }
    }

    public void SetLife(int life) {
        Life = life;
        UpdateLifeDisplay();
    }

    private void UpdateLifeDisplay() {
        TextView life_display = (TextView)findViewById(R.id.life_display);
        life_display.setText(Integer.toString(Life));

        if(Life <= 0) {
            life_display.setTextColor(Color.RED);

            if(AlertTimer != null) {
                AlertTimer.cancel();
            }

            AlertTimer = new CountDownTimer(2000, 1000) {
                public void onTick(long millisUntilFinished) {
                }

                public void onFinish() {
                    MainActivity ma = (MainActivity)getContext();
                    ma.CheckLifeTotals(Player);
                }
            }.start();
        }
        else {
            life_display.setTextColor(Color.WHITE);
        }
    }

    public void SetBackground(int c) {
        String col = String.format("#%06X", (0xFFFFFF & c));
        this.setBackgroundColor(Color.parseColor(col));

        TextView life_display = (TextView)findViewById(R.id.life_display);
        if(c == -1) {
            life_display.setTextColor(Color.parseColor("#000000"));
        }
        else {
            life_display.setTextColor(Color.parseColor("#ffffff"));
        }
    }
}

@@ -501,16 +501,55 @@
  Player;
+%0A    private boolean InverseTextColour;
 %0A%0A    pu
@@ -870,24 +870,60 @@
 et, this);%0A%0A
+        InverseTextColour = false;%0A%0A
         setB
@@ -2386,59 +2386,8 @@
 ) %7B%0A
-            life_display.setTextColor(Color.RED);%0A%0A
     
@@ -2821,59 +2821,31 @@
   %7D%0A
-        else %7B%0A            life_display.set
+%0A        Update
 TextColo
 r(Co
@@ -2844,33 +2844,13 @@
 Colo
+u
 r(
-Color.WHITE);%0A        %7D
+);
 %0A   
@@ -3123,61 +3123,458 @@
     
-life_display.setTextColor(Color.parseColor(%22#000000%22)
+InverseTextColour = true;%0A        %7D%0A        else %7B%0A            InverseTextColour = false;%0A        %7D%0A%0A        UpdateTextColour();%0A    %7D%0A%0A    private void UpdateTextColour() %7B%0A        TextView life_display = (TextView)findViewById(R.id.life_display);%0A%0A        if(InverseTextColour) %7B%0A            if(Life %3E= 1) %7B%0A                life_display.setTextColor(Color.BLACK);%0A            %7D%0A            else %7B%0A                life_display.setTextColor(Color.RED
 );%0A 
@@ -3576,34 +3576,41 @@
 D);%0A        
+  
-%7D%0A
+  %7D%0A%0A  
         
 else %7B%0A     
@@ -3589,36 +3589,74 @@
    %7D%0A%0A          
-else
+  return;%0A        %7D%0A%0A        if(Life %3E= 1)
  %7B%0A            l
@@ -3690,29 +3690,88 @@
 lor.
-parseColor(%22#ffffff%22)
+WHITE);%0A        %7D%0A        else %7B%0A            life_display.setTextColor(Color.RED
 );%0A 

Fix for issue 2