package cz.lamorak.wordgame;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.TextView;

import com.jakewharton.rxbinding2.view.RxView;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import cz.lamorak.wordgame.model.Word;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.PublishSubject;

/**
 * Created by ondrej on 5.4.2017.
 */

public class GameActivity extends AppCompatActivity {

    private static final int WORD_LIMIT = 3; // seconds

    private CompositeDisposable disposables;
    private Disposable wordTimerDisposable;
    private PublishSubject<Boolean> guessSubject;
    private AtomicBoolean gameStarted;
    private List<Word> words;

    private TextView countdown;
    private TextView wordOriginal;
    private TextView wordGuess;
    private Button wrongButton;
    private Button correctButton;

    public static void call(final Context context) {
        final Intent intent = new Intent(context, GameActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        disposables = new CompositeDisposable();
        guessSubject = PublishSubject.create();
        gameStarted = new AtomicBoolean(false);

        countdown = (TextView) findViewById(R.id.countdown);
        wordOriginal = (TextView) findViewById(R.id.word_original);
        wordGuess = (TextView) findViewById(R.id.word_guess);
        correctButton = (Button) findViewById(R.id.button_correct);
        wrongButton = (Button) findViewById(R.id.button_wrong);

        disposables.add(
                WordGameApp.getServiceProvider()
                        .getWordService()
                        .loadWords()
                        .doOnNext(Collections::shuffle)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(this::startGame)
        );
    }

    @Override
    protected void onResume() {
        super.onResume();
        disposables.add(
                RxView.clicks(wrongButton)
                        .map(o -> false)
                        .subscribe(guessSubject::onNext)
        );
        disposables.add(
                RxView.clicks(correctButton)
                        .map(o -> true)
                        .subscribe(guessSubject::onNext)
        );
        disposables.add(
                guessSubject.subscribe(aBoolean -> displayWord())
        );
        if (gameStarted.get()) {
            displayWord();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        disposables.clear();
    }

    private void startGame(final List<Word> words) {
        this.words = words;
        gameStarted.set(true);
        displayWord();
    }

    private void displayWord() {
        if (words.size() < 2) {
            finishGame();
        }
        final Word word = words.remove(0);
        wordOriginal.setText(word.getEngllishWord());
        wordGuess.setText(word.getSpanishWord());
        wordGuess.setTranslationY(0);
        wordGuess.animate()
                .translationY(1000)
                .setDuration(3000L)
                .start();

        if (wordTimerDisposable != null && !wordTimerDisposable.isDisposed()) {
            wordTimerDisposable.dispose();
        }
        wordTimerDisposable = Observable.timer(WORD_LIMIT, TimeUnit.SECONDS)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(l -> guessSubject.onNext(false));
        disposables.add(wordTimerDisposable);
    }
}

@@ -457,16 +457,67 @@
 oolean;%0A
+import java.util.concurrent.atomic.AtomicInteger;%0A%0A
 import c
@@ -547,16 +547,16 @@
 l.Word;%0A
-
 import i
@@ -921,16 +921,73 @@
 vity %7B%0A%0A
+    private static final int TIME_LIMIT = 15; // seconds%0A
     priv
@@ -1174,16 +1174,57 @@
 ubject;%0A
+    private AtomicInteger timeRemaining;%0A
     priv
@@ -1885,24 +1885,79 @@
 t.create();%0A
+        timeRemaining = new AtomicInteger(TIME_LIMIT);%0A
         game
@@ -2806,32 +2806,568 @@
 isposables.add(%0A
+                Observable.interval(1, TimeUnit.SECONDS)%0A                        .filter(timeElapsed -%3E gameStarted.get())%0A                        .take(timeRemaining.get())%0A                        .map(timeElapsed -%3E timeRemaining.getAndDecrement())%0A                        .map(String::valueOf)%0A                        .observeOn(AndroidSchedulers.mainThread())%0A                        .doOnComplete(this::finishGame)%0A                        .subscribe(timeString -%3E countdown.setText(timeString))%0A        );%0A        disposables.add(%0A
                 
@@ -4862,24 +4862,24 @@
 xt(false));%0A
-
         disp
@@ -4917,11 +4917,68 @@
 );%0A    %7D
+%0A%0A    private void finishGame() %7B%0A        finish();%0A    %7D
 %0A%7D%0A

Implement game coundown timer