package com.yidianling.home.ui.view

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import com.yidianling.common.tools.RxImageTool
import com.yidianling.home.R
import com.yidianling.home.event.IHomeBaseEvent
import com.yidianling.home.model.bean.HomeHeaderBean
import kotlinx.android.synthetic.xlzx.home_button_banner_view.view.*

/**
 * @author <a href="https://www.jianshu.com/u/c1e5310dd724">xujian</a>
 * @描述: 顶部预约专家，即时倾诉，心理课堂，心理测试按钮模块
 * @Copyright Copyright (c) 2019
 * @Company 壹点灵
 * @date 2019/02/13
 */
class HomeButtonBannerView(private val mContext: Context, private var homeEvent: IHomeBaseEvent?) :
    LinearLayout(mContext) {
    /**
     * 数据缓存
     */
    var mDataList: ArrayList<HomeHeaderBean.AskCategoryDataBean>? = null
    /**
     * 间隔
     */
    var margin: Int = 0

    /**
     * 是否添加了实时测试状态View
     */
    var hasRealTestView: Boolean = false

    private var realTestView: HomePagerBannerRealView? = null
    /**
     * 线
     */
    private var lineView: View? = null
    private var buttonParams: LinearLayout.LayoutParams? = null

    init {
        initView()
    }

    private fun initView() {
        val params = RecyclerView.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        layoutParams = params
        View.inflate(mContext, R.layout.home_button_banner_view, this)

//        initButtonView()

        homeModuleButtonBannerFirst.setOnClickListener {
            homeEvent?.reservationExpertsClick()
        }
        homeModuleButtonBannerSecond.setOnClickListener {
            homeEvent?.nowConfideClick()
        }
        homeModuleButtonBannerThird.setOnClickListener {
            homeEvent?.psychologyClassClick()
        }
        homeModuleButtonBannerFourth.setOnClickListener {
            homeEvent?.psychologyTestClick()
        }
    }


    fun initData(
        homeSaleData: List<HomeHeaderBean.HomeSaleDataBean>?,
        homeCategory: List<HomeHeaderBean.AskCategoryDataBean>?
    ) {
        setRealTextView(homeSaleData)
        homeEvent?.let { home_category_view.setEvent(it) };
        home_category_view.initData(homeCategory)
    }

    /**
     * 初始化实时测评状态View
     */
    private fun initRealTextView() {
        if (null == realTestView) {
            var realTestParam = FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
            realTestView = HomePagerBannerRealView(context, homeEvent!!)
            realTestView!!.layoutParams = realTestParam
        }
    }

    private fun initLineView() {
        var lineParam =
            FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, RxImageTool.dip2px(0.5f))
        var dp15 = RxImageTool.dip2px(15f)
        lineParam.leftMargin = dp15
        lineParam.rightMargin = dp15
        lineView = View(context)
        lineView!!.setBackgroundColor(resources.getColor(R.color.home_category_view_test_line))
        lineView!!.layoutParams = lineParam
    }


    /**
     * 实时测评状态赋值
     */
    private fun setRealTextView(list: List<HomeHeaderBean.HomeSaleDataBean>?) {
        if (!hasRealTestView) {
            initLineView()
            ll_home_module_button.addView(lineView)
            initRealTextView()
            ll_home_module_button.addView(realTestView)

            val cutOffParams = FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                RxImageTool.dip2px(0.5f)
            )
            cutOffParams.height = RxImageTool.dip2px(10f)
            val cutOffView = View(context)
            cutOffView.setBackgroundColor(resources.getColor(R.color.home_colorBg))
            cutOffView.layoutParams = cutOffParams

            ll_home_module_button.addView(cutOffView)

            hasRealTestView = true
        }
        realTestView!!.initData(list)
    }

}