package org.ccci.gto.android.common.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ScrollView;

import org.ccci.gto.android.common.R;

import androidx.annotation.IdRes;
import androidx.annotation.Nullable;

public class AffixingScrollView extends ScrollView {
    private static final int INVALID_RESOURCE = -1;

    @IdRes
    private int mAffixingId = INVALID_RESOURCE;
    @Nullable
    private View mAffixingView;
    @IdRes
    private int mAnchorId = INVALID_RESOURCE;
    @Nullable
    private View mAnchorView;

    public AffixingScrollView(final Context context) {
        super(context);
    }

    public AffixingScrollView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        this.init(attrs);
    }

    public AffixingScrollView(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
        this.init(attrs);
    }

    private void init(final AttributeSet attrs) {
        final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.AffixingScrollView);
        mAffixingId = a.getResourceId(R.styleable.AffixingScrollView_affixingView, INVALID_RESOURCE);
        mAnchorId = a.getResourceId(R.styleable.AffixingScrollView_anchorView, INVALID_RESOURCE);
        a.recycle();
    }

    /**
     * Attach the appropriate child view to managed during scrolling as the affixing view. This view MUST be an
     * existing child.
     *
     * @param v View to manage as the floating view
     */
    public void setAffixingView(final View v) {
        mAffixingView = v;
        updateAffixingView();
    }

    /**
     * Attach the appropriate child view to monitor during scrolling as the anchoring space for the floating view.
     * This view MUST be an existing child.
     *
     * @param v View to manage as the anchoring space
     */
    public void setAnchorView(final View v) {
        mAnchorView = v;
        updateAffixingView();
    }

    /* BEGIN lifecycle */

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if (mAnchorId > 0) {
            mAnchorView = this.findViewById(mAnchorId);
            mAnchorId = INVALID_RESOURCE;
        }
        if (mAffixingId > 0) {
            mAffixingView = this.findViewById(mAffixingId);
            mAffixingId = INVALID_RESOURCE;
        }
        updateAffixingView();
    }

    @Override
    protected void onLayout(final boolean changed, final int l, final int t, final int r, final int b) {
        super.onLayout(changed, l, t, r, b);
        updateAffixingView();
    }

    @Override
    protected void onScrollChanged(final int l, final int t, final int oldl, final int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        updateAffixingView();
    }

    /* END lifecycle */

    private void updateAffixingView() {
        if (mAnchorView == null || mAffixingView == null) {
            return;
        }

        // is the anchor view off the screen?
        if (mAnchorView.getTop() - getScrollY() < 0) {
            // Yes, move the affixing view to the top of the visible view
            mAffixingView.offsetTopAndBottom(getScrollY() - mAffixingView.getTop());
        } else {
            // No, move the affixing view to the anchor view
            mAffixingView.offsetTopAndBottom(mAnchorView.getTop() - mAffixingView.getTop());
        }
    }
}

@@ -315,16 +315,90 @@
 lable;%0A%0A
+import static org.ccci.gto.android.common.base.Constants.INVALID_ID_RES;%0A%0A
 public c
@@ -446,61 +446,8 @@
 w %7B%0A
-    private static final int INVALID_RESOURCE = -1;%0A%0A
     
@@ -487,32 +487,30 @@
 d = INVALID_
+ID_
 RES
-OURCE
 ;%0A    @Nulla
@@ -588,32 +588,30 @@
 d = INVALID_
+ID_
 RES
-OURCE
 ;%0A    @Nulla
@@ -1293,32 +1293,30 @@
 ew, INVALID_
+ID_
 RES
-OURCE
 );%0A        m
@@ -1393,24 +1393,22 @@
 INVALID_
+ID_
 RES
-OURCE
 );%0A     
@@ -2105,25 +2105,26 @@
    /
-* BEGIN l
+/ region L
 ifecycle
  */%0A
@@ -2115,27 +2115,24 @@
 on Lifecycle
- */
 %0A%0A    @Overr
@@ -2229,19 +2229,33 @@
 nchorId 
-%3E 0
+!= INVALID_ID_RES
 ) %7B%0A    
@@ -2334,32 +2334,30 @@
 d = INVALID_
+ID_
 RES
-OURCE
 ;%0A        %7D%0A
@@ -2384,11 +2384,25 @@
 gId 
-%3E 0
+!= INVALID_ID_RES
 ) %7B%0A
@@ -2499,16 +2499,14 @@
 LID_
+ID_
 RES
-OURCE
 ;%0A  
@@ -2955,23 +2955,29 @@
    /
-* END l
+/ endregion L
 ifecycle
  */%0A
@@ -2972,19 +2972,16 @@
 ifecycle
- */
 %0A%0A    pr

update AffixingScrollView to utilize an actual invalid id res