/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * 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 android.support.design.widget;

import android.os.Build;
import android.support.v4.view.ViewCompat;
import android.view.View;
import android.view.ViewParent;

/**
 * Utility helper for moving a {@link android.view.View} around using
 * {@link android.view.View#offsetLeftAndRight(int)} and
 * {@link android.view.View#offsetTopAndBottom(int)}.
 * <p>
 * Also the setting of absolute offsets (similar to translationX/Y), rather than additive
 * offsets.
 */
class ViewOffsetHelper {

    private final View mView;

    private int mLayoutTop;
    private int mLayoutLeft;
    private int mOffsetTop;
    private int mOffsetLeft;

    public ViewOffsetHelper(View view) {
        mView = view;
    }

    public void onViewLayout() {
        // Now grab the intended top
        mLayoutTop = mView.getTop();
        mLayoutLeft = mView.getLeft();

        // And offset it as needed
        updateOffsets();
    }

    private void updateOffsets() {
        ViewCompat.offsetTopAndBottom(mView, mOffsetTop - (mView.getTop() - mLayoutTop));
        ViewCompat.offsetLeftAndRight(mView, mOffsetLeft - (mView.getLeft() - mLayoutLeft));

        // Manually invalidate the parent to make sure we get drawn pre-M
        if (Build.VERSION.SDK_INT < 23) {
            tickleParentInvalidationFlag(mView);
        }
    }

    private static void tickleParentInvalidationFlag(View view) {
        ViewParent vp = view.getParent();
        if (vp instanceof View) {
            View parent = (View) vp;
            final float x = ViewCompat.getTranslationX(parent);
            ViewCompat.setTranslationX(parent, x + 1);
            ViewCompat.setTranslationX(parent, x);
        }
    }

    /**
     * Set the top and bottom offset for this {@link ViewOffsetHelper}'s view.
     *
     * @param offset the offset in px.
     * @return true if the offset has changed
     */
    public boolean setTopAndBottomOffset(int offset) {
        if (mOffsetTop != offset) {
            mOffsetTop = offset;
            updateOffsets();
            return true;
        }
        return false;
    }

    /**
     * Set the left and right offset for this {@link ViewOffsetHelper}'s view.
     *
     * @param offset the offset in px.
     * @return true if the offset has changed
     */
    public boolean setLeftAndRightOffset(int offset) {
        if (mOffsetLeft != offset) {
            mOffsetLeft = offset;
            updateOffsets();
            return true;
        }
        return false;
    }

    public int getTopAndBottomOffset() {
        return mOffsetTop;
    }

    public int getLeftAndRightOffset() {
        return mOffsetLeft;
    }
}
@@ -1789,16 +1789,25 @@
 ate the 
+view and 
 parent t
@@ -1893,30 +1893,24 @@
       tickle
-Parent
 Invalidation
@@ -1934,90 +1934,17 @@
     
-%7D%0A
     
-%7D%0A%0A    private static void tickleParentInvalidationFlag(View view) %7B%0A       
+final
  Vie
@@ -1956,17 +1956,18 @@
 nt vp = 
-v
+mV
 iew.getP
@@ -1971,24 +1971,28 @@
 etParent();%0A
+    
         if (
@@ -2029,22 +2029,35 @@
     
-View parent = 
+    tickleInvalidationFlag(
 (Vie
@@ -2061,22 +2061,110 @@
 View) vp
+)
 ;%0A    
+        %7D%0A        %7D%0A    %7D%0A%0A    private static void tickleInvalidationFlag(View view) %7B%0A
         
@@ -2210,21 +2210,15 @@
 onX(
-parent);%0A    
+view);%0A
     
@@ -2246,24 +2246,22 @@
 nslation
-X(parent
+Y(view
 , x + 1)
@@ -2262,20 +2262,16 @@
 x + 1);%0A
-    
         
@@ -2299,31 +2299,19 @@
 tion
-X(parent, x);%0A        %7D
+Y(view, x);
 %0A   

Fix AppBarLayout not being drawn after rotation