package com.github.pedrovgs.sample.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import com.github.pedrovgs.DraggablePanel;
import com.github.pedrovgs.sample.R;
import com.github.pedrovgs.sample.fragment.PlaceFragment;
import com.github.pedrovgs.sample.viewmodel.PlaceViewModel;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.pedrogomez.renderers.RendererAdapter;

import javax.inject.Inject;

/**
 * @author Pedro Vicente Gómez Sánchez.
 */
public class PlacesSampleActivity extends DIFragmentActivity {

    @InjectView(R.id.lv_places)
    ListView lv_places;
    @InjectView(R.id.draggable_panel)
    DraggablePanel draggablePanel;

    @Inject
    RendererAdapter<PlaceViewModel> placesAdapter;

    private PlaceFragment placeFragment;
    private SupportMapFragment mapFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_place_sample);
        ButterKnife.inject(this);
        initializeFragments();
        initializeListView();
        initializeDraggablePanel();
    }


    private void initializeFragments() {
        placeFragment = new PlaceFragment();
        GoogleMapOptions options = new GoogleMapOptions();
        mapFragment = SupportMapFragment.newInstance(options);
    }

    private void initializeListView() {
        lv_places.setAdapter(placesAdapter);
        lv_places.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                draggablePanel.setVisibility(View.VISIBLE);
                draggablePanel.maximize();
                PlaceViewModel placeViewModel = placesAdapter.getItem(position);
                placeFragment.showPlace(placeViewModel);

                mapFragment.getMap().clear();
                LatLng latitudeLongitude = new LatLng(placeViewModel.getLatitude(), placeViewModel.getLongitude());
                mapFragment.getMap().addMarker(new MarkerOptions().position(latitudeLongitude));
                mapFragment.getMap().moveCamera(CameraUpdateFactory.newLatLng(latitudeLongitude));

            }
        });
    }

    private void initializeDraggablePanel() {
        draggablePanel.setFragmentManager(getSupportFragmentManager());
        draggablePanel.setTopFragment(placeFragment);
        draggablePanel.setBottomFragment(mapFragment);
        draggablePanel.setScaleFactor(getResources().getInteger(R.integer.scale_factor));
        draggablePanel.setTopViewHeight(getResources().getDimension(R.dimen.top_fragment_height));
        draggablePanel.setTopFragmentMarginRight(getResources().getDimension(R.dimen.top_fragment_margin));
        draggablePanel.setTopFragmentMarginBottom(getResources().getDimension(R.dimen.top_fragment_margin));
        draggablePanel.setBackgroundColor(getResources().getColor(R.color.black));
        draggablePanel.initializeView();
        draggablePanel.setVisibility(View.GONE);
    }


}

@@ -519,25 +519,18 @@
 oogleMap
-Options
 ;%0A
+
 import c
@@ -1560,121 +1560,401 @@
     
-GoogleMapOptions options = new GoogleMapOptions();%0A        mapFragment = SupportMapFragment.newInstance(options);
+mapFragment = new SupportMapFragment() %7B%0A            @Override%0A            public void onActivityCreated(Bundle savedInstanceState) %7B%0A                super.onActivityCreated(savedInstanceState);%0A                GoogleMap map = mapFragment.getMap();%0A                if (map != null) %7B%0A                    map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);%0A                %7D%0A            %7D%0A        %7D;%0A
 %0A   
@@ -2823,16 +2823,20 @@
 ewLatLng
+Zoom
 (latitud
@@ -2849,13 +2849,17 @@
 tude
+, 10f
 ));%0A
-%0A
     

Configure map to work in satellite mode