package com.zj365.health.act.tonguediagnosis

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Route
import com.androidkun.xtablayout.XTabLayout
import com.xty.base.act.BaseVmAct
import com.xty.base.vp2.VpAdapter
import com.xty.common.arouter.ARouterUrl
import com.zj365.health.databinding.ActivityTongueDetailBinding
import com.zj365.health.fragment.tongue.TongueDiagnosisInfoFragment
import com.zj365.health.fragment.tongue.TongueReportFragment
import com.zj365.health.fragment.tongue.TongueUserInfoFragment
import com.zj365.health.vm.TongueDiagnosisVm
import com.xty.network.model.CancerGoodsBean
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

@Route(path = ARouterUrl.TONGUE_DETAIL_ACTIVITY)
class TongueDetailActivity : BaseVmAct<TongueDiagnosisVm>() {
    val binding by lazy { ActivityTongueDetailBinding.inflate(layoutInflater) }
    override fun setLayout() = binding.root
    val listFragment= mutableListOf<Fragment>()
    private val mTitles = arrayOf(
        "基础信息", "问诊信息", "舌诊报告"
    )

    var page = 0

    lateinit var bean : CancerGoodsBean

    override fun initData() {
        super.initData()
        bean = intent.getSerializableExtra("orderBean") as CancerGoodsBean
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = "舌诊详情"
        binding.title.mIvBack.setOnClickListener { finish() }
        initTab()
        initVp2()
    }

    private fun initTab() {
        mTitles.forEach {
            var tab = binding.xTablayout.newTab()
            tab.text = it
            binding.xTablayout.addTab(tab)
        }
        binding.xTablayout.setOnTabSelectedListener(object : XTabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: XTabLayout.Tab?) {
                var filed = tab!!.javaClass.getDeclaredField("mView")
                filed.isAccessible = true
                var tabView = filed.get(tab) as View
                if (tabView.isPressed) {
                    //如果是手动按压的话 跳转
                    binding.mVp2.setCurrentItem(tab!!.position, false)
                }
            }

            override fun onTabUnselected(tab: XTabLayout.Tab?) {
            }

            override fun onTabReselected(tab: XTabLayout.Tab?) {
            }

        })
        binding.xTablayout.post {
            binding.xTablayout.getTabAt(page)!!.select()
        }
    }

    private fun initVp2() {

        listFragment.clear()
        listFragment.add(TongueUserInfoFragment())
        listFragment.add(TongueDiagnosisInfoFragment())
        listFragment.add(TongueReportFragment())

        binding.mVp2.adapter = VpAdapter(listFragment, this)
        binding.mVp2.isUserInputEnabled = false
        binding.mVp2.offscreenPageLimit =1
        binding.mVp2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
            }
        })
        binding.mVp2.post {
            binding.mVp2.setCurrentItem(page, false)
        }

    }

    fun getOrderDetailInfo(){
        mViewModel.loading?.show()
        GlobalScope.launch {
            kotlinx.coroutines.delay(2000)
            mViewModel.tongueGetOrderInfo(bean.orderNo!!)
        }
    }


    override fun liveObserver() {
        mViewModel.orderInfo.observe(this){
            bean = it.data
            listFragment.forEachIndexed { index, fragment ->
                if (fragment.isAdded) {
                    when (index) {
                        0 -> {
                            (fragment as TongueUserInfoFragment).initMyView()
                        }

                        1 -> {
                            (fragment as TongueDiagnosisInfoFragment).initMyView()
                        }

                        2 -> {
                            (fragment as TongueReportFragment).initMyView()
                        }

                        else -> {}
                    }
                }
            }
            mViewModel.loading?.dismiss()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode === AppCompatActivity.RESULT_OK) {
            (listFragment[0] as TongueUserInfoFragment).onActivityResult(requestCode,resultCode,data)
        }
    }

}