package com.yidianling.uikit.custom.widget.expertConsultService.view

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import com.ydl.webview.H5Params
import com.ydl.webview.NewH5Activity
import com.ydl.ydlcommon.base.config.HttpConfig
import com.yidianling.im.R
import com.yidianling.uikit.custom.http.response.ServiceItemBean
import com.yidianling.uikit.custom.widget.expertConsultService.callback.ConsultServiceViewCallback
import kotlinx.android.synthetic.main.im_expert_consult_service_item_view.view.*

class ExpertConsultServiceItemView : LinearLayout {


    constructor(context: Context, listener: ConsultServiceViewCallback?) : super(context) {
        mListener = listener
        mContext = context

        initView()
    }

    private var mContext: Context? = null
    private var mListener: ConsultServiceViewCallback? = null


    private fun initView() {

        val mWidth: Int = LayoutParams.MATCH_PARENT
        val mHeight: Int = LayoutParams.WRAP_CONTENT

        val params = LayoutParams(
            mWidth,
            mHeight
        )
        params.height = mHeight
        layoutParams = params

        orientation = HORIZONTAL
        View.inflate(mContext, R.layout.im_expert_consult_service_item_view, this)
    }

    /**
     * 设置数据
     */
    @SuppressLint("SetTextI18n")
    fun setData(bean: ServiceItemBean.ProductsBean) {
        //是否是套餐
        if (bean.productDto.isPackage == 2) {
            //展示左上角套餐布局
            service_item_package.visibility = View.VISIBLE
            //服务名
            service_item_title.text = "       ${bean.productDto.name}"
            //起售次数展示
            service_item_low_buy_time.visibility = View.VISIBLE
            service_item_low_buy_time.text = "(${bean.productDto.packageNum}次起售)"
        } else {
            //隐藏套餐布局
            service_item_package.visibility = View.GONE
            //服务名
            service_item_title.text = bean.productDto.name ?: ""
            //隐藏起售次数限制
            service_item_low_buy_time.visibility = View.GONE
        }
        val mCompare: Comparator<ServiceItemBean.ProductsBean.ProductSpecDtosBean> = Comparator { o1, o2 ->
            val res = o1.price.compareTo(o2.price)
            when {
                res == 0 -> 0
                res > 0 -> -1
                else -> 1
            }
        }
        if (bean.productSpecDtos != null && bean.productSpecDtos.size > 0) {
            val mBean: ServiceItemBean.ProductsBean.ProductSpecDtosBean = bean.productSpecDtos.sortedWith(mCompare).last()
            //价格，取productSpecDtos数组最小价格，不保留小数
            service_item_price.text = String.format(
                "%.0f",
                mBean.price
            )

            //时间，取productSpecDtos数组最后一个的时间
            service_item_time.text = "/${mBean.spec2}分钟"
        }

        //好评率，*20后 保留1位小数
        service_item_feddbackrate.text = String.format(
            "%.1f",
            (bean.productDto.feedbackRate) * 20f
        ) + "%"

        //销量
        service_item_saleout_num.text = "销量${bean.productDto.saleoutAmount}"

        setOnClickListener {
            mListener?.onItemClick(bean)
        }

        im_expert_service_list_btn.setOnClickListener {
            NewH5Activity.start(mContext, H5Params(HttpConfig.MH5_URL + "consult/#/pages/DownOrder/DownOrder?product_id=" + bean.productDto.id, null))
        }

    }
}