package io.github.plastix.forage.ui.cachedetail;


import android.content.res.Resources;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.TextView;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.mikepenz.community_material_typeface_library.CommunityMaterial;
import com.mikepenz.iconics.IconicsDrawable;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import io.github.plastix.forage.ForageApplication;
import io.github.plastix.forage.R;
import io.github.plastix.forage.data.local.Cache;
import io.github.plastix.forage.util.StringUtils;


/**
 * Fragment that is responsible for showing the Geocache detail UI.
 */
public class CacheDetailFragment extends Fragment implements CacheDetailView, AppBarLayout.OnOffsetChangedListener {

    private static final String EXTRA_CACHE_CODE = "CACHE_CODE";
    private static final int PERCENTAGE_TO_HIDE_FAB = 40;

    @Bind(R.id.cachedetail_appbar)
    AppBarLayout appBarLayout;

    @Bind(R.id.cachedetail_toolbar)
    Toolbar toolbar;

    @Bind(R.id.cachedetail_collapsing_toolbar)
    CollapsingToolbarLayout collapsingToolbarLayout;

    @Bind(R.id.cachedetail_fab)
    FloatingActionButton fab;

    @Bind(R.id.cachedetail_map)
    MapView map;

    @Bind(R.id.cachedetail_description)
    TextView description;

    @Bind(R.id.cachedetail_difficulty)
    TextView difficulty;

    @Bind(R.id.cachedetail_size)
    TextView size;

    @Bind(R.id.cachedetail_terrain)
    TextView terrain;

    @Inject
    CacheDetailPresenter presenter;

    @Inject
    Resources resources;

    private String cacheCode;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);

        this.cacheCode = getArguments().getString(EXTRA_CACHE_CODE);

        injectDependencies();
    }

    private void injectDependencies() {
        ForageApplication.getComponent(getContext())
                .plus(new CacheDetailModule(this)).injectTo(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_cache_detail, container, false);

        // Inject Butterknife views
        ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        setActivityActionBar();

        setupFab();

        // TODO Hacky fix for Google Maps
        map.onCreate(null);
        map.setVisibility(View.GONE);

        presenter.getGeocache(cacheCode);
    }

    private void setActivityActionBar() {
        AppCompatActivity parent = ((AppCompatActivity) getActivity());
        parent.setSupportActionBar(toolbar);

        parent.getDelegate().getSupportActionBar().setDisplayShowHomeEnabled(true);
        parent.getDelegate().getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    private void setupFab() {
        appBarLayout.addOnOffsetChangedListener(this);

        fab.setImageDrawable(new IconicsDrawable(getContext(), CommunityMaterial.Icon.cmd_compass).color(Color.WHITE));
        // TODO Fab clicks
    }

    @Override
    public void returnedGeocache(Cache cache) {
        collapsingToolbarLayout.setTitle(cache.getName());
        description.setText(cache.getDescription());

        difficulty.setText(String.valueOf(cache.getDifficulty()));
        terrain.setText(String.valueOf(cache.getTerrain()));
        size.setText(cache.getSize());

        MapListener mapListener = new MapListener(cache.getLocation());
        map.getMapAsync(mapListener);
    }

    @Override
    public void onError() {
        getActivity().finish();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    /**
     * Callback to animate the FAB in and out depending on the scroll
     */
    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
        int currentScrollPercentage = (Math.abs(i)) * 100 / appBarLayout.getTotalScrollRange();

        if (currentScrollPercentage >= PERCENTAGE_TO_HIDE_FAB) {
            fab.hide();
        } else {
            fab.show();
        }
    }

    /**
     * Class to encapsulate Google Map config logic.
     */
    private class MapListener implements OnMapReadyCallback {

        private Location location;

        public MapListener(String location) {
            this.location = StringUtils.stringToLocation(location);
        }

        @Override
        public void onMapReady(GoogleMap googleMap) {
            googleMap.getUiSettings().setMapToolbarEnabled(false);

            // Add marker for geocache and move camera
            LatLng markerPos = new LatLng(location.getLatitude(), location.getLongitude());
            googleMap.addMarker(new MarkerOptions()
                    .position(markerPos));

            CameraPosition camerPosition = new CameraPosition.Builder()
                    .target(markerPos)
                    .zoom(13).build();

            googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(camerPosition));

            animateIntoView();
        }

        private void animateIntoView() {
            map.setVisibility(View.VISIBLE);
            AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
            animation.setDuration(500);
            map.startAnimation(animation);
        }

    }

}

@@ -1562,46 +1562,8 @@
 View
-, AppBarLayout.OnOffsetChangedListener
  %7B%0A%0A
@@ -1630,133 +1630,8 @@
 DE%22;
-%0A    private static final int PERCENTAGE_TO_HIDE_FAB = 40;%0A%0A    @Bind(R.id.cachedetail_appbar)%0A    AppBarLayout appBarLayout;
 %0A%0A  
@@ -3790,64 +3790,8 @@
 ) %7B%0A
-        appBarLayout.addOnOffsetChangedListener(this);%0A%0A
     
@@ -3902,24 +3902,24 @@
 or.WHITE));%0A
+
         // T
@@ -4110,25 +4110,24 @@
 ription());%0A
-%0A
         diff
@@ -4551,32 +4551,32 @@
 nDestroyView();%0A
+
         ButterKn
@@ -4604,420 +4604,8 @@
  %7D%0A%0A
-    /**%0A     * Callback to animate the FAB in and out depending on the scroll%0A     */%0A    @Override%0A    public void onOffsetChanged(AppBarLayout appBarLayout, int i) %7B%0A        int currentScrollPercentage = (Math.abs(i)) * 100 / appBarLayout.getTotalScrollRange();%0A%0A        if (currentScrollPercentage %3E= PERCENTAGE_TO_HIDE_FAB) %7B%0A            fab.hide();%0A        %7D else %7B%0A            fab.show();%0A        %7D%0A    %7D%0A%0A
     

Remove old unused Fab code