package com.cloudycrew.cloudycar.summarycontainer;

import com.cloudycrew.cloudycar.ViewController;
import com.cloudycrew.cloudycar.controllers.UserController;
import com.cloudycrew.cloudycar.models.requests.CompletedRequest;
import com.cloudycrew.cloudycar.models.requests.PendingRequest;
import com.cloudycrew.cloudycar.models.requests.Request;
import com.cloudycrew.cloudycar.observables.IObserver;
import com.cloudycrew.cloudycar.requeststorage.IRequestStore;

import java.util.Date;

import rx.Observable;

/**
 * Created by George on 2016-11-23.
 */

public class SummaryMenuController extends ViewController<ISummaryMenuView> {
    private IRequestStore requestStore;
    private UserController userController;

    public SummaryMenuController(UserController userController, IRequestStore requestStore) {
        this.userController = userController;
        this.requestStore = requestStore;
    }

    @Override
    public void attachView(ISummaryMenuView view) {
        super.attachView(view);
        requestStore.addObserver(requestStoreObserver);
    }

    @Override
    public void detachView() {
        super.detachView();
        requestStore.removeObserver(requestStoreObserver);
    }

    private IObserver<IRequestStore> requestStoreObserver = store -> {
        dispatchDisplayUnreadRiderRequests(getTotalUnreadRiderRequests());
        dispatchDisplayUnreadDriverRequests(getTotalUnreadDriverRequests());
    };

    private int getTotalUnreadRiderRequests() {
        return Observable.from(requestStore.getRequests())
                         .filter(r -> r.getRider().equals(getCurrentUsername()))
                         .filter(r -> !isRequestRead(r))
                         .count()
                         .toBlocking()
                         .first();
    }

    private int getTotalUnreadDriverRequests() {
        return Observable.from(requestStore.getRequests())
                         .filter(r -> isCurrentUserADriverForRequest(r))
                         .filter(r -> !isRequestRead(r))
                         .count()
                         .toBlocking()
                         .first();
    }

    private boolean isCurrentUserADriverForRequest(Request request) {
        if (request instanceof PendingRequest) {
            PendingRequest pendingRequest = (PendingRequest) request;
            return pendingRequest.hasBeenAcceptedBy(getCurrentUsername());
        } else if (request instanceof CompletedRequest) {
            CompletedRequest completedRequest = (CompletedRequest) request;
            return completedRequest.getDriverUsername().equals(getCurrentUsername());
        }
        return false;
    }

    private boolean isRequestRead(Request request) {
        Date lastUserReadTime = userController.getLastReadTime(request.getId());

        return lastUserReadTime == null ||
                request.getLastUpdated().compareTo(lastUserReadTime) > 0;
    }

    private String getCurrentUsername() {
        return userController.getCurrentUser().getUsername();
    }

    private void dispatchDisplayUnreadRiderRequests(int numUnread) {
        if (isViewAttached()) {
            getView().displayTotalUnreadRiderRequests(numUnread);
        }
    }

    private void dispatchDisplayUnreadDriverRequests(int numUnread) {
        if (isViewAttached()) {
            getView().displayTotalUnreadDriverRequests(numUnread);
        }
    }
}

@@ -267,23 +267,25 @@
 equests.
-Pending
+Confirmed
 Request;
@@ -1944,38 +1944,37 @@
  -%3E 
-isCurrentUserA
+should
 Driver
-For
 Request
+BeNotified
 (r))
@@ -2170,175 +2170,53 @@
 ean 
-isCurrentUserA
+should
 Driver
-For
 Request
-(Request request) %7B%0A        if (request instanceof PendingRequest) %7B%0A            PendingRequest pendingRequest = (Pending
+BeNotified(
 Request
-)
  request
 ;%0A  
@@ -2215,84 +2215,11 @@
 uest
-;%0A            return pendingRequest.hasBeenAcceptedBy(getCurrentUsername());
+) %7B
 %0A   
@@ -2227,15 +2227,8 @@
     
-%7D else 
 if (
@@ -2248,21 +2248,21 @@
 nceof Co
-mplet
+nfirm
 edReques
@@ -2280,21 +2280,21 @@
       Co
-mplet
+nfirm
 edReques
@@ -2297,21 +2297,21 @@
 quest co
-mplet
+nfirm
 edReques
@@ -2317,21 +2317,21 @@
 st = (Co
-mplet
+nfirm
 edReques
@@ -2363,21 +2363,21 @@
 eturn co
-mplet
+nfirm
 edReques
@@ -2638,18 +2638,18 @@
 ime 
-=
+!
 = null 
-%7C%7C
+&&
 %0A   
@@ -2665,59 +2665,59 @@
     
-request.getLastUpdated().compareTo(lastUserReadTime
+lastUserReadTime.compareTo(request.getLastUpdated()
 ) %3E 

Address issues in the SummaryMenuController