package it.integry.integrywmsnative.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ScrollView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.RecyclerView;

public class ElevatedToolbar extends CardView {

    private final float CARD_ELEVATION = 50f;

    private RecyclerView mRecyclerView = null;
    private ScrollView mScrollView = null;
    private NestedScrollView mNestedScrollView = null;

    public ElevatedToolbar(Context context) {
        super(context);
        resetElevation();
    }

    public ElevatedToolbar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        resetElevation();
    }

    public ElevatedToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        resetElevation();
    }

    public void resetElevation() {
        setCardElevation(0f);
    }

    public void resetAll() {
        resetElevation();

        removeRecyclerViewScrollListener();

        removeScrollViewScrollListener();

        removeNestedScrollViewListener();

        mRecyclerView = null;
        mScrollView = null;
        mNestedScrollView = null;

    }


    RecyclerView.OnScrollListener recyclerViewScrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            if(!recyclerView.canScrollVertically(-1)) {
                resetElevation();
            } else {
                setCardElevation(CARD_ELEVATION);
            }
        }
    };


    View.OnScrollChangeListener recyclerViewScrollChangeListener = (view, i, i1, i2, i3) -> {

        if(!view.canScrollVertically(-1)) {
            resetElevation();
        } else {
            setCardElevation(CARD_ELEVATION);
        }
    };

    public void setRecyclerView(RecyclerView recyclerView) {
        this.mRecyclerView = recyclerView;
        resetElevation();

        if(mRecyclerView != null) {
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//                mRecyclerView.addOnScrollListener(recyclerViewScrollChangeListener);
//            } else {
                mRecyclerView.addOnScrollListener(recyclerViewScrollListener);
//            }

        } else {
            Log.e("ElevatedToolbar", "RecyclerView is NULL");
        }
    }

    public void removeRecyclerViewScrollListener(){
        if(mRecyclerView != null) {
            mRecyclerView.setOnScrollListener(null);
        }
    }




    private ViewTreeObserver.OnScrollChangedListener scrollViewScrollListener = () -> {
        if(mScrollView != null) {
            if(mScrollView.getScrollY() == 0) {
                resetElevation();
            } else {
                setCardElevation(CARD_ELEVATION);
            }
        }
    };

    public void setScrollView(ScrollView scrollView) {
        this.mScrollView = scrollView;
        resetElevation();

        if(mScrollView != null) {
            mScrollView.getViewTreeObserver().addOnScrollChangedListener(scrollViewScrollListener);
        } else {
            Log.e("ElevatedToolbar", "ScrollView is NULL");
        }
    }

    public void removeScrollViewScrollListener(){
        if(mScrollView != null) {
            mScrollView.getViewTreeObserver().removeOnScrollChangedListener(scrollViewScrollListener);
        }
    }





    private NestedScrollView.OnScrollChangeListener nestedScrollViewListener = (v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
        if(mNestedScrollView != null) {
            if(scrollY == 0) {
                resetElevation();
            } else {
                setCardElevation(CARD_ELEVATION);
            }
        }
    };

    public void setNestedScrollView(NestedScrollView nestedScrollView) {
        this.mNestedScrollView = nestedScrollView;
        resetElevation();

        if(mNestedScrollView != null) {
            mNestedScrollView.setOnScrollChangeListener(nestedScrollViewListener);
        } else {
            Log.e("ElevatedToolbar", "NestedScrollView is NULL");
        }
    }

    public void removeNestedScrollViewListener() {
        if(mNestedScrollView != null) {
            mNestedScrollView.setOnScrollChangeListener((NestedScrollView.OnScrollChangeListener) null);
        }
    }



}
