package com.morihacky.android.rxjava;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import com.morihacky.android.rxjava.wiring.LogAdapter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import rx.Observable;
import rx.Observer;
import rx.Subscription;
import timber.log.Timber;

import static android.os.Looper.getMainLooper;
import static android.os.Looper.myLooper;

public class TimingDemoFragment
      extends BaseFragment {

    @InjectView(R.id.list_threading_log) ListView _logsList;

    private LogAdapter _adapter;
    private List<String> _logs;

    private Subscription _subscription1 = null;
    private Subscription _subscription2 = null;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        _setupLogger();
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.fragment_demo_timing, container, false);
        ButterKnife.inject(this, layout);
        return layout;
    }

    // -----------------------------------------------------------------------------------

    @OnClick(R.id.btn_demo_timing_1)
    public void Btn1_RunSingleTaskAfter2s() {
        _log(String.format("A1 [%s] --- BTN click", _getCurrentTimestamp()));

        Observable.timer(2, TimeUnit.SECONDS)//
              //.just(1).delay(2, TimeUnit.SECONDS)//
              .subscribe(new Observer<Long>() {
                  @Override
                  public void onCompleted() {
                      _log(String.format("A1 [%s] XXX COMPLETE", _getCurrentTimestamp()));
                  }

                  @Override
                  public void onError(Throwable e) {
                      Timber.e(e, "something went wrong in TimingDemoFragment example");
                  }

                  @Override
                  public void onNext(Long number) {
                      _log(String.format("A1 [%s]     NEXT", _getCurrentTimestamp()));
                  }
              });
    }

    @OnClick(R.id.btn_demo_timing_2)
    public void Btn2_RunTask_IntervalOf1s() {
        if (_subscription1 != null && !_subscription1.isUnsubscribed()) {
            _subscription1.unsubscribe();
            _log(String.format("B2 [%s] XXX BTN KILLED", _getCurrentTimestamp()));
            return;
        }

        _log(String.format("B2 [%s] --- BTN click", _getCurrentTimestamp()));

        _subscription1 = Observable//
              .interval(1, TimeUnit.SECONDS)//
              .subscribe(new Observer<Long>() {
                  @Override
                  public void onCompleted() {
                      _log(String.format("B2 [%s] XXXX COMPLETE", _getCurrentTimestamp()));
                  }

                  @Override
                  public void onError(Throwable e) {
                      Timber.e(e, "something went wrong in TimingDemoFragment example");
                  }

                  @Override
                  public void onNext(Long number) {
                      _log(String.format("B2 [%s]     NEXT", _getCurrentTimestamp()));
                  }
              });
    }

    @OnClick(R.id.btn_demo_timing_4)
    public void Btn4_RunTask5Times_IntervalOf3s() {
        _log(String.format("D4 [%s] --- BTN click", _getCurrentTimestamp()));

        Observable//
              .interval(3, TimeUnit.SECONDS).take(5)//
              .subscribe(new Observer<Long>() {
                  @Override
                  public void onCompleted() {
                      _log(String.format("D4 [%s] XXX COMPLETE", _getCurrentTimestamp()));
                  }

                  @Override
                  public void onError(Throwable e) {
                      Timber.e(e, "something went wrong in TimingDemoFragment example");
                  }

                  @Override
                  public void onNext(Long number) {
                      _log(String.format("D4 [%s]     NEXT", _getCurrentTimestamp()));
                  }
              });
    }

    // -----------------------------------------------------------------------------------
    // Method that help wiring up the example (irrelevant to RxJava)

    private void _setupLogger() {
        _logs = new ArrayList<>();
        _adapter = new LogAdapter(getActivity(), new ArrayList<String>());
        _logsList.setAdapter(_adapter);
    }

    private void _log(String logMsg) {
        _logs.add(0, String.format(logMsg + " [MainThread: %b]", getMainLooper() == myLooper()));

        // You can only do below stuff on main thread.
        new Handler(getMainLooper()).post(new Runnable() {

            @Override
            public void run() {
                _adapter.clear();
                _adapter.addAll(_logs);
            }
        });
    }

    private String _getCurrentTimestamp() {
        return new SimpleDateFormat("k:m:s:S a").format(new Date());
    }

}

@@ -3645,32 +3645,1164 @@
      %7D);%0A    %7D%0A%0A
+    @OnClick(R.id.btn_demo_timing_3)%0A    public void Btn3_RunTask_IntervalOf1s_StartImmediately() %7B%0A        if (_subscription2 != null && !_subscription2.isUnsubscribed()) %7B%0A            _subscription2.unsubscribe();%0A            _log(String.format(%22C3 %5B%25s%5D XXX BTN KILLED%22, _getCurrentTimestamp()));%0A            return;%0A        %7D%0A%0A        _log(String.format(%22C3 %5B%25s%5D --- BTN click%22, _getCurrentTimestamp()));%0A%0A        _subscription2 = Observable//%0A              .timer(0, 1, TimeUnit.SECONDS)//%0A              .subscribe(new Observer%3CLong%3E() %7B%0A                  @Override%0A                  public void onCompleted() %7B%0A                      _log(String.format(%22C3 %5B%25s%5D XXXX COMPLETE%22, _getCurrentTimestamp()));%0A                  %7D%0A%0A                  @Override%0A                  public void onError(Throwable e) %7B%0A                      Timber.e(e, %22something went wrong in TimingDemoFragment example%22);%0A                  %7D%0A%0A                  @Override%0A                  public void onNext(Long number) %7B%0A                      _log(String.format(%22C3 %5B%25s%5D     NEXT%22, _getCurrentTimestamp()));%0A                  %7D%0A              %7D);%0A    %7D%0A%0A
     @OnClick(R.i

add button 3 example where you start immediately and then go on with intervals