package com.alexstyl.specialdates.home;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.support.transition.Fade;
import android.support.transition.Transition;
import android.support.transition.TransitionManager;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.alexstyl.android.Version;
import com.alexstyl.specialdates.R;
import com.alexstyl.specialdates.transition.FadeInTransition;
import com.alexstyl.specialdates.transition.FadeOutTransition;
import com.alexstyl.specialdates.transition.SimpleTransitionListener;
import com.alexstyl.specialdates.ui.ViewFader;
import com.alexstyl.specialdates.upcoming.view.ExposedSearchToolbar;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

final class SearchTransitioner {

    private final Activity activity;
    private final HomeNavigator navigator;
    private final ViewGroup activityContent;
    private final ExposedSearchToolbar toolbar;
    private final FrameLayout toolbarHolder;
    private final ViewFader viewFader;

    private final int toolbarMargin;
    private boolean transitioning;

    SearchTransitioner(Activity activity,
                       HomeNavigator navigator,
                       ViewGroup activityContent,
                       ExposedSearchToolbar toolbar,
                       FrameLayout toolbarHolder,
                       ViewFader viewFader) {
        this.activity = activity;
        this.navigator = navigator;
        this.activityContent = activityContent;
        this.toolbar = toolbar;
        this.toolbarHolder = toolbarHolder;
        this.viewFader = viewFader;
        this.toolbarMargin = activity.getResources().getDimensionPixelSize(R.dimen.padding_tight);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    void transitionToSearch() {
        if (transitioning) {
            return;
        }
        if (supportsTransitions()) {
            Transition transition = FadeOutTransition.withAction(navigateToSearchWhenDone());
            TransitionManager.beginDelayedTransition(toolbar, transition);
            toolbarHolder.setElevation(activity.getResources().getDimensionPixelSize(R.dimen.toolbar_elevation));
            expandToolbar();
            viewFader.hideContentOf(toolbar);

            TransitionManager.beginDelayedTransition(activityContent, new Fade(Fade.OUT));
            activityContent.setVisibility(GONE);
        } else {
            navigator.toSearch(activity);
        }
    }

    private void expandToolbar() {
        FrameLayout.LayoutParams frameLP = (FrameLayout.LayoutParams) toolbar.getLayoutParams();
        frameLP.setMargins(0, 0, 0, 0);
        toolbar.setLayoutParams(frameLP);
    }

    private Transition.TransitionListener navigateToSearchWhenDone() {
        return new SimpleTransitionListener() {

            @Override
            public void onTransitionStart(Transition transition) {
                transitioning = true;
            }

            @Override
            public void onTransitionEnd(Transition transition) {
                transitioning = false;
                navigator.toSearch(activity);
                activity.overridePendingTransition(0, 0);
            }
        };
    }

    private static boolean supportsTransitions() {
        return Version.hasKitKat();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    void onActivityResumed() {
        if (supportsTransitions()) {
            TransitionManager.beginDelayedTransition(toolbar, FadeInTransition.createTransition());
            FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) toolbar.getLayoutParams();
            layoutParams.setMargins(toolbarMargin, toolbarMargin, toolbarMargin, toolbarMargin);
            viewFader.showContent(toolbar);
            toolbar.setLayoutParams(layoutParams);

            TransitionManager.beginDelayedTransition(activityContent, new Fade(Fade.IN));
            activityContent.setVisibility(VISIBLE);
            toolbarHolder.setElevation(0);
        }
    }
}

@@ -260,24 +260,67 @@
 ionManager;%0A
+import android.support.v4.view.ViewCompat;%0A
 import andro
@@ -338,16 +338,16 @@
 wGroup;%0A
-
 import a
@@ -2179,37 +2179,34 @@
             
-toolbarHolder
+ViewCompat
 .setElevatio
@@ -2203,24 +2203,39 @@
 etElevation(
+toolbarHolder, 
 activity.get
@@ -4094,29 +4094,26 @@
         
-toolbarHolder
+ViewCompat
 .setElev
@@ -4118,16 +4118,31 @@
 evation(
+toolbarHolder, 
 0);%0A    

Use ViewCompat to set Elevation