/*
 * 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();
    }
}

/*
 * 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 topToolBar;
    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) {
        topToolBar = parent.findViewById(R.id.topToolbarLayout);
        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 (topToolBar.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() - topAlwaysToolbar.getHeight() - findView.getHeight();
    }
}

change behavior for toolbar