package org.lagonette.android.app.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MediatorLiveData;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;

import org.lagonette.android.app.arch.EventShipper;
import org.lagonette.android.app.arch.LiveEvent;
import org.lagonette.android.app.arch.MutableLiveEvent;

public class SharedMapsActivityViewModel extends AndroidViewModel {

    @NonNull
    private final MutableLiveData<String> mSearch;

    @NonNull
    private final MutableLiveData<Boolean> mWorkInProgress;

    @NonNull
    private final MutableLiveEvent<Void> mMapIsReady;

    @NonNull
    private final MutableLiveData<Boolean> mEnableMyPositionFAB;

    @NonNull
    private final MutableLiveEvent<ShowPartnerEvent> mShowPartnerRequest;

    @NonNull
    private final ShowPartnerEvent mShowPartnerEvent;

    @NonNull
    private final MutableLiveEvent<Integer> mMapMovement;

    @NonNull
    private final MutableLiveData<Integer> mMapTopPadding;

    @NonNull
    private final MutableLiveData<Integer> mMapBottomPadding;

    @NonNull
    private final MediatorLiveData<Integer[]> mMapPadding;

    public SharedMapsActivityViewModel(Application application) {
        super(application);
        mWorkInProgress = new MutableLiveData<>();
        mMapIsReady = new MutableLiveEvent<>();
        mEnableMyPositionFAB = new MutableLiveData<>();
        mShowPartnerRequest = new MutableLiveEvent<>();
        mMapMovement = new MutableLiveEvent<>();
        mSearch = new MutableLiveData<>();
        mMapTopPadding = new MutableLiveData<>();
        mMapBottomPadding = new MutableLiveData<>();
        mMapPadding = new MediatorLiveData<>();

        mShowPartnerEvent = new ShowPartnerEvent();

        mSearch.postValue(""); // TODO Not sure if usefull

        mMapPadding.addSource(
                mMapTopPadding,
                topPadding -> mMapPadding.setValue(
                        new Integer[] {
                                0,
                                topPadding,
                                0,
                                mMapBottomPadding.getValue()
                        }
                )
        );
        mMapPadding.addSource(
                mMapBottomPadding,
                bottomPadding -> mMapPadding.setValue(
                        new Integer[] {
                                0,
                                mMapTopPadding.getValue(),
                                0,
                                bottomPadding
                        }
                )
        );
    }

    @NonNull
    public LiveData<String> getSearch() {
        return mSearch;
    }

    @NonNull
    public LiveData<Boolean> getWorkInProgress() {
        return mWorkInProgress;
    }

    @NonNull
    public LiveEvent<Void> getMapIsReady() {
        return mMapIsReady;
    }

    @NonNull
    public LiveData<Boolean> getEnableMyPositionFAB() {
        return mEnableMyPositionFAB;
    }

    @NonNull
    public LiveEvent<ShowPartnerEvent> getShowPartnerRequest() {
        return mShowPartnerRequest;
    }

    @NonNull
    public EventShipper.Sender<Integer> getMapMovementSender() {
        return mMapMovement;
    }

    @NonNull
    public LiveEvent<Integer> getMapMovement() {
        return mMapMovement;
    }

    @NonNull
    public MutableLiveData<Integer> getMapTopPadding() {
        return mMapTopPadding;
    }

    @NonNull
    public MutableLiveData<Integer> getMapBottomPadding() {
        return mMapBottomPadding;
    }

    @NonNull
    public LiveData<Integer[]> getMapPadding() {
        return mMapPadding;
    }

    public void search(@NonNull String search) {
        mSearch.setValue(search);
    }

    public void setWorkInProgress(boolean workInProgress) {
        mWorkInProgress.postValue(workInProgress);
    }

    public void callMapIsReady() {
        mMapIsReady.call();
    }

    public void setEnableMyPositionFAB(boolean enable) {
        mEnableMyPositionFAB.postValue(enable);
    }

    public void showPartner(long partnerId, boolean zoom) {
        mShowPartnerEvent.partnerId = partnerId;
        mShowPartnerEvent.zoom = zoom;
        mShowPartnerRequest.send(mShowPartnerEvent);
    }

    public void showFullMap() {
        mShowPartnerRequest.call();
    }

    public static class ShowPartnerEvent {

        public long partnerId;

        public boolean zoom;

    }

}

@@ -454,16 +454,63 @@
 veEvent;
+%0Aimport org.lagonette.android.util.IntegerUtil;
 %0A%0Apublic
@@ -1279,31 +1279,27 @@
 torLiveData%3C
-I
+i
 nt
-eger
 %5B%5D%3E mMapPadd
@@ -1966,16 +1966,67 @@
 sefull%0A%0A
+        mMapPadding.setValue(new int%5B%5D %7B0,0,0,0%7D);%0A
         
@@ -2114,194 +2114,139 @@
  -%3E 
-mMapPadding.setValue(%0A                        new Integer%5B%5D %7B%0A                                0,%0A                                topPadding,%0A                                0,%0A          
+%7B%0A                    int%5B%5D paddings = mMapPadding.getValue();%0A                    paddings%5B1%5D = IntegerUtil.intValue(topPadding);%0A
     
@@ -2265,28 +2265,20 @@
     
-  
 mMap
-Bottom
 Padding.
 getV
@@ -2273,25 +2273,25 @@
 Padding.
-g
+s
 etValue(
 )%0A      
@@ -2282,26 +2282,27 @@
 etValue(
-)%0A        
+paddings);%0A
         
@@ -2311,34 +2311,16 @@
       %7D%0A
-                )%0A
         
@@ -2425,144 +2425,51 @@
  -%3E 
-mMapPadding.setValue(%0A                        new Integer%5B%5D %7B%0A                                0,%0A                               
+%7B%0A                    int%5B%5D paddings =
  mMap
-Top
 Padd
@@ -2486,77 +2486,65 @@
 ue()
-,%0A                                0,%0A                                
+;%0A                    paddings%5B3%5D = IntegerUtil.intValue(
 bott
@@ -2552,16 +2552,18 @@
 mPadding
+);
 %0A       
@@ -2567,37 +2567,63 @@
                 
-    %7D
+mMapPadding.setValue(paddings);
 %0A               
@@ -2623,17 +2623,17 @@
         
-)
+%7D
 %0A       
@@ -3626,23 +3626,19 @@
 iveData%3C
-I
+i
 nt
-eger
 %5B%5D%3E getM

Fix NPE on maps padding live data