package com.revo.display.views.fragment;

import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.MapView;
import com.revo.display.R;
import com.revo.display.RevoApplication;
import com.revo.display.bluetooth.ValuesCallback;
import com.revo.display.network.RFirebase;
import com.revo.display.network.ValueCallback;
import com.revo.display.views.custom.Compass;

/**
 * Created by sihrc on 9/20/14.
 */
public class SpectatorFragment extends RevoFragment {
    private RFirebase ref = RevoApplication.app.getFireBaseHelper();
    private Compass compass;
    private MapView map;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.spectator_fragment, container, false);
        compass = (Compass) rootView.findViewById(R.id.compass);
        map = (MapView) rootView.findViewById(R.id.map);

        map.onCreate(savedInstanceState);

        updateMode();
        return rootView;
    }

    @Override
    public void onPause() {
        super.onPause();
        map.onPause();
        ref.deregisterListener(SpectatorFragment.class.getSimpleName() + "direction", new String[] {"driver", "direction"});
    }

    @Override
    public void onResume() {
        super.onResume();
        map.onResume();
        updateMode();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        map.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        map.onSaveInstanceState(outState);
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        map.onLowMemory();
    }

    @Override
    public String tag() {
        return SpectatorFragment.class.getSimpleName();
    }

    @Override
    public void setupDriverMode() {
        // Get data from firebase
        ref.registerListener(SpectatorFragment.class.getSimpleName() + "direction", new String[] {"driver", "direction"}, new ValueCallback() {
            @Override
            public void handleValue(Object value) {
                if (compass != null) {
                    compass.onValueChanged((Long) value);
                }
            }
        });
    }

    @Override
    public void setupNotDriverMode() {
        // Get data from firebase
        ref.registerListener(SpectatorFragment.class.getSimpleName() + "direction", new String[] {"driver", "direction"}, new ValueCallback() {
            @Override
            public void handleValue(Object value) {
                if (compass != null) {
                    compass.onValueChanged((Long) value);
                }
            }
        });
    }

    @Override
    public ValuesCallback getValuesCallback() {
        return null;
    }
}

@@ -535,16 +535,39 @@
 mpass;%0A%0A
+import java.util.Map;%0A%0A
 /**%0A * C
@@ -1414,24 +1414,153 @@
 rection%22%7D);%0A
+        ref.deregisterListener(SpectatorFragment.class.getSimpleName() + %22coordinates%22, new String%5B%5D %7B%22driver%22, %22coordinates%22%7D);%0A
     %7D%0A%0A    @
@@ -2197,32 +2197,181 @@
 pDriverMode() %7B%0A
+        generalSetup();%0A    %7D%0A%0A    @Override%0A    public void setupNotDriverMode() %7B%0A        generalSetup();%0A    %7D%0A%0A    private void generalSetup() %7B%0A
         // Get d
@@ -2751,101 +2751,8 @@
 %7D);%0A
-    %7D%0A%0A    @Override%0A    public void setupNotDriverMode() %7B%0A        // Get data from firebase
 %0A   
@@ -2816,33 +2816,35 @@
 leName() + %22
-direction
+coordinates
 %22, new Strin
@@ -2855,33 +2855,35 @@
 %7B%22driver%22, %22
-direction
+coordinates
 %22%7D, new Valu
@@ -2982,39 +2982,35 @@
             if (
-compass
+map
  != null) %7B%0A    
@@ -3029,89 +3029,721 @@
     
-compass.onValueChanged((Long) value);%0A                %7D%0A            %7D%0A        %7D);
+try %7B%0A                        Map%3CString, Object%3E coords = (Map%3CString, Object%3E) value;%0A                        double lat = (Double) coords.get(%22latitude%22);%0A                        double lon = (Double) coords.get(%22longitude%22);%0A                        addMapPosition(lat, lon);%0A                    %7D catch (ClassCastException e) %7B%0A                        Log.d(tag() + %22 handle coordinate%22, %22Problem casting the Map%3CString, Object%3E%22);%0A                        e.printStackTrace();%0A                    %7D%0A                %7D%0A            %7D%0A        %7D);%0A    %7D%0A%0A    private void addMapPosition(double lat, double lon) %7B%0A        if (map != null) %7B%0A            // add new line from previous coord to current coord%0A        %7D
 %0A   

add listener for driver coordinates