package com.yidianling.home.ui.view

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Paint
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import com.yidianling.home.R
import com.yidianling.home.event.IHomeBaseEvent
import com.yidianling.home.model.bean.HomeConfideBean
import com.ydl.ydl_image.config.SimpleImageOpConfiger
import com.ydl.ydl_image.manager.YDLImageCacheManager
import com.yidianling.common.tools.RxImageTool
import kotlinx.android.synthetic.ydl.home_confide_expert_info_view.view.*

/**
 * @author jiucheng
 * @描述:首页倾诉模块--专家个人信息view
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2019/2/16
 */
class HomeConfideExpertInfoView(private var mContext: Context, private var homeEvent: IHomeBaseEvent?) : FrameLayout(mContext) {
    private var dp70 = 0

    init {
        initView()

        dp70 = RxImageTool.dip2px(70f)
    }

    private fun initView() {
        var params = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        layoutParams = params
        View.inflate(context, R.layout.home_confide_expert_info_view, this)
    }

    fun setData(bean: HomeConfideBean.BodyBean?, position: Int, isLast: Boolean) {
        if (bean == null) {
            visibility = View.GONE
            return
        }
        //设置头像
        setHead(bean)
        //设置名称
        tv_name.text = bean.confidedName
        //设置倾诉标签View的最大宽度
        tag_view.setConfideWidth()
        //咨询师标签
        tag_view.initData(bean.confidedTag as MutableList<String>)
        //设置向TA倾诉人数
        setConfideNum(bean)
        //设置接通率文案
        setConnection(bean)
        //设置向TA倾诉按钮状态
        setConfideButton(bean)
        //设置红包、原价
        setCoupon(bean)
        //最后一项显示分割线
        if (isLast) {
            view_line.visibility = View.GONE
        } else {
            view_line.visibility = View.VISIBLE
        }
        this.setOnClickListener {
            homeEvent!!.confideClick(bean.linkUrl,bean.doctorId)
        }
    }

    /**
     * 设置头像
     */
    private fun setHead(bean: HomeConfideBean.BodyBean) {
        val op = SimpleImageOpConfiger()
        op.errorPic = R.drawable.platform_head_place_hold_pic
        op.loadingPic = R.drawable.platform_head_place_hold_pic
        op.transform = 0
        YDLImageCacheManager.showImage(mContext, bean.confidedIcon, img_head, op)
    }

    /**
     * 设置向TA倾诉人数
     */
    private fun setConfideNum(bodyBean: HomeConfideBean.BodyBean) {
        if (TextUtils.isEmpty(bodyBean.confideNum)) {
            tv_confideNum.visibility = View.GONE
            tv_tv_confideNumContent.visibility = View.GONE
        } else {
            tv_confideNum.visibility = View.VISIBLE
            tv_tv_confideNumContent.visibility = View.VISIBLE
            tv_confideNum.text = bodyBean.confideNum
        }
    }

    /**
     * 设置接通率文案
     */
    private fun setConnection(bodyBean: HomeConfideBean.BodyBean) {
        if (TextUtils.isEmpty(bodyBean.confideConnection)) {
            tv_Connection.visibility = View.INVISIBLE
            tv_ConnectionContent.visibility = View.INVISIBLE
        } else {
            tv_Connection.visibility = View.VISIBLE
            tv_ConnectionContent.visibility = View.VISIBLE
            tv_Connection.text = bodyBean.confideConnection
        }
    }

    /**
     * 设置向TA倾诉按钮状态
     */
    private fun setConfideButton(bodyBean: HomeConfideBean.BodyBean) {
        when (bodyBean.confideLine) {//1在线 2离线 3通话中  4继续倾诉
            1 -> {
                tv_confide.setBackgroundResource(R.drawable.home_confide_line_1)
            }
            2 -> {
                tv_confide.setBackgroundResource(R.drawable.home_confide_line_2)
            }
            3 -> {
                tv_confide.setBackgroundResource(R.drawable.home_confide_line_3)
            }
            4 -> {
                tv_confide.setBackgroundResource(R.drawable.home_confide_line_4)
            }
        }
        tv_confide.setOnClickListener {
            if (bodyBean.confideLine == 2) {//喊他上线  私聊
                homeEvent!!.toChatForMsg(bodyBean.uid.toString())
            } else {
                homeEvent!!.confideClick(bodyBean.linkUrl, bodyBean.doctorId)
            }
        }
    }

    /**
     * 设置红包、原价
     */
    @SuppressLint("SetTextI18n")
    private fun setCoupon(bodyBean: HomeConfideBean.BodyBean) {

        if(!TextUtils.isEmpty(bodyBean.couponText)){
            tvCoupon.text = bodyBean.couponText
            tvCoupon.visibility = View.VISIBLE
        }else{
            tvCoupon.visibility = View.GONE
        }
        if (TextUtils.isEmpty(bodyBean.confideFee) || TextUtils.isEmpty(bodyBean.couponMoney) || bodyBean.couponMoney!!.toFloat() <= 0){
            tvOriginalPrice.visibility = View.GONE
        }else{
            tvOriginalPrice.visibility = View.VISIBLE
            val originalBuffer = StringBuffer()
            originalBuffer.append(bodyBean.confideFee).append("元/次")
            tvOriginalPrice.text = originalBuffer.toString()
            //添加删除线
            tvOriginalPrice.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
        }
        try {
            val couponMoney = bodyBean.couponMoney!!.toFloat().toInt()
            val confideFee = bodyBean.confideFee!!.toFloat().toInt()

            if (couponMoney >= confideFee){
                tv_price.text = "¥0"
            }else{
                val price = confideFee - couponMoney
                tv_price.text = "¥"+price.toString()
            }
        }catch (e:Exception){
            //防止数据类型变化引起奔溃
        }

    }
}
