package com.artursworld.reactiontest.view.games;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.artursworld.reactiontest.R;
import com.artursworld.reactiontest.controller.helper.GameStatus;
import com.artursworld.reactiontest.controller.util.UtilsRG;
import com.artursworld.reactiontest.view.UserManagement;

import java.util.Random;

public class GoGameView extends AppCompatActivity {

    private Activity activity;
    private GameStatus gameStatus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_go_game);
        activity = this;
        gameStatus = GameStatus.WAITING;

        UtilsRG.info("received message="+getIntentMessage(UserManagement.EXTRA_MESSAGE));

        hideActionBar(getSupportActionBar());
        setBackGroundColor(activity, R.color.goGameBlue);
        waitAndChangeStatusToClick(2000, 5000);
    }

    private String getIntentMessage(String messageKey){
        Intent intent = getIntent();
        String message = intent.getStringExtra(messageKey);
        return message;
    }

    private void hideActionBar(ActionBar actionBar){
        actionBar.hide();
    }

    private void setBackGroundColor(Activity activity, int colorId){
        try {
            int color = ContextCompat.getColor(activity.getApplicationContext(), colorId);
            activity.getWindow().getDecorView().setBackgroundColor(color);
            UtilsRG.info("background color set to: " +color);
        }
        catch (Exception e){
            String message = "Could not set background color.";
            UtilsRG.error(message + "\n" + e.getLocalizedMessage());
        }
    }

    private int getRandomNumberInRange(int min, int max){
        Random r = new Random();
        int random =  r.nextInt(max - min + 1) + min;
        UtilsRG.info("Waiting random number=" +random);
        return random;
    }

    private void waitAndChangeStatusToClick(int minWaitTimeInMilliSeconds, int maxWaitTimeInMilliSeconds){
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                onChangeStatusToClick();
            }
        }, getRandomNumberInRange(minWaitTimeInMilliSeconds,maxWaitTimeInMilliSeconds));
    }

    private void onChangeStatusToClick() {
        UtilsRG.info("set different color");
        setBackGroundColor(activity, R.color.goGameGreen);
        gameStatus = GameStatus.CLICK;
    }

}

@@ -347,20 +347,82 @@
 id.view.
-View
+MotionEvent;%0Aimport android.view.View;%0Aimport android.widget.Toast
 ;%0A%0Aimpor
@@ -773,26 +773,28 @@
 eStatus 
-gameStatus
+statusOfGame
 ;%0A%0A    @
@@ -983,34 +983,36 @@
 is;%0A        
-gameStatus
+statusOfGame
  = GameStatu
@@ -2809,44 +2809,715 @@
     
-gameStatus = GameStatus.CLICK;%0A    %7D%0A
+statusOfGame = GameStatus.CLICK;%0A    %7D%0A%0A    @Override%0A    public boolean onTouchEvent(MotionEvent event)%7B%0A        if(event.getAction() == MotionEvent.ACTION_DOWN)%7B%0A            String text = %22x = %22 + event.getX() + %22, y = %22 + event.getY();%0A            Toast.makeText(this, text, Toast.LENGTH_LONG).show();%0A            if(statusOfGame == GameStatus.CLICK)%7B%0A                UtilsRG.info(%22right!%22);%0A                setBackGroundColor(this, R.color.goGameBlue);%0A                statusOfGame = GameStatus.WAITING;%0A                waitAndChangeStatusToClick(2000,4000);%0A            %7D%0A            else%7B%0A                UtilsRG.info(%22to early%22);%0A            %7D%0A        %7D%0A        return super.onTouchEvent(event);%0A    %7D
 %0A%7D%0A

add prototype goGame color change