package org.thoughtcrime.securesms.conversationlist

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.ViewCompat
import com.google.android.material.appbar.AppBarLayout

class ConversationFilterBehavior(context: Context, attributeSet: AttributeSet) : AppBarLayout.Behavior(context, attributeSet) {

  var callback: Callback? = null

  override fun onStartNestedScroll(parent: CoordinatorLayout, child: AppBarLayout, directTargetChild: View, target: View, nestedScrollAxes: Int, type: Int): Boolean {
    if (type == ViewCompat.TYPE_NON_TOUCH || callback?.canStartNestedScroll() == false) {
      return false
    } else {
      return super.onStartNestedScroll(parent, child, directTargetChild, target, nestedScrollAxes, type)
    }
  }

  override fun onStopNestedScroll(coordinatorLayout: CoordinatorLayout, child: AppBarLayout, target: View, type: Int) {
    super.onStopNestedScroll(coordinatorLayout, child, target, type)
    callback?.onStopNestedScroll()
    child.setExpanded(false, true)
  }

  override fun onTouchEvent(parent: CoordinatorLayout, child: AppBarLayout, ev: MotionEvent): Boolean {
    if (ev.action == MotionEvent.ACTION_UP) {
      callback?.onStopNestedScroll()
      child.setExpanded(false, true)
    }
    return super.onTouchEvent(parent, child, ev)
  }

  interface Callback {
    fun onStopNestedScroll()
    fun canStartNestedScroll(): Boolean
  }
}
