package com.zj365.health.act.datamanage

import android.annotation.SuppressLint
import android.content.Intent
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.viewpager2.widget.ViewPager2
import com.xty.base.act.IBaseAct
import com.xty.base.vp2.VpAdapter
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.util.CommonToastUtils
import com.xty.common.weight.tablayout.TabEntity
import com.xty.common.weight.tablayout.listener.CustomTabEntity
import com.xty.common.weight.tablayout.listener.OnTabSelectListener
import com.zj365.health.R
import com.zj365.health.databinding.ActHealthRecordBinding
import com.zj365.health.fragment.*

/**
 * 健康档案
 */
//@Route(path = ARouterUrl.HEALTH_RECORD)
class HealthRecordAct : IBaseAct() {
    private val fragmentManager: FragmentManager = supportFragmentManager

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

    var fromUserId: String = ""
    var type: String = "6"

    val listFragment = mutableListOf<Fragment>()
    var tabName = arrayOf<String>()
    private val mTabEntities: java.util.ArrayList<CustomTabEntity> = java.util.ArrayList()
    private val mTitles = arrayOf(
        "体检上传", "用药上传"
    )

    private fun initVp2() {
        listFragment.add(DataManageTijianFrag().newInstance())
        listFragment.add(DataManageUseMedicalFrag().newInstance())
//        listFragment.add(EstimateReportFrag().newInstance(order))
        tabName = arrayOf("体检上传", "用药上传")
        mTabEntities.clear()
        mTitles.forEach {
            mTabEntities.add(
                TabEntity(
                    it,
                    android.R.drawable.ic_delete,
                    android.R.drawable.ic_delete
                )
            )
        }
        binding.title.mTvRight.apply {
            text = "退出管理"
            setTextColor(resources.getColor(R.color.col_02c))
            setOnClickListener {
                if (type == "6") {
                    tijianExitManage()
                } else {
                    medicalExitManage()
                }
            }
        }

        binding.commonTabLayout.setTabData(mTabEntities)
        binding.commonTabLayout.setOnTabSelectListener(object :
            OnTabSelectListener {
            override fun onTabSelect(position: Int) {
//                if (position == 1){
//                    ToastUtils.showShort("正在优化,请耐心等待")
//                    return
//                }
                binding.commonTabLayout.currentTab = position
                binding.mVp2.currentItem = position
                if (position == 0) {  //体检上传frag
                    type = "6"
                    binding.mAddData.text = "上传体检"

                    val medical = listFragment[1] as DataManageUseMedicalFrag
                    if (medical.isManage) {
                        medical.isManage = false
                        medicalExitManage()
                    }

                } else {  //用药上传frag
                    type = "7"
                    binding.mAddData.text = "上传用药"

                    val tijian = listFragment[0] as DataManageTijianFrag

                    if (tijian.isManage) {
                        tijian.isManage = false
                        tijianExitManage()
                    }
                }

//                var visibility = binding.title.mTvRight.visibility
//                if (visibility == View.VISIBLE) {
//                    if (position == 1) {  //体检上传frag
//                        type = "6"
//                    } else {  //用药上传frag
//                        type = "7"
//                    }
//                }7

            }

            override fun onTabReselect(position: Int) {
            }
        })


        binding.mVp2.adapter = VpAdapter(listFragment, this)
        binding.mVp2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                binding.commonTabLayout.currentTab = position
            }
        })
        binding.mVp2.currentItem = 0
        binding.mVp2.isUserInputEnabled = false
    }


    override fun setLayout() = binding.root
    override fun initData() {
        super.initData()
        intent.extras?.let {
            fromUserId = it.getString("fromUserId").toString()
        }
    }


    @SuppressLint("CommitTransaction")
    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mIvBack.setOnClickListener { finish() }
        binding.title.mTvTitle.text = "健康档案"
//        fragment = DataUploadFrag()
//        fragmentManager.beginTransaction().apply {
//            replace(R.id.fragment, fragment)
//            commit()
//        }
        initVp2()

        binding.mAddData.setOnClickListener {
            //上传图片
            bundle.clear()
            bundle.putString("type", type)
            RouteManager.goAct(ARouterUrl.UPLOAD_Medical, bundle)
        }

        binding.mTvManage.setOnClickListener {
            if (type == "6") {
                val tijian = listFragment[0] as DataManageTijianFrag
                if (tijian.mAdapter.data.size > 0) {
                    tijian.toManage(!tijian.isManage)
                    exitManage(tijian.isManage)
                } else {
                    CommonToastUtils.showToast("暂无数据")
                }
            } else {
                val medical = listFragment[1] as DataManageUseMedicalFrag
                if (medical.mAdapter.data.size > 0) {
                    medical.toManage(!medical.isManage)
                    exitManage(medical.isManage)
                } else {
                    CommonToastUtils.showToast("暂无数据")
                }
            }
        }

        binding.mDelete.setOnClickListener {
            if (type == "6") {
                val tijian = listFragment[0] as DataManageTijianFrag
                tijian.delete()
            } else {
                val medical = listFragment[1] as DataManageUseMedicalFrag
                medical.delete()
            }
        }

        binding.tvChange.setOnClickListener {
            if (type == "6") {
                val tijian = listFragment[0] as DataManageTijianFrag
                tijian.alterData()
            } else {
                val medical = listFragment[1] as DataManageUseMedicalFrag
                medical.alterData()
            }
        }
    }

    fun tijianExitManage() {
        val tijian = listFragment[0] as DataManageTijianFrag
        tijian.exitManage()
        exitManage(false)
    }

    fun medicalExitManage() {
        val medical = listFragment[1] as DataManageUseMedicalFrag
        medical.exitManage()
        exitManage(false)
    }

    fun exitManage(isManage: Boolean) {
        if (isManage) {
            binding.mTvManage.visibility = View.GONE
            binding.mDelete.visibility = View.VISIBLE
            binding.mAddData.visibility = View.GONE
            binding.tvChange.visibility = View.VISIBLE
        } else {
            binding.mTvManage.visibility = View.VISIBLE
            binding.mDelete.visibility = View.GONE
            binding.mAddData.visibility = View.VISIBLE
            binding.tvChange.visibility = View.GONE
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
//        fragment.onActivityResult(requestCode, resultCode, data)

    }

}