package com.zj365.health.act

import android.os.Bundle
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.xty.common.arouter.RouteManager
import com.zj365.health.R
import com.zj365.health.databinding.ActivityHealthEstimateBinding
import com.zj365.health.fragment.FragDayReport
import com.zj365.health.fragment.FragWeekReport
import com.zj365.health.vm.ExceptionVm
import com.zj365.health.vm.ReportVm
import com.zj365.health.weight.DialogTip

@Route(path = ARouterUrl.HEALTH_ESTIMATE)
class HealthEstimateActivity  : BaseVmAct<ExceptionVm>()  {
    val binding by lazy { ActivityHealthEstimateBinding.inflate(layoutInflater) }

    val tabName by lazy { arrayOf("24小时","近7天","近30天") }
    var listFragment = mutableListOf<Fragment>()
    var page = 0
    var isDemonstrate: Boolean = false //是否是演示模式
    var userId = ""

    val dialogBind by lazy {
        DialogTip(this, "需绑定中健三六五健康手表，才能查看","去绑定") {
            RouteManager.goAct(ARouterUrl.DEVICE_CHOICE)
        }
    }

    override fun initData() {
        super.initData()
        intent.extras?.let {
            isDemonstrate = intent.extras!!.getBoolean("isDemonstrate", false)
            if (!it.getString("id").isNullOrEmpty()) {
                userId = it.getString("id").toString()
            }
        }
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        if (isDemonstrate){
            binding.title.mTvTitle.text = getString(R.string.health_estimate_de)
            binding.tvGuid.text = "退出演示模式"
        }else{
            binding.title.mTvTitle.text = getString(R.string.health_estimate)
            binding.tvGuid.visibility = View.GONE
        }
        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
                }
                share()
            }
        }

        binding.tvGuid.setOnClickListener {
            if (isDemonstrate){
                finish()
            } else {
                bundle.clear()
                bundle.putString("id", userId)
                var reportBean =
                when (page) {
                    0->{
                        (listFragment[page] as FragDayReport).mViewModel.infoLive.value?.data
                    }
                    else->{
                        (listFragment[page] as FragWeekReport).mViewModel.infoLive.value?.data
                    }
                }

                bundle.putSerializable("reportBean", reportBean)
                RouteManager.goAct(ARouterUrl.HEALTH_PROP, bundle)
            }
        }

        initTab()
        initViewPager()
    }


    private fun initTab() {
        binding.xTablayout.setxTabDisplayNum(tabName.size)
        tabName.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?) {
                page=tab!!.position
                binding.mVp2.setCurrentItem( tab!!.position,false)
            }

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

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

        })
    }

    private fun initViewPager() {

        listFragment.add(setArgumentFrag(FragDayReport(), ReportVm.ReportInfoType.Day,userId))
        listFragment.add(setArgumentFrag(FragWeekReport(), ReportVm.ReportInfoType.Week,userId,false))
        listFragment.add(setArgumentFrag(FragWeekReport(), ReportVm.ReportInfoType.Week,userId,true))

        binding.mVp2.adapter = VpAdapter(listFragment, this)
        binding.mVp2.currentItem = page
        binding.mVp2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                binding.xTablayout.getTabAt(position)!!.select()
                page=position
                binding.xTablayout.getTabAt(position)!!.select()
            }
        })
        binding.mVp2.post {
            binding.mVp2.setCurrentItem(page, false)
        }
    }

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

    fun share() {
        when (page) {
            0->{
                (listFragment[page] as FragDayReport).share()
            }
            else->{
                (listFragment[page] as FragWeekReport).share()
            }
        }

//        CommonUtils.getMyUserInfo()?.apply {
//            var aesId = AESCrypt.encrypt("${id}")
//            var subtitle = "${name}的24小时健康分析数据"
////            var url = "${BuildConfig.image_prefix}/shareApi/reportDaily/$aesId/${reportCreateTime}"
////            WxUtils.shareWebPage("24小时健康分析报告分享", subtitle, baseContext, url)
//        }
    }

    override fun liveObserver() {

    }

    override fun setLayout() = binding.root
}