package com.yidianling.home.ui.view

import android.content.Context
import android.graphics.Typeface
import android.os.Build
import android.support.design.widget.TabLayout
import android.support.v4.content.ContextCompat
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import com.yidianling.common.tools.LogUtil
import com.yidianling.home.R
import com.yidianling.home.constract.HomeViewConfig
import com.yidianling.home.event.IHomeBaseEvent
import com.yidianling.home.model.bean.HomeConsultBean
import com.yidianling.home.model.bean.HomeHeaderBean
import kotlinx.android.synthetic.ydl.home_confide_view.view.*

/**
 * @author <a href="https://www.jianshu.com/u/c1e5310dd724">xujian</a>
 * @描述: 咨询理解模块
 * @Copyright Copyright (c) 2019
 * @Company 壹点灵
 * @date 2019/02/13
 */
class HomeConsultView(private val mContext: Context, private var homeEvent: IHomeBaseEvent?) : LinearLayout(mContext) {
    private var mLastPosition: Int = 0
    /**
     * 专家信息view缓存list
     */
    private var cacheInfoViewList: ArrayList<HomeConsultItemView>? = null
    /**
     * TabLayout.OnTabSelectedListener
     */
    private var listener: ConsultTabSelectedListener? = null

    /**
     * 当前选中cateId
     */
    var cateId = 0

    init {
        initView()
    }

    private fun initView() {
        val params = RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        layoutParams = params
        orientation = VERTICAL
        View.inflate(mContext, R.layout.home_confide_view, this)
        homeModuleConfideViewHomeCommonTitleView.setTitle(HomeViewConfig.getOrder().consultTitle)
        homeModuleConfideViewHomeCommonTitleView.setOnClickListener {
            homeEvent?.consultMoreClick()
        }
    }

    /**
     * 创建tab栏
     * @param lastPosition 上次选中的下标
     */
    fun setTitle(list: List<HomeHeaderBean.ConsultCategoryDateBean>?, lastPosition: Int) {
        if (list == null || list.isEmpty()) {
            visibility = View.GONE
            return
        }
        if (listener != null) {
            tab_layout.removeOnTabSelectedListener(listener!!)
        }
        mLastPosition = lastPosition
        tab_layout.removeAllTabs()
        for ((index, bean) in list.withIndex()) {
            tab_layout.addTab(tab_layout.newTab().setText(bean.name), index == lastPosition)
        }
        listener = ConsultTabSelectedListener(list, tab_layout)
        tab_layout.addOnTabSelectedListener(listener!!)
        val textView = ((tab_layout!!.getChildAt(0) as LinearLayout).getChildAt(lastPosition) as LinearLayout).getChildAt(1)
        if (textView != null && textView is TextView) {
            textView.textSize = 17f
            textView.setTextColor(ContextCompat.getColor(mContext, R.color.platform_color_242424))
            textView.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            tab_layout.setOnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
                LogUtil.e("scrollX=$scrollX")
                LogUtil.e("oldScrollX=$oldScrollX")
            }
        }
    }

    /**
     * 设置数据
     */
    fun setConsultInfoView(list: List<HomeConsultBean.ListBean>?,category : HomeHeaderBean.ConsultCategoryDateBean?) {
        if (list == null || list.isEmpty()) {
            ll_content.visibility = View.GONE
            return
        }

        cateId = category?.id?:0

        ll_content.visibility = View.VISIBLE

        if (cacheInfoViewList == null) {
            cacheInfoViewList = ArrayList()
        }
        //创建HomeConsultItemView：每个tab最多显示三个专家
        if (cacheInfoViewList!!.size == list.size) {
            for (index in 0 until list.size) {
                //设置数据
                val itemView = ll_content.getChildAt(index)
                if (itemView is HomeConsultItemView){
                    itemView.setData(list[index], index, index == list.size - 1,"${category?.id}")
                }
            }
        } else {
            ll_content.removeAllViews()
            cacheInfoViewList?.clear()
            for (index in 0 until list!!.size) {
//                if (index > 2) {
//                    //只取前三条数据
//                    break
//                }
                //数据大于缓存view数量，创建view
                if (index > cacheInfoViewList!!.size - 1) {
                    val homeConsultItemView = createConsultInfoView()
                    cacheInfoViewList!!.add(homeConsultItemView)
                }
                //设置数据
                cacheInfoViewList!![index].setData(list[index]!!, index, index == list!!.size - 1,"${category?.id}")
                //添加到布局
                ll_content.addView(cacheInfoViewList!![index])
            }
        }
    }

    /**
     * 创建专家信息view
     */
    private fun createConsultInfoView(): HomeConsultItemView {
        return HomeConsultItemView(mContext, homeEvent)
    }


    inner class ConsultTabSelectedListener : TabLayout.OnTabSelectedListener {
        private var list: List<HomeHeaderBean.ConsultCategoryDateBean>? = null
        private var tabLayout: TabLayout? = null

        constructor(list: List<HomeHeaderBean.ConsultCategoryDateBean>, tabLayout: TabLayout) {
            this.list = list
            this.tabLayout = tabLayout
        }

        override fun onTabReselected(tab: TabLayout.Tab?) {
        }

        override fun onTabUnselected(tab: TabLayout.Tab?) {
            var textView = ((tabLayout!!.getChildAt(0) as LinearLayout).getChildAt(tab!!.position) as LinearLayout).getChildAt(1)
            if (textView != null && textView is TextView) {
                textView.textSize = 15f
                textView.setTextColor(ContextCompat.getColor(mContext, R.color.platform_color_333333))
            }
        }

        override fun onTabSelected(tab: TabLayout.Tab?) {
            if (mLastPosition == tab!!.position){
                return
            }
            var textView = ((tabLayout!!.getChildAt(0) as LinearLayout).getChildAt(tab!!.position) as LinearLayout).getChildAt(1)
            if (textView != null && textView is TextView) {
                textView.textSize = 17f
                textView.setTextColor(ContextCompat.getColor(mContext, R.color.platform_color_242424))
                textView.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
            }
            val bean = list!![tab!!.position]
            homeEvent!!.getConsultData(bean, tab!!.position)

        }
    }
}
