package com.yidianling.consultant.ui.view.topView

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.yidianling.common.tools.LogUtil
import com.yidianling.common.tools.RxImageTool
import com.yidianling.consultant.R
import com.yidianling.consultant.adapter.CategoryDetailAdapter
import com.yidianling.consultant.adapter.EightCategoryAdapter
import com.yidianling.consultant.listener.OnCategoriesSelectedListener
import com.yidianling.consultant.model.bean.AllFilter
import com.yidianling.consultant.model.bean.ChildrenBean
import com.yidianling.consultant.model.bean.EightCategoryBean
import kotlinx.android.synthetic.main.consultant_expert_search_big_shot_one.view.*
import kotlinx.android.synthetic.main.consultant_layout_category_view.*
import kotlinx.android.synthetic.main.consultant_layout_category_view.view.*

class EightCategoryView : LinearLayout, CategoryDetailAdapter.OutAdapterClicklistener,
    EightCategoryAdapter.OnLeftClickListener {


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

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

    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(
        context,
        attrs,
        defStyle
    ) {
        mContext = context
        initView()
    }

    private var mContext: Context? = null
    private var mutableList: MutableList<MutableList<ChildrenBean>>? = null
    private lateinit var mAllfilter: AllFilter
    private var eightCategoryListBean = mutableListOf(
        EightCategoryBean("亲子教育", false, false, false),
        EightCategoryBean("情绪压力", false, false, false),
        EightCategoryBean("恋爱情感", false, false, false),
        EightCategoryBean("婚姻家庭", false, false, false),
        EightCategoryBean("个人成长", false, false, false),
        EightCategoryBean("人际关系", false, false, false),
        EightCategoryBean("职场发展", false, false, false),
        EightCategoryBean("心理健康", false, false, false)
    )
    private lateinit var eightCategoryAdapter: EightCategoryAdapter
    private lateinit var categoryRightAdapter: CategoryDetailAdapter
    private val signList = ArrayList<String>()      // 用于埋点
    var onSubjectsSelectedListener: OnCategoriesSelectedListener? = null
    var isDrag = false
    private fun initView() {
        layoutParams = LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            RxImageTool.dp2px(466f)
        )

        View.inflate(mContext, R.layout.consultant_layout_category_view, this)
        // 重置
        btn_reset.setOnClickListener {
            reset()
        }
        // 确定
        btnConfirm.setOnClickListener {
            onSubjectsSelectedListener?.onCategoriesSelected(signList)

        }
    }

    fun reset() {
        mutableList?.forEachIndexed { index, mutableList ->
            mutableList.forEachIndexed { index, childrenBean ->
                childrenBean.isSelected = false
            }
        }
        mAllfilter.categoryId2List.clear()
        mAllfilter.categoryId3List.clear()
        signList.clear()
        eightCategoryAdapter.notifyDataSetChanged()
        categoryRightAdapter.notifyDataSetChanged()
    }

    fun setData(
        mutableList: MutableList<MutableList<ChildrenBean>>,
        mAllFilter: AllFilter,
        onSubjectsSelectedListener: OnCategoriesSelectedListener
    ) {
        this.mutableList = mutableList
        this.mAllfilter = mAllFilter
        this.onSubjectsSelectedListener = onSubjectsSelectedListener
        eightCategoryAdapter = mContext?.let { EightCategoryAdapter(it, eightCategoryListBean) }!!
        eightCategoryAdapter.setleftClick(this)
        eight_category.layoutManager = LinearLayoutManager(mContext)
        eight_category.adapter = eightCategoryAdapter

        categoryRightAdapter = CategoryDetailAdapter(mContext!!, mutableList, eightCategoryListBean)
        val manager = LinearLayoutManager(mContext)
        category_detail.layoutManager = manager
        category_detail.adapter = categoryRightAdapter
        categoryRightAdapter.setOutClicklistener(this)
        category_detail.setOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
                    isDrag = false
                }
            }

            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                if (!isDrag) {
                    var firstVisibleItem: Int = manager.findFirstVisibleItemPosition()
                    eightCategoryListBean.forEachIndexed { index, eightCategoryBean ->
                        eightCategoryListBean[index].selected = index == firstVisibleItem
                    }
                    eightCategoryAdapter.notifyDataSetChanged()
                }
            }
        })
    }

    override fun outAdapterClick(outPosition: Int, inPosition: Int) {
        //之前的老数据多选分类页。
        if (inPosition == 0) {
            if (mutableList?.get(outPosition)?.get(0)?.isSelected!!) {
                mutableList?.get(outPosition)?.get(0)?.isSelected = false
                mAllfilter.categoryId2List.remove(mutableList?.get(outPosition)?.get(0)?.cate_id)
                signList.remove(eightCategoryListBean[outPosition].string)
            } else {
                mutableList?.get(outPosition)?.forEachIndexed { index, _ ->
                    mutableList?.get(outPosition)?.get(index)?.isSelected = index == 0
                    if (index == 0) {
                        mutableList?.get(outPosition)?.get(0)?.cate_id?.let {
                            mAllfilter.categoryId2List.add(
                                it
                            )
                        }
                        signList.add(eightCategoryListBean[outPosition].string)
                    } else {
                        mAllfilter.categoryId3List.remove(
                            mutableList?.get(outPosition)?.get(index)?.cate_id
                        )
                        signList.remove(mutableList?.get(outPosition)?.get(index)?.cate_name)
                    }
                }
            }
        } else {
            if (!mutableList?.get(outPosition)?.get(inPosition)?.isSelected!! && mutableList?.get(
                    outPosition
                )?.get(0)?.isSelected!!
            ) {
                mutableList?.get(outPosition)?.get(0)?.isSelected = false
                mAllfilter.categoryId2List.remove(mutableList?.get(outPosition)?.get(0)?.cate_id)
                signList.remove(eightCategoryListBean[outPosition].string)
            }
            mutableList?.get(outPosition)?.get(inPosition)?.isSelected =
                !mutableList?.get(outPosition)?.get(inPosition)?.isSelected!!
            if (mutableList?.get(outPosition)?.get(inPosition)?.isSelected!!) {
                mutableList?.get(outPosition)?.get(inPosition)?.cate_id?.let {
                    mAllfilter.categoryId3List.add(
                        it
                    )
                }
                mutableList?.get(outPosition)?.get(inPosition)?.cate_name?.let { signList.add(it) }
            } else {
                mAllfilter.categoryId3List.remove(
                    mutableList?.get(outPosition)?.get(inPosition)?.cate_id
                )
                signList.remove(mutableList?.get(outPosition)?.get(inPosition)?.cate_name)
            }
        }
        mutableList?.forEachIndexed { index, mutableList ->
            eightCategoryListBean.get(index).hasData = false
            mutableList.forEachIndexed { indexin, childrenBean ->
                if (childrenBean.isSelected) {
                    eightCategoryListBean.get(index).hasData = true
                    return@forEachIndexed
                }
            }
        }
        eightCategoryAdapter.notifyDataSetChanged()
        categoryRightAdapter.notifyDataSetChanged()
    }

    override fun onLeftClick(leftPosition: Int) {
        eightCategoryListBean.forEachIndexed { index, eightCategoryBean ->
            eightCategoryListBean[index].selected = index == leftPosition
        }
        eightCategoryAdapter.notifyDataSetChanged()
        isDrag = true
        category_detail.scrollToPosition(leftPosition)
    }
}