package com.zj365.health.act

import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
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.IBaseAct
import com.xty.base.vp2.VpAdapter
import com.xty.common.UmengEventId
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.onEventObject
import com.zj365.health.R
import com.zj365.health.databinding.ActHealthReportBinding
import com.zj365.health.fragment.FragDayReport
import com.zj365.health.fragment.FragWeekReport
import com.zj365.health.vm.ReportVm
import com.zj365.health.weight.DialogTip

/**
 * 健康日报
 */
@Route(path = ARouterUrl.HEALTH_REPORT)
class HealthReportAct : IBaseAct() {
    private var pageType: Int = 0//页面导航标签（默认日报）
    var isDemonstrate: Boolean = false //是否是演示模式
    var isEstimate: Boolean = true //是否是评估
    val dialogBind by lazy {
        DialogTip(this, "需绑定中健三六五健康手表，才能查看", "去绑定") {
            RouteManager.goAct(ARouterUrl.DEVICE_CHOICE)
        }
    }

    val binding by lazy { ActHealthReportBinding.inflate(layoutInflater) }
    val reportArray by lazy { resources.getStringArray(R.array.report) }
    var positionPage = 0
    var userId = ""
    var listFrag = mutableListOf<Fragment>()

    override fun openCache() {
        super.openCache()

    }

    override fun initData() {
        super.initData()
    }

    override fun setLayout() = binding.root
    override fun initView() {
        super.initView()

        statusBar(binding.title.mView)
        binding.title.mIvBack.setOnClickListener { finish() }
        binding.title.mIvReight.apply {
            visibility = View.VISIBLE
            this.setImageResource(R.mipmap.icon_share)
            this.setOnClickListener {
                if (isDemonstrate) {
                    dialogBind.show()
                    return@setOnClickListener
                }
                when (positionPage) {
                    0 -> {
                        (listFrag[positionPage] as FragDayReport).share()
                    }

                    else -> {
                        (listFrag[positionPage] as FragWeekReport).share()
                    }
                }
            }
        }
        intent.extras?.let {
            if (!it.getString("id").isNullOrEmpty()) {
                userId = it.getString("id")!!.toString()
            }
            isDemonstrate = it.getBoolean(
                "isDemonstrate",
                false
            )
            pageType = it.getInt(
                "pageType",
                0
            )
        }

        val bg: Drawable = ContextCompat.getDrawable(this, R.drawable.shape_white)!!
        val parentView = binding.title.mTvTitle.parent as View
        parentView.background = bg
        if (isDemonstrate) {
            binding.mSubmit.visibility = View.VISIBLE
            binding.mSubmit.setOnClickListener { finish() }
            binding.title.mTvTitle.text = getString(R.string.health_my_de)
        } else {
            binding.title.mTvTitle.text = getString(R.string.health_my)
        }
        initTab()
        initVp2()

    }

    private fun initTab() {
        reportArray.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?) {
                positionPage = tab!!.position
                binding.mVp2.setCurrentItem(tab.position, false)
            }

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

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

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

    private fun initVp2() {


        listFrag.add(setArgumentFrag(FragDayReport(), ReportVm.ReportInfoType.Day, userId))
        listFrag.add(setArgumentFrag(FragWeekReport(), ReportVm.ReportInfoType.Week, userId))
        listFrag.add(setArgumentFrag(FragWeekReport(), ReportVm.ReportInfoType.Month, userId))
        listFrag.add(setArgumentFrag(FragWeekReport(), ReportVm.ReportInfoType.Year, userId))

        binding.mVp2.adapter = VpAdapter(listFrag, this)
        //binding.mVp2.offscreenPageLimit = 1
        binding.mVp2.currentItem = positionPage
        binding.mVp2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                onEventObject(
                    baseContext,
                    UmengEventId.HealthReport,
                    "${reportArray[position]}报告详情页"
                )
                positionPage = position
                binding.xTablayout.getTabAt(position)!!.select()
            }
        })
        binding.mVp2.post {
            binding.mVp2.setCurrentItem(pageType, false)
        }
    }

    private fun setArgumentFrag(
        frag: Fragment,
        index: ReportVm.ReportInfoType,
        id: String = ""
    ): Fragment {
        var bundle = Bundle()
        bundle.putSerializable("index", index)
        bundle.putString("id", id)
        frag.arguments = bundle
        return frag
    }


}