package com.yidianling.ydlcommon.custom.view

import android.content.Context
import android.text.Spannable
import android.text.SpannableString
import android.text.TextUtils
import android.text.style.AbsoluteSizeSpan
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import com.yidianling.common.tools.RxImageTool
import com.yidianling.ydlcommon.R
import com.yidianling.ydlcommon.custom.bean.CourseButtonBean
import kotlinx.android.synthetic.main.view_course_detail_bottom.view.*


/**
 * @author jiucheng
 * @描述: 课程详情页底部view
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2019/7/10
 */
class CourseDetailBottomView : LinearLayout {
    private var mContext: Context? = null
    private var listener: OnBottomClickListener? = null

    constructor(context: Context) : super(context) {
        mContext = context
        initView()
    }

    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet) {
        mContext = context
        initView()
    }

    init {
        orientation = VERTICAL
    }

    private fun initView() {
        var parmas = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, RxImageTool.dp2px(56f))
        parmas.gravity = Gravity.CENTER_VERTICAL
        layoutParams = parmas
        View.inflate(mContext, R.layout.view_course_detail_bottom, this)
    }

    fun setData(bean: CourseButtonBean?) {
        if (bean == null) {
            visibility = View.GONE
            return
        }
        visibility = View.VISIBLE
        tv_button.isEnabled = bean.type
        var text = if (!TextUtils.isEmpty(bean.signUpTime)) {
            bean.desc + "\n" + bean.signUpTime
        } else {
            bean.desc
        }
        val sp = SpannableString(text)
        sp.setSpan(AbsoluteSizeSpan(12, true), bean.desc.length, text.length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
        tv_button.text = text
        tv_share_back.visibility = if (bean.isRebate == "0") View.GONE else View.VISIBLE

        tv_to_top.setOnClickListener {
            if (listener != null) {
                listener!!.goTopClick()
            }
        }
        tv_share_back.setOnClickListener {
            if (listener != null) {
                listener!!.shareClick()
            }
        }
        tv_button.setOnClickListener {
            if (listener != null) {
                listener!!.enterButtonClick()
            }
        }
    }

    fun setListener(listener: OnBottomClickListener) {
        this.listener = listener
    }

    interface OnBottomClickListener {
        fun goTopClick()
        fun shareClick()
        fun enterButtonClick()
    }
}