package com.zj365.health.act.datamanage

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.GsonUtils
import com.xty.base.fragment.BaseFragList
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.event.HealthDataChangeEvent
import com.xty.common.util.CommonToastUtils
import com.zj365.health.databinding.FragDatamanagerUserMedicalBinding
import com.zj365.health.vm.HealthRecordVm
import com.xty.network.model.CancerGoodsBean
import com.xty.network.model.MedicalDataBean
import com.xty.network.model.PaingBean
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 健康档案--用药上传
 */
class DataManageUseMedicalFrag : BaseFragList<HealthRecordVm>() {
    //val binding by lazy{FragWholeRankBinding.inflate(layoutInflater)}
    val binding by lazy { FragDatamanagerUserMedicalBinding.inflate(layoutInflater) }
    val mAdapter by lazy { DataManageMedicalAdapter() }
    var isChange = false
    override fun setLayout() = binding.root

    var result = mutableListOf<String>()
    override fun initView() {
        super.initView()
        mViewModel.getHealthDataList(page, "7")
        binding.mRefresh.setOnLoadMoreListener {
            loadMore()
        }
        initAdapter()
    }

    override fun initAdapter() {
        setRecycleRefresh(binding.mRecycle, binding.mRefresh, true)
        binding.mRecycle.layoutManager = LinearLayoutManager(activity)
        binding.mRecycle.adapter = mAdapter

        mAdapter.setOnItemClickListener { adapter, view, position ->
            val healthDataBean = mAdapter.data.get(position) as MedicalDataBean
            if (isManage) {
                val findViewById = view.findViewById<ImageView>(com.zj365.health.R.id.ivSelected)
                findViewById.isSelected = !findViewById.isSelected
                healthDataBean.isSelectd = findViewById.isSelected
                val selectData = this.mAdapter.getSelectData()
                changeButtonStatus(selectData.size == 1)
            } else {
                bundle.clear()
                bundle.putString("data", GsonUtils.toJson(healthDataBean))
                bundle.putString("type","7")
                RouteManager.goAct(ARouterUrl.DATAMANAGE_DETAIL_ACT, bundle)
            }
        }

    }

    override fun refresh() {
        page = 1
        if (isManage){
            toManage(false)
        }
        mViewModel.getHealthDataList(page, "7")
    }

    private fun loadMore() {
        page++
        if (isManage) {
            toManage(false)
        }
        mViewModel.getHealthDataList(page, "7")
    }
    private fun  isEnableLoadMore(datas: PaingBean<MedicalDataBean>){

        if (binding.mRefresh.isLoading){
            binding.mRefresh.finishLoadMore()
        }

        if (mAdapter.data.size==datas.total){
            binding.mRefresh.setEnableLoadMore(false)
        }else{
            binding.mRefresh.setEnableLoadMore(true)
        }
    }

    var isEdit = false
    private fun changeButtonStatus(canChange: Boolean) {
        val dataManageTypeAct = requireActivity() as HealthRecordAct
        dataManageTypeAct.binding.tvChange.isEnabled = canChange
    }

    var isManage = false
    fun toManage(isManager: Boolean) {
        isManage = isManager
        mAdapter.isSelect = isManager
        mAdapter.notifyDataSetChanged()
        if (isManager) {
            activity?.let {
                val dataManageTypeAct = it as HealthRecordAct
                dataManageTypeAct.binding.title.mTvRight.visibility = View.VISIBLE
            }
            changeButtonStatus(mAdapter.getSelectData().size == 1)
            isEdit = true
        } else {
            activity?.let {
                val dataManageTypeAct = it as HealthRecordAct
                dataManageTypeAct.binding.title.mTvRight.visibility = View.GONE
                dataManageTypeAct.exitManage(false)
            }
            mAdapter.reset()
            isEdit = false
        }
    }

    fun alterData(){
        val selectData = mAdapter.getSelectData()
        if (selectData.isEmpty()) {
            return
        }
        val dataValue = selectData.get(0)
        bundle.clear()
        bundle.putString("type","7")
        bundle.putString("data", GsonUtils.toJson(dataValue))
        RouteManager.goAct(ARouterUrl.UPLOAD_Medical_EDIT, bundle)
    }

    var order: CancerGoodsBean? = null
    var status: Int? = null
    private var filePath: String? = null
    override fun initData() {
        super.initData()
        var result = arguments?.getString("order")
        result?.let {
            order = GsonUtils.fromJson(it, CancerGoodsBean::class.java)
        }
    }


    fun newInstance(): DataManageUseMedicalFrag {

        val fragment = DataManageUseMedicalFrag()
        var bundle = Bundle()
        fragment.arguments = bundle
        return fragment
    }


    override fun setViewModel() = HealthRecordVm()
    override fun observer() {

        mViewModel.getHealthData.observe(this) {
            setDate(mAdapter, it.data.records)
            isEnableLoadMore(it.data)

            //添加数据后滚动到第一条
            if (isChange){
                binding.mRecycle.scrollToPosition(0)
                isChange = false
            }
        }
        mViewModel.deleteData.observe(this) {
            refresh()
            val dataManageTypeAct = requireActivity() as HealthRecordAct
            dataManageTypeAct.medicalExitManage()
        }
    }

    fun delete(){
        val selectData = mAdapter.getSelectData()
        if (selectData.isEmpty()){
            CommonToastUtils.showToast("未勾选数据")
            return
        }
        mViewModel.deleteMedical(mAdapter.getSelectData())
    }

    fun exitManage() {
        toManage(false)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun healthDataChange(event: HealthDataChangeEvent){
        isChange = true
        refresh()
    }
}