package com.develop.common.widget

import android.content.Context
import android.graphics.Rect
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import androidx.appcompat.widget.AppCompatTextView
import androidx.recyclerview.widget.RecyclerView
import com.develop.base.ext.load
import com.develop.base.ext.resId2Dimension
import com.develop.base.ext.updateText
import com.develop.common.R
import com.develop.common.data_repo.db.NewsModel
import com.develop.common.databinding.NewsViewBinding
import com.drake.brv.utils.linear
import com.drake.brv.utils.models
import com.drake.brv.utils.setup

class NewsLayout : RelativeLayout {
    private var binding: NewsViewBinding
    private var dataList = mutableListOf<NewsModel>()

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context, attrs, defStyleAttr
    ) {
    }

    var onNewsBackClickListener: OnNewsNewsBackClickListener? = null

    init {
        val root = View.inflate(context, R.layout.news_view, this)
        binding = NewsViewBinding.bind(root)
        binding.newRecycler.apply {
            linear(RecyclerView.VERTICAL)
            setup {
                addType<NewsModel>(R.layout.item_news)
                onBind {
                    val model = getModel<NewsModel>()
                    val ivCircle = findView<ImageView>(R.id.iv_circle)
                    val tvNewsTitle = findView<AppCompatTextView>(R.id.tv_news_title)
                    if (model.hasRead) {
                        ivCircle.load(R.drawable.a3a3a2_circle)
                        tvNewsTitle.setTextColor(resources.getColor(R.color.color_A3A3A2))
                    } else {
                        ivCircle.load(R.drawable.ee8f08_circle)
                        tvNewsTitle.setTextColor(resources.getColor(R.color.color_EE8F08))
                    }
                    tvNewsTitle.updateText(model.des)
                    findView<AppCompatTextView>(R.id.tv_news_content).updateText(model.des)
                }
            }
            addItemDecoration(object : RecyclerView.ItemDecoration() {
                override fun getItemOffsets(
                    outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State
                ) {
                    super.getItemOffsets(outRect, view, parent, state)
                    outRect.left = dp39
                    outRect.right = dp39
                    if (parent.getChildAdapterPosition(view) == 0) {
                        outRect.top = dp5
                    }
                    outRect.bottom = dp24
                }
            })

        }.models = dataList
        binding.tvNotice.updateText("${context.getString(R.string.notice)}(0)")
    }


    fun updateNewsData(data: MutableList<NewsModel>) {
        dataList.clear()
        dataList.addAll(data)
        if (dataList.isEmpty()) {
            binding.tvEmpty.visibility = View.VISIBLE
            binding.newRecycler.visibility = View.GONE
        } else {
            binding.tvEmpty.visibility = View.GONE
            binding.newRecycler.visibility = View.VISIBLE
            binding.newRecycler.adapter?.notifyDataSetChanged()
        }
    }

    interface OnNewsNewsBackClickListener {
        fun onNewsBackClick()
    }

    companion object {
        var dp39 = (com.develop.base.R.dimen.convert_39px).resId2Dimension().toInt()
        var dp5 = (com.develop.base.R.dimen.convert_5px).resId2Dimension().toInt()
        var dp24 = (com.develop.base.R.dimen.convert_24px).resId2Dimension().toInt()
    }

}