// Path: ParallelRequests/app/src/main/java/itis/homework/parallelrequests/network/RequestControllerImpl.java
// public class RequestControllerImpl implements RequestsController {
// 
//     private final List<RequestType> mLoadingRequests;
// 
//     private final List<RequestType> mLoadedRequests;
// 
//     private boolean mIsFatalError = false;
// 
//     public RequestControllerImpl() {
//         mLoadingRequests = new ArrayList<>();
//         mLoadedRequests = new ArrayList<>();
//     }
// 
//     @Override
//     public boolean tryRequest(RequestType requestType) {
//         if (mIsFatalError) {
//             return false;
//         }
//         if (mLoadingRequests.contains(requestType) || mLoadedRequests.contains(requestType)) {
//             return false;
//         }
// 
//         if (requestType == RequestType.AUTH || requestType == RequestType.CONFIG) {
//             mLoadingRequests.add(requestType);
//             Otto.get().post(LoadingStateChangedEvent.produce(new RequestsState(RequestsState.GeneralState.LOADING,
//                     mLoadingRequests, mLoadedRequests)));
//             return true;
//         }
// 
//         if ((requestType == RequestType.FRIENDS || requestType == RequestType.POSTS
//                 || requestType == RequestType.MESSAGES || requestType == RequestType.GROUPS)
//                 && (!mLoadedRequests.contains(RequestType.AUTH) || !mLoadedRequests.contains(RequestType.CONFIG))) {
//             mIsFatalError = true;
//             Otto.get().post(LoadingStateChangedEvent.produce(new RequestsState(RequestsState.GeneralState.ERROR)));
//             return false;
//         }
// 
//         if (requestType == RequestType.PHOTOS && !mLoadedRequests.contains(RequestType.MESSAGES)) {
//             mIsFatalError = true;
//             Otto.get().post(LoadingStateChangedEvent.produce(new RequestsState(RequestsState.GeneralState.ERROR)));
//             return false;
//         }
// 
//         mLoadingRequests.add(requestType);
//         Otto.get().post(LoadingStateChangedEvent.produce(new RequestsState(RequestsState.GeneralState.LOADING,
//                 mLoadingRequests, mLoadedRequests)));
//         return true;
//     }
// 
//     @Override
//     public void onRequestFinished(RequestType requestType) {
//         if (mIsFatalError) {
//             Otto.get().post(LoadingStateChangedEvent.produce(new RequestsState(RequestsState.GeneralState.ERROR)));
//             return;
//         }
//         mLoadingRequests.remove(requestType);
//         mLoadedRequests.add(requestType);
//         if (mLoadedRequests.size() >= RequestType.values().length && mLoadingRequests.isEmpty()) {
//             Otto.get().post(LoadingStateChangedEvent.produce(new RequestsState(RequestsState.GeneralState.LOADED)));
//         } else {
//             Otto.get().post(LoadingStateChangedEvent.produce(new RequestsState(RequestsState.GeneralState.LOADING,
//                     mLoadingRequests, mLoadedRequests)));
//         }
//     }
// 
//     @Override
//     public void reset() {
//         mLoadingRequests.clear();
//         mLoadedRequests.clear();
//         mIsFatalError = false;
// 
//         Otto.get().post(LoadingStateChangedEvent.produce(new RequestsState(RequestsState.GeneralState.WIATING)));
//     }
// }
// 
// Path: ParallelRequests/app/src/main/java/itis/homework/parallelrequests/network/RequestProcessor.java
// public class RequestProcessor implements RequestsService {
// 
//     private final RequestsController mRequestsController;
// 
//     public RequestProcessor(RequestsController requestsController) {
//         mRequestsController = requestsController;
//     }
// 
//     @Override
//     public Observable<Boolean> config() {
//         return process(RequestType.CONFIG);
//     }
// 
//     @Override
//     public Observable<Boolean> auth() {
//         return process(RequestType.AUTH);
//     }
// 
//     @Override
//     public Observable<Boolean> friends() {
//         return process(RequestType.FRIENDS);
//     }
// 
//     @Override
//     public Observable<Boolean> posts() {
//         return process(RequestType.POSTS);
//     }
// 
//     @Override
//     public Observable<Boolean> groups() {
//         return process(RequestType.GROUPS);
//     }
// 
//     @Override
//     public Observable<Boolean> messages() {
//         return process(RequestType.MESSAGES);
//     }
// 
//     @Override
//     public Observable<Boolean> photos() {
//         return process(RequestType.PHOTOS);
//     }
// 
//     @Override
//     public void reset() {
//         mRequestsController.reset();
//     }
// 
//     @NonNull
//     private Observable<Boolean> process(RequestType requestType) {
//         return Observable.create(new Observable.OnSubscribe<Boolean>() {
//             @Override
//             public void call(Subscriber<? super Boolean> subscriber) {
//                 if (Looper.myLooper() == Looper.getMainLooper()) {
//                     throw new NetworkOnMainThreadException();
//                 }
// 
//                 if (!mRequestsController.tryRequest(requestType)) {
//                     subscriber.onError(new Throwable());
//                 }
// 
//                 SystemClock.sleep(requestType.getDelay());
//                 mRequestsController.onRequestFinished(requestType);
// 
//                 subscriber.onCompleted();
//             }
//         });
//     }
// }
// 
// Path: ParallelRequests/app/src/main/java/itis/homework/parallelrequests/network/RequestsService.java
// public interface RequestsService {
// 
//     Observable<Boolean> config();
// 
//     Observable<Boolean> auth();
// 
//     Observable<Boolean> friends();
// 
//     Observable<Boolean> posts();
// 
//     Observable<Boolean> groups();
// 
//     Observable<Boolean> messages();
// 
//     Observable<Boolean> photos();
// 
//     void reset();
// 
// }


import android.app.Application;
import android.content.Context;
import android.support.annotation.NonNull;
import itis.homework.parallelrequests.network.RequestControllerImpl;
import itis.homework.parallelrequests.network.RequestProcessor;
import itis.homework.parallelrequests.network.RequestsService;
package itis.homework.parallelrequests.app;



/**
 * @author Artur Vasilov
 */
public class AppDelegate extends Application {

    private RequestsService mRequestsService;

// Path: ParallelRequests/app/src/main/java/itis/homework/parallelrequests/network/RequestControllerImpl.java
// public class RequestControllerImpl implements RequestsController {
// 
//     private final List<RequestType> mLoadingRequests;
// 
//     private final List<RequestType> mLoadedRequests;
// 
//     private boolean mIsFatalError = false;
// 
//     public RequestControllerImpl() {
//         mLoadingRequests = new ArrayList<>();
//         mLoadedRequests = new ArrayList<>();
//     }
// 
//     @Override
//     public boolean tryRequest(RequestType requestType) {
//         if (mIsFatalError) {
//             return false;
//         }
//         if (mLoadingRequests.contains(requestType) || mLoadedRequests.contains(requestType)) {
//             return false;
//         }
// 
//         if (requestType == RequestType.AUTH || requestType == RequestType.CONFIG) {
//             mLoadingRequests.add(requestType);
//             Otto.get().post(LoadingStateChangedEvent.produce(new RequestsState(RequestsState.GeneralState.LOADING,
//                     mLoadingRequests, mLoadedRequests)));
//             return true;
//         }
// 
//         if ((requestType == RequestType.FRIENDS || requestType == RequestType.POSTS
//                 || requestType == RequestType.MESSAGES || requestType == RequestType.GROUPS)
//                 && (!mLoadedRequests.contains(RequestType.AUTH) || !mLoadedRequests.contains(RequestType.CONFIG))) {
//             mIsFatalError = true;
//             Otto.get().post(LoadingStateChangedEvent.produce(new RequestsState(RequestsState.GeneralState.ERROR)));
//             return false;
//         }
// 
//         if (requestType == RequestType.PHOTOS && !mLoadedRequests.contains(RequestType.MESSAGES)) {
//             mIsFatalError = true;
//             Otto.get().post(LoadingStateChangedEvent.produce(new RequestsState(RequestsState.GeneralState.ERROR)));
//             return false;
//         }
// 
//         mLoadingRequests.add(requestType);
//         Otto.get().post(LoadingStateChangedEvent.produce(new RequestsState(RequestsState.GeneralState.LOADING,
//                 mLoadingRequests, mLoadedRequests)));
//         return true;
//     }
// 
//     @Override
//     public void onRequestFinished(RequestType requestType) {
//         if (mIsFatalError) {
//             Otto.get().post(LoadingStateChangedEvent.produce(new RequestsState(RequestsState.GeneralState.ERROR)));
//             return;
//         }
//         mLoadingRequests.remove(requestType);
//         mLoadedRequests.add(requestType);
//         if (mLoadedRequests.size() >= RequestType.values().length && mLoadingRequests.isEmpty()) {
//             Otto.get().post(LoadingStateChangedEvent.produce(new RequestsState(RequestsState.GeneralState.LOADED)));
//         } else {
//             Otto.get().post(LoadingStateChangedEvent.produce(new RequestsState(RequestsState.GeneralState.LOADING,
//                     mLoadingRequests, mLoadedRequests)));
//         }
//     }
// 
//     @Override
//     public void reset() {
//         mLoadingRequests.clear();
//         mLoadedRequests.clear();
//         mIsFatalError = false;
// 
//         Otto.get().post(LoadingStateChangedEvent.produce(new RequestsState(RequestsState.GeneralState.WIATING)));
//     }
// }
// 
// Path: ParallelRequests/app/src/main/java/itis/homework/parallelrequests/network/RequestProcessor.java
// public class RequestProcessor implements RequestsService {
// 
//     private final RequestsController mRequestsController;
// 
//     public RequestProcessor(RequestsController requestsController) {
//         mRequestsController = requestsController;
//     }
// 
//     @Override
//     public Observable<Boolean> config() {
//         return process(RequestType.CONFIG);
//     }
// 
//     @Override
//     public Observable<Boolean> auth() {
//         return process(RequestType.AUTH);
//     }
// 
//     @Override
//     public Observable<Boolean> friends() {
//         return process(RequestType.FRIENDS);
//     }
// 
//     @Override
//     public Observable<Boolean> posts() {
//         return process(RequestType.POSTS);
//     }
// 
//     @Override
//     public Observable<Boolean> groups() {
//         return process(RequestType.GROUPS);
//     }
// 
//     @Override
//     public Observable<Boolean> messages() {
//         return process(RequestType.MESSAGES);
//     }
// 
//     @Override
//     public Observable<Boolean> photos() {
//         return process(RequestType.PHOTOS);
//     }
// 
//     @Override
//     public void reset() {
//         mRequestsController.reset();
//     }
// 
//     @NonNull
//     private Observable<Boolean> process(RequestType requestType) {
//         return Observable.create(new Observable.OnSubscribe<Boolean>() {
//             @Override
//             public void call(Subscriber<? super Boolean> subscriber) {
//                 if (Looper.myLooper() == Looper.getMainLooper()) {
//                     throw new NetworkOnMainThreadException();
//                 }
// 
//                 if (!mRequestsController.tryRequest(requestType)) {
//                     subscriber.onError(new Throwable());
//                 }
// 
//                 SystemClock.sleep(requestType.getDelay());
//                 mRequestsController.onRequestFinished(requestType);
// 
//                 subscriber.onCompleted();
//             }
//         });
//     }
// }
// 
// Path: ParallelRequests/app/src/main/java/itis/homework/parallelrequests/network/RequestsService.java
// public interface RequestsService {
// 
//     Observable<Boolean> config();
// 
//     Observable<Boolean> auth();
// 
//     Observable<Boolean> friends();
// 
//     Observable<Boolean> posts();
// 
//     Observable<Boolean> groups();
// 
//     Observable<Boolean> messages();
// 
//     Observable<Boolean> photos();
// 
//     void reset();
// 
// }


// Path: ParallelRequests/app/src/main/java/itis/homework/parallelrequests/app/AppDelegate.java
import android.app.Application;
import android.content.Context;
import android.support.annotation.NonNull;
import itis.homework.parallelrequests.network.RequestControllerImpl;
import itis.homework.parallelrequests.network.RequestProcessor;
import itis.homework.parallelrequests.network.RequestsService;

package itis.homework.parallelrequests.app;



/**
 * @author Artur Vasilov
 */
public class AppDelegate extends Application {

    private RequestsService mRequestsService;

    @Override