/*
 * Copyright 2015 Adam Speakman
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package speakman.whatsshakingnz.ui.activities;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import com.google.android.gms.maps.MapView;

import javax.inject.Inject;

import speakman.whatsshakingnz.R;
import speakman.whatsshakingnz.WhatsShakingApplication;
import speakman.whatsshakingnz.databinding.ActivityDetailBinding;
import speakman.whatsshakingnz.model.Earthquake;
import speakman.whatsshakingnz.model.EarthquakeStore;
import speakman.whatsshakingnz.ui.viewmodel.EarthquakeOverviewViewModel;

public class DetailActivity extends AppCompatActivity implements EarthquakeStore.EarthquakeDataChangeObserver {

    public static String EXTRA_EARTHQUAKE = "speakman.whatsshakingnz.ui.activities.DetailActivity.EXTRA_EARTHQUAKE";
    public static Intent createIntent(Context ctx, Earthquake earthquake) {
        Intent intent = new Intent(ctx, DetailActivity.class);
        intent.putExtra(EXTRA_EARTHQUAKE, earthquake.getId());
        return intent;
    }

    @Inject
    EarthquakeStore store;

    private MapView map;
    private ActivityDetailBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WhatsShakingApplication.getInstance().inject(this);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_detail);
        map = (MapView) findViewById(R.id.activity_detail_map);
        map.onCreate(savedInstanceState);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Earthquake earthquake = getEarthquake();
        EarthquakeOverviewViewModel viewModel = new EarthquakeOverviewViewModel(earthquake);
        binding.setEarthquakeModel(viewModel);
    }

    @Override
    protected void onResume() {
        super.onResume();
        map.onResume();
        store.registerDataChangeObserver(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        map.onPause();
        store.unregisterDataChangeObserver(this);
    }

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

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

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

    @Override
    public void onEarthquakeDataChanged() {
        Earthquake earthquake = getEarthquake();
        EarthquakeOverviewViewModel viewModel = new EarthquakeOverviewViewModel(earthquake);
        binding.setEarthquakeModel(viewModel);
    }

    private Earthquake getEarthquake() {
        Earthquake earthquake = store.getEarthquake(getIntent().getStringExtra(EXTRA_EARTHQUAKE));
        return earthquake;
    }
}

@@ -899,23 +899,278 @@
 ms.maps.
-MapView
+GoogleMap;%0Aimport com.google.android.gms.maps.MapView;%0Aimport com.google.android.gms.maps.OnMapReadyCallback;%0Aimport com.google.android.gms.maps.model.LatLng;%0Aimport com.google.android.gms.maps.model.Marker;%0Aimport com.google.android.gms.maps.model.MarkerOptions
 ;%0A%0Aimpor
@@ -1635,16 +1635,36 @@
 Observer
+, OnMapReadyCallback
  %7B%0A%0A    
@@ -2071,16 +2071,20 @@
 View map
+View
 ;%0A    pr
@@ -2119,16 +2119,46 @@
 binding;
+%0A    private Marker mapMarker;
 %0A%0A    @O
@@ -2419,16 +2419,20 @@
      map
+View
  = (MapV
@@ -2487,16 +2487,20 @@
      map
+View
 .onCreat
@@ -2636,186 +2636,18 @@
     
-Earthquake earthquake = getEarthquake();%0A        EarthquakeOverviewViewModel viewModel = new EarthquakeOverviewViewModel(earthquake);%0A        binding.setEarthquakeModel(viewModel
+refreshUI(
 );%0A 
@@ -2735,16 +2735,20 @@
      map
+View
 .onResum
@@ -2888,16 +2888,20 @@
      map
+View
 .onPause
@@ -3046,16 +3046,20 @@
      map
+View
 .onDestr
@@ -3199,16 +3199,20 @@
      map
+View
 .onSaveI
@@ -3328,16 +3328,20 @@
      map
+View
 .onLowMe
@@ -3425,45 +3425,221 @@
     
-Earthquake earthquake
+refreshUI();%0A    %7D%0A%0A    @Override%0A    public void onMapReady(GoogleMap googleMap) %7B%0A        if (mapMarker != null) %7B%0A            mapMarker.remove();%0A        %7D%0A        MarkerOptions markerOptions
  = get
-Earthquake
+MarkerOptions
 ();%0A
@@ -3650,81 +3650,146 @@
     
-EarthquakeOverviewViewModel viewModel = new EarthquakeOverviewViewModel(e
+mapMarker = googleMap.addMarker(markerOptions);%0A    %7D%0A%0A    private MarkerOptions getMarkerOptions() %7B%0A        Earthquake earthquake = getE
 arth
@@ -3793,16 +3793,17 @@
 rthquake
+(
 );%0A     
@@ -3809,44 +3809,124 @@
     
-binding.setEarthquakeModel(viewModel
+return new MarkerOptions()%0A                .position(new LatLng(earthquake.getLatitude(), earthquake.getLongitude())
 );%0A 
@@ -4103,11 +4103,275 @@
 e;%0A    %7D
+%0A%0A    private void refreshUI() %7B%0A        Earthquake earthquake = getEarthquake();%0A        EarthquakeOverviewViewModel viewModel = new EarthquakeOverviewViewModel(earthquake);%0A        binding.setEarthquakeModel(viewModel);%0A        mapView.getMapAsync(this);%0A    %7D%0A%0A
 %0A%7D%0A

Add map marker to detail view