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.alibaba.android.arouter.facade.annotation.Route
import com.xty.base.act.IBaseAct
import com.xty.base.vp2.VpAdapter
import com.xty.common.arouter.ARouterUrl
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.ActDatamanageBinding
import com.zj365.health.fragment.*


/**
 * 我的上传
 */
@Route(path = ARouterUrl.DATA_MANAGE)
class DataManageAct : IBaseAct() {
    private val fragmentManager: FragmentManager = supportFragmentManager

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

    var fromUserId: String = ""

    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(DataManageFrag().newInstance())
        listFragment.add(DataManageMedicalFrag().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 {

                val get = listFragment.get(1) as DataManageMedicalFrag
                get.exitManage()
            }
        }
        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
                var visibility = binding.title.mTvRight.visibility
                if (visibility == View.VISIBLE) {
                    if (position == 1) {
                        visibility = View.VISIBLE
                    } else {
                        visibility = View.GONE
                    }
                }

            }

            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()
    }

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

    }

}