package com.zswc.creativity.adapter

import android.graphics.Color
import coil.load
import coil.transform.RoundedCornersTransformation
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.ysnows.base.base.BAdapter
import com.zswc.creativity.R
import com.zswc.creativity.databinding.ItemRecommedBinding
import com.zswc.creativity.model.CollectionList


class RecommedAdapter() : BAdapter<CollectionList,
        BaseDataBindingHolder<ItemRecommedBinding>>(R.layout.item_recommed) {


    //state 1:进行中2：即将开售3：已售罄）
    override fun convert(holder: BaseDataBindingHolder<ItemRecommedBinding>, item: CollectionList) {
        holder.dataBinding?.item = item


        holder.dataBinding?.imgBg?.load(item.cover){
           // 圆角
            transformations(RoundedCornersTransformation(20f))
            placeholder(R.drawable.bg_need_error)
            error(R.drawable.bg_need_error)
        }

//        Thread {
//            val url = URL(item.cover)
//            val conn: URLConnection = url.openConnection()
//            conn.connect()
//            val input: InputStream = conn.getInputStream()
//            val options = BitmapFactory.Options()
//            options.inSampleSize = 10
//            var originalImage = BitmapFactory.decodeStream(input, null, options)
//            holder.dataBinding?.imgBg?.setImageBitmap(originalImage)
//        }.start()



        //设置图片
//        holder.dataBinding?.imgBg?.load(item.cover) {
//            size(100,100)
//            error(R.drawable.bg_need_error) //加载错误占位图
//        }

        when (item.state) {
            "1" -> {
                holder.dataBinding?.tvState?.text = "热卖中 "
                holder.dataBinding?.tvState?.setTextColor(Color.parseColor("#FFF488"))
                holder.dataBinding?.imgState?.setImageResource(R.drawable.ct_time_ing)
            }
            "2" -> {
                holder.dataBinding?.tvState?.text = "即将开售 " + item.sell_time
                holder.dataBinding?.tvState?.setTextColor(Color.parseColor("#62FFF0"))
                holder.dataBinding?.imgState?.setImageResource(R.drawable.ct_time)
            }
            "3" -> {

                holder.dataBinding?.tvState?.text = "已售罄 "
                holder.dataBinding?.tvState?.setTextColor(Color.parseColor("#999999"))
                holder.dataBinding?.imgState?.setImageResource(R.drawable.ct_time_stop)
            }
            else -> {
                holder.dataBinding?.tvState?.text = "已售罄 "
                holder.dataBinding?.tvState?.setTextColor(Color.parseColor("#999999"))
                holder.dataBinding?.imgState?.setImageResource(R.drawable.ct_time_stop)

            }

        }

    }


}
