package com.khmelenko.lab.sensorsclient.ui.presenter;

import com.khmelenko.lab.sensorsclient.network.RestClient;
import com.khmelenko.lab.sensorsclient.network.response.WeatherData;
import com.khmelenko.lab.sensorsclient.ui.view.DeviceDataActivityView;

import java.util.List;

import javax.inject.Inject;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Presenter for device data activity
 *
 * @author Dmytro Khmelenko (d.khmelenko@gmail.com)
 */
public final class DeviceDataActivityPresenter extends BasePresenter<DeviceDataActivityView> {

    private final RestClient mRestClient;

    private Subscription mCurrentDataSubscription;
    private Subscription mHistoryDataSubscription;

    @Inject
    public DeviceDataActivityPresenter(RestClient restClient) {
        mRestClient = restClient;
    }

    @Override
    public void onAttach() {
        // do nothing
    }

    @Override
    public void onDetach() {
        if (mCurrentDataSubscription != null && mCurrentDataSubscription.isUnsubscribed()) {
            mCurrentDataSubscription.unsubscribe();
        }

        if (mHistoryDataSubscription != null && mHistoryDataSubscription.isUnsubscribed()) {
            mHistoryDataSubscription.unsubscribe();
        }
    }

    /**
     * Starts loading history data
     *
     * @param deviceName Device name
     */
    public void loadHistory(String deviceName) {
        Subscriber<List<WeatherData>> subscriber = historyDataSubscriber();
        mHistoryDataSubscription = getHistoryData(deviceName)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    /**
     * Starts loading current data
     *
     * @param deviceName Device name
     */
    public void loadCurrentData(String deviceName) {
        Subscriber<WeatherData> subscriber = currentDataSubscriber();
        mCurrentDataSubscription = getCurrentData(deviceName)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    /**
     * Gets observable for current data
     *
     * @param deviceName Device name
     * @return Observable
     */
    private Observable<WeatherData> getCurrentData(String deviceName) {
        return mRestClient.getService().getCurrentData(deviceName);
    }

    /**
     * Gets observable for history data
     *
     * @param deviceName Device name
     * @return Observable
     */
    private Observable<List<WeatherData>> getHistoryData(String deviceName) {
        return mRestClient.getService().getHistory(deviceName);
    }

    /**
     * Prepares subscriber for the current data
     *
     * @return Subscriber
     */
    private Subscriber<WeatherData> currentDataSubscriber() {
        return new Subscriber<WeatherData>() {

            @Override
            public void onCompleted() {
                // do nothing
            }

            @Override
            public void onError(Throwable e) {
                getView().showErrorToast(e.getMessage());
            }

            @Override
            public void onNext(WeatherData currentData) {
                getView().setCurrentData(currentData);
            }
        };
    }

    /**
     * Prepares subscriber for the history data
     *
     * @return Subscriber
     */
    private Subscriber<List<WeatherData>> historyDataSubscriber() {
        return new Subscriber<List<WeatherData>>() {
            @Override
            public void onCompleted() {
                // do nothing
            }

            @Override
            public void onError(Throwable e) {
                getView().showErrorToast(e.getMessage());
            }

            @Override
            public void onNext(List<WeatherData> history) {
                getView().setHistoryData(history);
            }
        };
    }

}

@@ -270,16 +270,54 @@
 il.List;
+%0Aimport java.util.concurrent.TimeUnit;
 %0A%0Aimport
@@ -455,16 +455,43 @@
 dulers;%0A
+import rx.functions.Func1;%0A
 import r
@@ -1139,16 +1139,17 @@
 null && 
+!
 mCurrent
@@ -1296,16 +1296,17 @@
 null && 
+!
 mHistory
@@ -1971,32 +1971,38 @@
 loadCurrentData(
+final 
 String deviceNam
@@ -2018,130 +2018,348 @@
     
-Subscriber%3CWeather
+mCurrent
 Data
-%3E s
+S
 ubscri
-ber = currentDataSubscriber();%0A        mCurrentDataSubscription = getCurrentData(deviceName
+ption = Observable.interval(0, 10, TimeUnit.SECONDS)%0A                .flatMap(new Func1%3CLong, Observable%3CWeatherData%3E%3E() %7B%0A                    @Override%0A                    public Observable%3CWeatherData%3E call(Long aLong) %7B%0A                        return getCurrentData(deviceName);%0A                    %7D%0A                %7D
 )%0A  
@@ -2487,33 +2487,44 @@
      .subscribe(
-s
+currentDataS
 ubscriber);%0A    
@@ -2508,32 +2508,34 @@
 ntDataSubscriber
+()
 );%0A    %7D%0A%0A    /*

Implement polling current weather data