package com.yidian.bcommon.mvvm.binding

import android.view.View
import android.widget.CheckBox
import android.widget.EditText
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.google.android.material.tabs.TabLayout
import com.google.android.material.textfield.TextInputLayout
import com.yidian.bcommon.R
import com.yidian.bcommon.extensions.toggleVisibility


/**
 * 是否显示该 View
 */
@BindingAdapter("isShow")
fun bindIsShow(view: View, isShow: Boolean) {
  view.toggleVisibility(isShow)
}

/**
 * 是否显示该 View
 */
@BindingAdapter("isVisible")
fun bindIsVisible(view: View, isVisible: Boolean) {
  if (isVisible) {
    view.visibility = View.VISIBLE
  } else {
    view.visibility = View.INVISIBLE
  }
}

/**
 * 是否显示该 View
 */
@BindingAdapter("isGone")
fun bindIsGone(view: View, isGone: Boolean) {
  if (isGone) {
    view.visibility = View.GONE
  } else {
    view.visibility = View.VISIBLE
  }
}

/**
 * 是否显示Error
 */
@BindingAdapter("textInputError")
fun bindIsShowError(textInputLayout: TextInputLayout, errorMessage: String?) {
  textInputLayout.error = errorMessage
}

/**
 * 借用 View 的 selected 属性，仿制按钮 enable
 */
@BindingAdapter("isSelected")
fun bindIsSelected(view: View, isSelected: Boolean) {
  view.isSelected = isSelected
}

/**
 * 为 [TabLayout] 的 Tabs 绑定 title
 */
@BindingAdapter("tabTitles")
fun bindTabLayoutTitles(tab: TabLayout, titles: List<String?>?) {
  titles?.forEachIndexed { index, s -> tab.getTabAt(index)?.text = s }
}

@BindingAdapter(value = ["setAdapter"])
fun RecyclerView.bindRecyclerViewAdapter(adapter: RecyclerView.Adapter<*>) {
  this.setHasFixedSize(true)
  this.adapter = adapter
}

@BindingAdapter("imageUrl")
fun bindImageUrl(imageView: ImageView, imageUrl: String) {
  Glide.with(imageView.context).load(imageUrl).centerCrop().placeholder(R.color.color_F6F6F6).into(imageView)
}

@BindingAdapter("imageSrc")
fun setImageSrc(imageView: ImageView, resId: Int) {
  imageView.setImageResource(resId)
}


@BindingAdapter("android:editable")
fun setEditable(editText: EditText, boolean: Boolean) {
  editText.isFocusable = boolean

  editText.isFocusableInTouchMode = boolean
}

@BindingAdapter("android:clickable")
fun clickable(view: View, boolean: Boolean) {
  view.isClickable = boolean
}

//data class IconState(
//  val state: Int = 1,
//) {
//
//  fun isSelected(): Boolean {
//    return (state and SELECTED) == 1
//  }
//
//  fun isChecked(): Boolean {
//    return (state and CHECKED) == 1
//  }
//
//  companion object {
//    private const val BASELINE: Int = 1
//
//    private const val SELECTED: Int = BASELINE shr 1
//    private const val CHECKED: Int = BASELINE shr 2
//
//    fun newState(vararg state: IconState): IconState {
//      return state.reduce { a, b ->
//        return IconState(a.state or b.state)
//      }
//    }
//  }
//
//}
//
//@BindingAdapter("iconState")
//fun View.bindIconState(state: IconState) {
//  this.isSelected = state.isSelected()
//}
//
//@BindingAdapter("iconState")
//fun CheckBox.bindIconState(state: IconState) {
//  this.isSelected = state.isSelected()
//  this.isChecked = state.isChecked()
//}
