package com.zj365.health.act.healthrecord

import android.graphics.Rect
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.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.tamsiree.rxkit.RxKeyboardTool
import com.xty.base.act.BaseListAct
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.zj365.health.adapter.healthrecord.HealthRecordsAdapter
import com.zj365.health.databinding.ActHealthRecordBinding
import com.zj365.health.databinding.ActHealthRecordsListBinding
import com.zj365.health.vm.HealthRecordsListVm

@Route(path = ARouterUrl.FAMILY_REPORT_LIST_ACT)
class HealthRecordsListAct : BaseListAct<HealthRecordsListVm>() {

    val binding by lazy { ActHealthRecordsListBinding.inflate(layoutInflater) }

    val mAdapter by lazy { HealthRecordsAdapter()}

    var searchTitle:String ? = null

    override fun initAdapter() {
        binding.recyclerView.adapter = mAdapter
        binding.recyclerView.layoutManager = LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
        binding.recyclerView.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(outRect: Rect, itemPosition: Int, parent: RecyclerView) {
                super.getItemOffsets(outRect, itemPosition, parent)
                outRect.bottom = 12
            }
        })
        setRecycleRefresh(binding.recyclerView,binding.mRefresh,true)
    }

    override fun loadData() {

        mViewModel.getHealthRecordList(searchTitle,page.toString())
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = "报告记录"
        binding.title.mIvBack.setOnClickListener {
            finish()
        }
        binding.tvUploadReport.setOnClickListener {
            RouteManager.goAct(ARouterUrl.FAMILY_REPORT_UPLOAD_ACT)
        }
        binding.etSearch.setOnEditorActionListener(object :OnEditorActionListener{
            override fun onEditorAction(p0: TextView?, p1: Int, p2: KeyEvent?): Boolean {
                if(p1 == EditorInfo.IME_ACTION_SEARCH){
                    searchTitle = binding.etSearch.text.toString().trim()
                    if(TextUtils.isEmpty(searchTitle)){
                        CommonToastUtils.showToast("请输入搜索内容")
                        return true
                    }

                    page = 1
                    loadData()
                    RxKeyboardTool.hideSoftInput(this@HealthRecordsListAct)
                    return true

                }
                return false
            }
        })

    }

    override fun onResume() {
        super.onResume()
        page = 1
        loadData()
    }

    override fun liveObserver() {
        mViewModel.healthReportListLiveData.observe(this){
            if (page > 1){
                mAdapter.addData(it.data.records)

            }else{
                mAdapter.setNewInstance(it.data.records)

            }
        }
    }

    override fun setLayout() = binding.root
}