package com.xuqm.sdhbwfu.core.extensions

import android.content.Context
import android.graphics.Rect
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import android.widget.EditText
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.textfield.TextInputLayout


fun View.gone() {
    if (visibility != View.GONE) {
        visibility = View.GONE
    }
}

fun View.invisible() {
    if (visibility != View.INVISIBLE) {
        visibility = View.INVISIBLE
    }
}

fun View.visible() {
    if (visibility != View.VISIBLE) {
        visibility = View.VISIBLE
    }
}

fun View.visibility(visible: Boolean) {
    if (visible) {
        visible()
    } else {
        gone()
    }
}

fun View.snack(msg: String) {
    if (!TextUtils.isEmpty(msg)) {
        Snackbar.make(this, msg, Snackbar.LENGTH_SHORT).show()
    }
}

/*
输入的时候不提示错误
 */
fun EditText.addTextChangedListener(textInputLayout: TextInputLayout) {

    addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {
            if (!TextUtils.isEmpty(textInputLayout.error)) {
                textInputLayout.isErrorEnabled = true
                textInputLayout.error = ""
                textInputLayout.isErrorEnabled = false
            }
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        }

        override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {

        }
    })
}

fun TextInputLayout.showError(msg: String) {
    isErrorEnabled = true
    error = msg
}

fun RecyclerView.addItemDecorationForVertical(stroke: Int) {
    addItemDecoration(object : RecyclerView.ItemDecoration() {
        override fun getItemOffsets(
            outRect: Rect,
            view: View,
            parent: RecyclerView,
            state: RecyclerView.State
        ) {
            outRect.bottom = stroke
            if (parent.getChildAdapterPosition(view) == 0)
                outRect.top = stroke
        }
    })
}
fun RecyclerView.linearLayoutManager(mContext: Context, stroke: Int) {

    layoutManager = LinearLayoutManager(mContext)

    addItemDecoration(object : RecyclerView.ItemDecoration() {
        override fun getItemOffsets(
            outRect: Rect,
            view: View,
            parent: RecyclerView,
            state: RecyclerView.State
        ) {
            outRect.bottom = stroke
            if (parent.getChildAdapterPosition(view) == 0)
                outRect.top = stroke
        }
    })

}

fun RecyclerView.gridLayoutManager(mContext: Context, stroke: Int, spanCount: Int) {
    layoutManager = GridLayoutManager(mContext, spanCount)
    addItemDecoration(object : RecyclerView.ItemDecoration() {
        override fun getItemOffsets(
            outRect: Rect,
            view: View,
            parent: RecyclerView,
            state: RecyclerView.State
        ) {
            outRect.bottom = stroke
            if (parent.getChildAdapterPosition(view) < spanCount)
                outRect.top = stroke
            if (parent.getChildAdapterPosition(view) % spanCount == 0)
                outRect.left = stroke
            else outRect.left = stroke / 2
            if (parent.getChildAdapterPosition(view) % spanCount == (spanCount - 1))
                outRect.right = stroke
            else outRect.right = stroke / 2
        }
    })
}

