package io.akessler.elixircounter;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.Button;

/**
 * Created by Andy on 9/14/2017.
 */
public class OverlayService extends Service {

    WindowManager windowManager;

    Button[] counterButtons;

    CountDownTimer regularElixirTimer;

    CountDownTimer doubleElixirTimer;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

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

        windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

        ElixirStore.createInstance(this, windowManager); // TODO Revisit this design pattern...

        initTimers();
        initCounterButtons();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        regularElixirTimer.cancel();
        doubleElixirTimer.cancel();
        // FIXME Remove TextView from ElixirStore
        for(int i = 0; i< counterButtons.length; i++) {
            Button b = counterButtons[i];
            if(counterButtons[i] != null) {
                windowManager.removeView(b);
                b = null;
            }
        }
    }

    private void initTimers() {
        doubleElixirTimer = new CountDownTimer(120000, 1400) {
            public void onTick(long millisUntilFinished) {
                ElixirStore.add(1);
            }

            public void onFinish() {
                System.out.println("2x elixir time complete.");
            }
        };

        regularElixirTimer = new CountDownTimer(120000, 2800) {
            public void onTick(long millisUntilFinished) {
                ElixirStore.add(1);
            }

            public void onFinish() {
                System.out.println("1x elixir time complete.");
                ElixirStore.add(1);
                doubleElixirTimer.start();
            }
        };
    }

    private void initCounterButtons() {
        counterButtons = new Button[11];
        for(int i = 0; i < counterButtons.length; i++) {
            int counterValue = i != 0 ? -i : 1; // FIXME There might be a cleaner way...
            counterButtons[i] = new CounterButton(this, counterValue);
            WindowManager.LayoutParams buttonParams = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, // FIXME Acts up on certain API versions
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                    PixelFormat.TRANSLUCENT
            );
            buttonParams.gravity = Gravity.LEFT | Gravity.TOP;
            // FIXME Change these values to be dynamic, based on dimensions of screen
            buttonParams.x = 25;
            buttonParams.y = (counterButtons.length - i) * 150;
            windowManager.addView(counterButtons[i], buttonParams);
        }
    }
}
@@ -291,16 +291,42 @@
 ravity;%0A
+import android.view.View;%0A
 import a
@@ -348,24 +348,24 @@
 dowManager;%0A
-
 import andro
@@ -535,16 +535,41 @@
 ttons;%0A%0A
+    Button startButton;%0A%0A
     Coun
@@ -1006,24 +1006,79 @@
 itTimers();%0A
+%0A        initStartButton(); // TODO initStopButton();%0A%0A
         init
@@ -1091,24 +1091,25 @@
 rButtons();%0A
+%0A
     %7D%0A%0A    @
@@ -1239,32 +1239,33 @@
 Timer.cancel();%0A
+%0A
         // FIXME
@@ -1298,16 +1298,63 @@
 irStore%0A
+        windowManager.removeView(startButton);%0A
         
@@ -1536,34 +1536,8 @@
 b);%0A
-                b = null;%0A
     
@@ -2255,32 +2255,32 @@
 ;%0A            %7D%0A
-
         %7D;%0A    %7D
@@ -2273,32 +2273,1081 @@
       %7D;%0A    %7D%0A%0A
+    private void initStartButton() %7B%0A        startButton = new Button(this);%0A        startButton.setText(%22Start%22);%0A        startButton.setOnClickListener(new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View v) %7B%0A                regularElixirTimer.start();%0A                startButton.setEnabled(false);%0A                startButton.setVisibility(View.GONE);%0A            %7D%0A        %7D);%0A        WindowManager.LayoutParams buttonParams = new WindowManager.LayoutParams(%0A                WindowManager.LayoutParams.WRAP_CONTENT,%0A                WindowManager.LayoutParams.WRAP_CONTENT,%0A                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, // FIXME Acts up on certain API versions%0A                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE %7C WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,%0A                PixelFormat.TRANSLUCENT%0A        );%0A        buttonParams.gravity = Gravity.CENTER;%0A        buttonParams.x = 0;%0A        buttonParams.y = 0;%0A        windowManager.addView(startButton, buttonParams);%0A    %7D%0A%0A
     private void

Add a start button to begin timers