package com.develop.food.base.widgets

import android.content.Context
import android.graphics.Rect
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import android.widget.RelativeLayout
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.develop.base.R
import com.develop.base.databinding.FoodContentViewBinding
import com.develop.base.databinding.NewsViewBinding
import com.develop.base.databinding.WifiContentViewBinding
import com.develop.food.base.adapter.NewsAdapter
import com.develop.food.base.data.model.FoodContentModel
import com.develop.food.base.data.model.NewsModel
import com.develop.food.base.utils.ResDimension

class NewsLayout : LinearLayout {
    private var binding: NewsViewBinding

    private var newsAdapter = NewsAdapter()
    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 {
            adapter = newsAdapter
            addItemDecoration(object : RecyclerView.ItemDecoration() {
                override fun getItemOffsets(
                    outRect: Rect,
                    view: View,
                    parent: RecyclerView,
                    state: RecyclerView.State
                ) {
                    super.getItemOffsets(outRect, view, parent, state)
                    outRect.left = ResDimension(R.dimen.convert_44px).toInt()
                    outRect.right = ResDimension(R.dimen.convert_44px).toInt()
                    if (parent.getChildAdapterPosition(view) == 0) {
                        outRect.top = ResDimension(R.dimen.convert_57px).toInt()
                    }
                    outRect.bottom = ResDimension(R.dimen.convert_60px).toInt()
                }
            })
        }
        binding.topBar.setTopBarTitle(context.getString(R.string.notice))
        binding.topBar.listenBackClick {
            onNewsBackClickListener?.onNewsBackClick()
        }
    }


    fun updateNewsData(data: MutableList<NewsModel>) {
        dataList.clear()
        dataList.addAll(data)
        newsAdapter.setList(dataList)
    }

    interface OnNewsNewsBackClickListener {
        fun onNewsBackClick()
    }

}