package com.develop.main.init.ui.adapter

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
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.food.base.common.Downloaded
import com.develop.food.base.common.FOOD_LIST_TYPE
import com.develop.food.base.common.FOOL_LIST_TITLE
import com.develop.food.base.data.model.HomeBannerModel
import com.develop.food.base.utils.ResDimension
import com.develop.main.R
import com.develop.main.init.ui.HomeActivity
import com.develop.food.base.router.Screens
import com.develop.food.base.router.navigateTo
import com.youth.banner.adapter.BannerAdapter

class HomeBannerAdapter(var bannerDataList: MutableList<HomeBannerModel>) :
    BannerAdapter<HomeBannerModel, HomeBannerAdapter.ViewHolder>(bannerDataList) {


    private lateinit var context: Context
    private val roundedCorners = RoundedCorners(ResDimension(R.dimen.convert_30px).toInt())
    private val options = RequestOptions.bitmapTransform(roundedCorners)

    var onBannerItemClickListener: OnBannerItemClickListener? = null

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var ivBanner: ImageView = itemView.findViewById(R.id.iv_banner)
        val netRecipesLayout: RelativeLayout = itemView.findViewById(R.id.net_recipes_layout)
        var downloadLayout: RelativeLayout = itemView.findViewById(R.id.download_layout)

    }

    override fun onCreateHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        context = parent.context
        return ViewHolder(
            LayoutInflater.from(context).inflate(R.layout.item_home_banner, parent, false)
        )
    }

    override fun onBindView(holder: ViewHolder, data: HomeBannerModel, position: Int, size: Int) {
        Glide.with(context).load(data.cover).apply(options).into(holder.ivBanner)
        holder.downloadLayout.setOnClickListener {
            (context as HomeActivity).navigateTo(Screens.Main.FOOD_LIST) {
                val bundle = Bundle()
                bundle.putString(FOOL_LIST_TITLE, context.getString(R.string.download))
                bundle.putInt(FOOD_LIST_TYPE, Downloaded)
                with(bundle)
            }
        }
        holder.netRecipesLayout.setOnClickListener {
            onBannerItemClickListener?.onNetRecipesClick(data)
        }
    }

    interface OnBannerItemClickListener {
        fun onNetRecipesClick(bannerModel: HomeBannerModel)
    }
}