/*
 * @Copyright 2018-2024 HardBackNutter
 * @License GNU General Public License
 *
 * This file is part of NeverTooManyBooks.
 *
 * NeverTooManyBooks is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * NeverTooManyBooks is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with NeverTooManyBooks. If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * Copyright 2019 Google LLC
 *
 * 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
 *
 *     https://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 com.hardbacknutter.fastscroller;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.graphics.Insets;
import androidx.core.view.OnApplyWindowInsetsListener;
import androidx.core.view.WindowInsetsCompat;

/**
 * Original code from <a href="https://github.com/zhanghai/AndroidFastScroll">
 * https://github.com/zhanghai/AndroidFastScroll</a>.
 */
class ScrollingViewOnApplyWindowInsetsListener
        implements OnApplyWindowInsetsListener {

    private static final int INSETS_TYPE_MASK = WindowInsetsCompat.Type.systemBars()
                                                | WindowInsetsCompat.Type.displayCutout();
    @NonNull
    private final Insets padding;
    @Nullable
    private final OverlayProvider overlayProvider;

    /**
     * Constructor.
     *
     * @param view            the scrolling view
     * @param overlayProvider (optional) overlay for the view
     */
    ScrollingViewOnApplyWindowInsetsListener(@NonNull final View view,
                                             @Nullable final OverlayProvider overlayProvider) {

        padding = Insets.of(view.getPaddingLeft(), view.getPaddingTop(),
                            view.getPaddingRight(), view.getPaddingBottom());
        this.overlayProvider = overlayProvider;
    }

    @Override
    @NonNull
    public WindowInsetsCompat onApplyWindowInsets(@NonNull final View v,
                                                  @NonNull final WindowInsetsCompat wic) {
        final Insets insets = wic.getInsets(INSETS_TYPE_MASK);

        v.setPadding(padding.left + insets.left,
                     padding.top,
                     padding.right + insets.right,
                     padding.bottom + insets.bottom);

        if (overlayProvider != null) {
            overlayProvider.setPadding(insets.left, 0, insets.right, insets.bottom);
        }
        return wic;
    }
}
