package es.craftsmanship.toledo.katangapp.activities;

import es.craftsmanship.toledo.katangapp.R;
import es.craftsmanship.toledo.katangapp.fragments.RouteMapFragment;
import es.craftsmanship.toledo.katangapp.fragments.RouteFragment;
import es.craftsmanship.toledo.katangapp.maps.GoogleMapsCameraHelper;
import es.craftsmanship.toledo.katangapp.models.BusStop;
import es.craftsmanship.toledo.katangapp.models.Route;

import android.content.Intent;

import android.os.Bundle;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;

import android.view.Menu;
import android.view.MenuItem;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import java.util.List;

/**
 * @author Cristóbal Hermida
 */
public class RouteMapActivity extends BaseGeoLocatedActivity implements OnMapReadyCallback {

    private static final String MAP_KEY = "MAPA";
    private static final String ROUTE_KEY = "RUTA";

    private List<BusStop> busStopResults;
    private GoogleMap googleMap;
    private RouteMapFragment mapFragment;
    private SectionsPagerAdapter sectionsPagerAdapter;
    private TabLayout tabLayout;
    private ViewPager viewPager;

    @Override
    public void onMapReady(GoogleMap googleMap) {
        this.googleMap = googleMap;

        this.googleMap.getUiSettings().setZoomControlsEnabled(true);
        this.googleMap.getUiSettings().setMyLocationButtonEnabled(true);
        this.googleMap.getUiSettings().setMapToolbarEnabled(true);
        this.googleMap.getUiSettings().setCompassEnabled(true);

        if (!busStopResults.isEmpty()) {
            for (BusStop stop : busStopResults) {
                LatLng locStop = new LatLng(stop.getLatitude(), stop.getLongitude());

                this.googleMap.addMarker(
                    new MarkerOptions().position(locStop).title(stop.getAddress()));
            }
        }

        LatLng locStopIni = null;

        if (getLatitude() != null && getLongitude() != null) {
            locStopIni  = new LatLng( getLatitude(), getLongitude());
        }
        else {
            BusStop busStop = busStopResults.get(0);

            locStopIni = new LatLng(busStop.getLatitude(), busStop.getLongitude());
        }

        this.googleMap.addMarker(
            new MarkerOptions()
                .position(locStopIni)
                .title("Mi situación")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN)));

        googleMap.moveCamera(GoogleMapsCameraHelper.getCameraUpdate(locStopIni, 12));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);

                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_route_map);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        Intent intent = getIntent();

        if (intent.hasExtra("route") && (intent.getSerializableExtra("route") != null)) {
            Route route = (Route) intent.getSerializableExtra("route");

            setTitle(route.getName());

            busStopResults = route.getBusStops();
            sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(),route);
        }
        else {
            sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(),null);
        }

        viewPager = (ViewPager) findViewById(R.id.container);

        viewPager.setAdapter(sectionsPagerAdapter);

        tabLayout = (TabLayout) findViewById(R.id.tabs);

        tabLayout.setupWithViewPager(viewPager);
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public Route route;

        public SectionsPagerAdapter(FragmentManager fm, Route route) {
            super(fm);

            this.route=route;
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    mapFragment = RouteMapFragment.newInstance();

                    mapFragment.getMapAsync(RouteMapActivity.this);

                    return mapFragment;
                case 1:
                    return RouteFragment.newInstance(route);
            }

            return null;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return MAP_KEY;
                case 1:
                    return ROUTE_KEY;
            }

            return null;
        }

    }

}
@@ -785,34 +785,8 @@
 r;%0A%0A
-import android.view.Menu;%0A
 impo
@@ -1388,204 +1388,8 @@
 lts;
-%0A    private GoogleMap googleMap;%0A    private RouteMapFragment mapFragment;%0A    private SectionsPagerAdapter sectionsPagerAdapter;%0A    private TabLayout tabLayout;%0A    private ViewPager viewPager;
 %0A%0A  
@@ -1462,50 +1462,8 @@
     
-this.googleMap = googleMap;%0A%0A        this.
 goog
@@ -1514,37 +1514,32 @@
 (true);%0A        
-this.
 googleMap.getUiS
@@ -1582,37 +1582,32 @@
 (true);%0A        
-this.
 googleMap.getUiS
@@ -1644,37 +1644,32 @@
 (true);%0A        
-this.
 googleMap.getUiS
@@ -1890,37 +1890,32 @@
                 
-this.
 googleMap.addMar
@@ -2058,15 +2058,8 @@
 pIni
- = null
 ;%0A%0A 
@@ -2372,21 +2372,16 @@
         
-this.
 googleMa
@@ -3362,24 +3362,75 @@
 tIntent();%0A%0A
+        SectionsPagerAdapter sectionsPagerAdapter;%0A
         if (
@@ -3897,32 +3897,42 @@
      %7D%0A%0A        
+ViewPager 
 viewPager = (Vie
@@ -4023,32 +4023,42 @@
 pter);%0A%0A        
+TabLayout 
 tabLayout = (Tab
@@ -4733,32 +4733,49 @@
                 
+RouteMapFragment 
 mapFragment = Ro

use local variables