package com.zj365.health.act.healthcode

import android.text.TextUtils
import android.view.KeyEvent
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import android.widget.TextView.OnEditorActionListener
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.tamsiree.rxkit.RxKeyboardTool
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.util.CommonToastUtils
import com.xty.network.model.HealthBodyBean
import com.zj365.health.adapter.healthcode.HealthCodeMainAdapter
import com.zj365.health.databinding.ActHealthCodeMainBinding
import com.zj365.health.vm.HealthCodeMainVm

@Route(path = ARouterUrl.HEALTH_CODE_MAIN_ACT)
class HealthCodeMainAct : BaseVmAct<HealthCodeMainVm>() {
    val binding by lazy { ActHealthCodeMainBinding.inflate(layoutInflater) }

    var searchStr:String? = null

    val leftAdapter by lazy { HealthCodeMainAdapter() }

    val rightAdapter by lazy { HealthCodeMainAdapter() }

    override fun liveObserver() {
        mViewModel.allBodyLiveData.observe(this){
            leftAdapter.setNewInstance(it.data.subList(0,6))
            rightAdapter.setNewInstance(it.data.subList(6,it.data.size))
        }
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = "健康医典"
        binding.title.mIvBack.setOnClickListener {
            finish()
        }

        binding.etSearch.setOnEditorActionListener(object : OnEditorActionListener{
            override fun onEditorAction(p0: TextView?, p1: Int, p2: KeyEvent?): Boolean {
                if(p1 == EditorInfo.IME_ACTION_SEARCH){
                    searchStr = binding.etSearch.text.toString().trim()
                    if(TextUtils.isEmpty(searchStr)){
                        CommonToastUtils.showToast("请输入搜索内容")
                        return true
                    }


                    RxKeyboardTool.hideSoftInput(this@HealthCodeMainAct)
                    return true

                }
                return false
            }

        })

        mViewModel.getAllBody()

        binding.leftRecycler.adapter = leftAdapter
        binding.leftRecycler.layoutManager = GridLayoutManager(this,6,LinearLayoutManager.HORIZONTAL,false)

        leftAdapter.setOnItemClickListener { adapter, view, position ->
            var healthBodyBean = adapter.data[position] as HealthBodyBean
            bundle.clear()
            bundle.putLong("id",healthBodyBean.id)
            bundle.putString("title",healthBodyBean.med_name)
            RouteManager.goAct(ARouterUrl.HEALTH_BODY_PART_ACT,bundle)
        }
        
        binding.rightRecycler.adapter = rightAdapter
        binding.rightRecycler.layoutManager =  GridLayoutManager(this,6,LinearLayoutManager.HORIZONTAL,false)
        rightAdapter.setOnItemClickListener { adapter, view, position ->
            var healthBodyBean = adapter.data[position] as HealthBodyBean
            bundle.clear()
            bundle.putLong("id",healthBodyBean.id)
            bundle.putString("title",healthBodyBean.med_name)
            RouteManager.goAct(ARouterUrl.HEALTH_BODY_PART_ACT,bundle)
        }
    }

    override fun setLayout() = binding.root
}