package com.duben.microtribe.ui.adapter

import android.content.Context
import android.graphics.Paint
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.duben.microtribe.R
import com.duben.microtribe.mvp.model.VipBean
import com.duben.microtribe.utils.BubbleUtils
import com.duben.microtribe.utils.SpanUtils
import com.duben.microtribe.utils.UIUtils
import kotlinx.android.synthetic.main.fragment_main_my.*

class VipAdapter(val vipList: MutableList<VipBean.ListBean>) :
    RecyclerView.Adapter<VipAdapter.ViewHolder>() {

    var myPos: Int = 0
    lateinit var context: Context

    lateinit var mOnItemClickListener: OnItemClickListener

    init {
        if (vipList != null && vipList.size > 0) {
            for (i in 0..vipList.size - 1) {
                if (vipList[i].activityType == 1) {
                    myPos = i
                    break
                }
            }
        }
    }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val rlVipItemRoot: FrameLayout = view.findViewById(R.id.rlVipItemRoot)
        val tvVipItemCurMoney: TextView = view.findViewById(R.id.tvVipItemCurMoney)
        val tvVipItemMoney: TextView = view.findViewById(R.id.tvVipItemMoney)
        val tvVipItemTitle: TextView = view.findViewById(R.id.tvVipItemTitle)
        val tvVipItemLable: TextView = view.findViewById(R.id.tvVipItemLable)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        context = parent.context
        val view = LayoutInflater.from(context).inflate(R.layout.item_rv_vip, parent, false)
        val viewHolder = ViewHolder(view)
        return viewHolder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val vipBean = vipList[position]

        if (vipBean.trial != 0) {
            holder.tvVipItemLable.setPadding(0, 0, 0, 0)
            holder.tvVipItemLable.text = "限时特惠"
            holder.tvVipItemLable.gravity = Gravity.CENTER
            holder.tvVipItemLable.setBackgroundResource(R.mipmap.ic_vip_lable)
        } else {
            holder.tvVipItemLable.setPadding(0, 0, 0, 0)
            holder.tvVipItemLable.text = "热门"
            holder.tvVipItemLable.gravity = Gravity.CENTER
            holder.tvVipItemLable.setBackgroundResource(R.mipmap.ic_vip_lable)
        }


//        try {
//            if(!TextUtils.isEmpty(vipBean.topTitle)){
//
//               val topArray= vipBean.topTitle.split(",")
//                holder.tvVipItemCurMoney.text = SpanUtils()
//                        .append(topArray[0])
//                        .append(topArray[1])
//                        .setFontSize(BubbleUtils.sp2px(28))
//                        .append(topArray[2])
//                        .create()
//                holder.tvVipItemMoney.text = topArray[3]
//                holder.tvVipItemTitle.text = topArray[4]
//            }else{
//                holder.tvVipItemTitle.text = vipBean.title
//                if (vipBean.price < 1) {
//                    holder.tvVipItemCurMoney.text = "￥${vipBean.price}"
//                } else {
//                    holder.tvVipItemCurMoney.text = "￥${vipBean.price}"
////            holder.tvVipItemCurMoney.text = "￥${String.format("%.0f", vipBean.price)}"
//                }
//                if (vipBean.oldPrice > 0) {
//                    holder.tvVipItemMoney.visibility = View.VISIBLE
//                    holder.tvVipItemMoney.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG) //中间横线（删除线）
//                    if (vipBean.oldPrice < 1) {
//                        holder.tvVipItemMoney.text = "原价：${vipBean.oldPrice}"
//                    } else {
//                        holder.tvVipItemMoney.text = "原价：${String.format("%.0f", vipBean.oldPrice)}"
//                    }
//                } else {
//                    holder.tvVipItemMoney.visibility = View.INVISIBLE
//                }
//
//            }
//        }catch (e:Exception){
//            e.printStackTrace()
            holder.tvVipItemTitle.text = vipBean.title
//            if (vipBean.price < 1) {
//                holder.tvVipItemCurMoney.text = "￥${vipBean.price}"
//            } else {
//                holder.tvVipItemCurMoney.text = "￥${vipBean.price}"
////            holder.tvVipItemCurMoney.text = "￥${String.format("%.0f", vipBean.price)}"
//            }
                        holder.tvVipItemCurMoney.text = SpanUtils()
                        .append(vipBean.price.toString())
                        .setFontSize(BubbleUtils.sp2px(34))
                        .append("元")
                        .create()
//            if (vipBean.oldPrice > 0) {
//                holder.tvVipItemMoney.visibility = View.VISIBLE
//                holder.tvVipItemMoney.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG) //中间横线（删除线）
//                if (vipBean.oldPrice < 1) {
//                    holder.tvVipItemMoney.text = "原价：${vipBean.oldPrice}"
//                } else {
//                    holder.tvVipItemMoney.text = "原价：${String.format("%.0f", vipBean.oldPrice)}"
//                }
//            } else {
//                holder.tvVipItemMoney.visibility = View.INVISIBLE
//            }
//        }
        holder.tvVipItemMoney.text = vipBean.topTitle


        holder.rlVipItemRoot.setOnClickListener {
            mOnItemClickListener.onItemClick(position)
            notifyDataSetChanged()
        }

        if (getPosition() == position) {
            //当前选中
            holder.rlVipItemRoot.setBackgroundResource(R.drawable.shape_vip_adapter)
//            holder.tvVipItemCurMoney.setTextColor(
//                ContextCompat.getColor(
//                    context,
//                    R.color.color_FD7E0E
//                )
//            )
        } else {
            holder.rlVipItemRoot.setBackgroundResource(R.drawable.shape_vip_adapter_none)
//            holder.tvVipItemCurMoney.setTextColor(
//                ContextCompat.getColor(
//                    context,
//                    R.color.white
//                )
//            )
        }

        holder.itemView.setTag(position)
    }

    override fun getItemCount() = vipList?.size

    interface OnItemClickListener {
        fun onItemClick(position: Int)
    }

    fun setOnItemClickListener(listener: OnItemClickListener) {
        mOnItemClickListener = listener
    }

    fun getPosition() = myPos

    fun setPosition(myPos: Int) {
        this.myPos = myPos
    }
}