package com.zj365.health.act.healthcode

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.xty.base.act.BaseVmAct
import com.xty.base.vm.BaseVm
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.weight.PinnedHeaderItemDecoration
import com.xty.common.weight.SideBar
import com.xty.network.model.HealthBodyPartBean
import com.xty.network.model.HealthHeaderEntity
import com.zj365.health.R
import com.zj365.health.adapter.healthcode.HealthBodyPartAdapter
import com.zj365.health.databinding.ActHealthCodeBodyPartBinding
import com.zj365.health.vm.HealthBodyPartVm
import retrofit2.http.Path

@Route(path = ARouterUrl.HEALTH_BODY_PART_ACT)
class HealthBodyPartAct : BaseVmAct<HealthBodyPartVm>() {

    var mTitle:String =""
    var id:Long = 0
    val binding by lazy { ActHealthCodeBodyPartBinding.inflate(layoutInflater) }

    val adapter by lazy { HealthBodyPartAdapter() }

    var mList = ArrayList<HealthHeaderEntity<HealthBodyPartBean.HealthBodyPartChildBean>>()

    private var filterSlideList = ArrayList<String>()

    override fun liveObserver() {
        mViewModel.refiningNameLiveData.observe(this){
            if (it.data != null){
                it.data.forEachIndexed { index, healthBodyPartBean ->
                    filterSlideList.add(healthBodyPartBean.first)
                    mList.add(HealthHeaderEntity(healthBodyPartBean.first,HealthBodyPartAdapter.TYPE_HEADER,HealthBodyPartBean.HealthBodyPartChildBean(0L,"",0)))
                    healthBodyPartBean.list.forEachIndexed { index, healthBodyPartChildBean ->
                        mList.add(HealthHeaderEntity(healthBodyPartBean.first,HealthBodyPartAdapter.TYPE_DATA,healthBodyPartChildBean))

                    }
                }
        //        binding.sideBar.setFilterData(filterSlideList.toArray(arrayOf()))
                adapter.setNewInstance(mList)
            }
        }
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)

        mTitle = intent.extras!!.getString("title","")
        id = intent.extras!!.getLong("id",0)
        binding.title.mTvTitle.text = "${mTitle}"
        binding.title.mIvBack.setOnClickListener {
            finish()
        }

        mViewModel.getBodyPart(id)

       /* val phid = PinnedHeaderItemDecoration.Builder(HealthBodyPartAdapter.TYPE_HEADER)
            .enableDivider(false)
            .create()
        phid.setOnScrollToHeaderListener {
            binding.sideBar.changeCurrentTextColor(it)
        }
        binding.recycler!!!!.addItemDecoration(
            phid)*/
        binding.sideBar.setOnTouchingLetterChangedListener(SideBar.OnTouchingLetterChangedListener { s: String ->
            val position: Int = adapter!!.getPositionForSection(s.get(0))
            if (position != -1) {
                binding.recycler!!.layoutManager!!.scrollToPosition(position)
            }
        })

        binding.recycler.adapter = adapter
        binding.recycler.layoutManager = LinearLayoutManager(this)
        
        adapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.rx_child ->{
                    bundle.clear()
                    bundle.putLong("id",mList[position].data.ref_id)
                    RouteManager.goAct(ARouterUrl.HEALTH_BODY_INQUIRY_ACT,bundle)
                }
            }
        }

    }

    override fun setLayout() = binding.root
}