/*
 * Copyright (C) 2017 Hazuki
 *
 * 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 jp.hazuki.yuzubrowser.utils.view.behavior;

import android.content.Context;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.view.View;

import jp.hazuki.yuzubrowser.R;
import jp.hazuki.yuzubrowser.webkit.CustomWebView;

public class WebViewBehavior extends AppBarLayout.ScrollingViewBehavior {

    private CustomWebView webView;
    private int prevY;
    private View bottomToolbar;
    private View topAlwaysToolbar;
    private View findView;

    public WebViewBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        bottomToolbar = parent.findViewById(R.id.bottomToolbarLayout);
        topAlwaysToolbar = parent.findViewById(R.id.topAlwaysToolbarLayout);
        findView = parent.findViewById(R.id.find);
        return super.layoutDependsOn(parent, child, dependency);
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        int bottom = dependency.getBottom();

        if (webView != null && !webView.isTouching()) {
            webView.scrollBy(0, bottom - prevY);
        }

        prevY = bottom;

        if (dependency.getHeight() != 0 && dependency.getHeight() == dependency.getBottom()) {
            child.setPadding(0, 0, 0, getPadding(dependency));
        } else {
            child.setPadding(0, 0, 0, 0);
        }

        return super.onDependentViewChanged(parent, child, dependency);
    }

    public void setWebView(CustomWebView webView) {
        this.webView = webView;
    }

    private int getPadding(View dependency) {
        return dependency.getHeight() + bottomToolbar.getHeight()
                - topAlwaysToolbar.getHeight() - findView.getHeight();
    }
}

@@ -1057,16 +1057,45 @@
  prevY;%0A
+    private View topToolBar;%0A
     priv
@@ -1110,32 +1110,32 @@
  bottomToolbar;%0A
-
     private View
@@ -1383,32 +1383,97 @@
 w dependency) %7B%0A
+        topToolBar = parent.findViewById(R.id.topToolbarLayout);%0A
         bottomTo
@@ -2039,26 +2039,26 @@
     if (
-dependency
+topToolBar
 .getHeig
@@ -2463,32 +2463,32 @@
 w dependency) %7B%0A
+
         return d
@@ -2512,52 +2512,8 @@
 ht()
- + bottomToolbar.getHeight()%0A               
  - t

change behavior for toolbar