package com.zj365.health.act

import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.tamsiree.rxkit.RxTimeTool
import com.xty.base.act.BaseVmAct
import com.xty.common.TimeSelect
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.event.BodyEvent
import com.zj365.health.adapter.BodInfoAdapter
import com.zj365.health.adapter.BodyCompareAdapter
import com.zj365.health.databinding.ActBodyManagerBinding
import com.zj365.health.vm.BodyManagerVm
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.text.SimpleDateFormat
import java.util.*

/**
* 身体数据管理
*/
@Route(path = ARouterUrl.BODY_MANAGER_ACT)
class BodyManagerAct: BaseVmAct<BodyManagerVm>() {
    val binding by lazy{ActBodyManagerBinding.inflate(layoutInflater)}
    var mAdapter= BodInfoAdapter()
    var mCompareAdapter= BodyCompareAdapter()

    var userId:String?=null

    override fun initData() {
        super.initData()
        userId = intent.getStringExtra("userId")
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mIvBack.setOnClickListener { finish() }
        binding.title.mTvTitle.text = "运动管理"

    /*    mAdapter.listUnit.addAll(mViewModel.infoUnit)
        binding.mRecycle.adapter=mAdapter
        binding.mRecycle.layoutManager= GridLayoutManager(this,4)
        mAdapter.setNewInstance(mViewModel.infoList)*/
        setBodyInfo()

        binding.mDetail.setOnClickListener {
            bundle.clear()
            bundle.putString("userId",userId)
            RouteManager.goAct(ARouterUrl.BODY_MANAGER_ACT_HISTORY,bundle)
        }

        binding.mCompare.setOnClickListener {

           var longTime= RxTimeTool.string2Milliseconds( binding.mOrignData.text.toString(), SimpleDateFormat("yyyy-MM-dd"))
            var calendar = Calendar.getInstance()
            calendar.timeInMillis = longTime;
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            selectTime(binding.mCompareData,calendar)
        }

        binding.mData.setOnClickListener {
            var instance = Calendar.getInstance()
            instance.add(Calendar.DAY_OF_MONTH, -1);

            selectTime(binding.mOrignData,instance)
        }

        binding.mAdd.setOnClickListener {
            bundle.clear()
            bundle.putString("userId",userId)
            RouteManager.goAct(ARouterUrl.BODY_INFO_SHOW_OR_ADD,bundle)
        }

        binding.mRv2.adapter=mCompareAdapter
        mCompareAdapter.listUnit.addAll(mViewModel.infoUnit)
        binding.mRv2.layoutManager=LinearLayoutManager(this)
        mCompareAdapter.setNewInstance(mViewModel.compareInfo)

        binding.mCompareData.text=getYestoryYestoryDate()

        binding.mOrignData.text=RxTimeTool.getYestoryDate(format="yyyy-MM-dd")

        mViewModel.findComparData(RxTimeTool.getYestoryDate(format="yyyy-MM-dd") ,getYestoryYestoryDate(),userId)
    }


    private fun selectTime(view:TextView,calendar:Calendar) {
        TimeSelect(this ){
            var data= binding.mOrignData.text.toString()
            var compareData=binding.mCompareData.text.toString()
            var longTimeCompare= RxTimeTool.string2Milliseconds( compareData, SimpleDateFormat("yyyy-MM-dd"))
            var longTime= RxTimeTool.string2Milliseconds( data, SimpleDateFormat("yyyy-MM-dd"))
            if(longTimeCompare>=longTime){
                var compareNewData=RxTimeTool.milliseconds2String((longTime-24*60*60*1000), SimpleDateFormat("yyyy-MM-dd"))
                binding.mCompareData.text=compareNewData
                mViewModel.findComparData(data,compareNewData,userId)
            }else {
                mViewModel.findComparData(data, compareData,userId)
            }

        }.apply {
            endDate=calendar
            selectTime(view)
        }
    }
    override fun liveObserver() {
        mViewModel.compareLiveData.observe(this){
            it.data.let {
                mCompareAdapter.sexType=it.sex

                setCompareInfo(0,it.compareData?.height?:"--",it.data?.height?:"--")
                setCompareInfo(1,it.compareData?.weight?:"--",it.data?.weight?:"--")
                setCompareInfo(2,it.compareData?.bm?:"--",it.data?.bm?:"--")
                setCompareInfo(3,it.compareData?.bf?:"--",it.data?.bf?:"--")
                setCompareInfo(4,it.compareData?.bmi?:"--",it.data?.bmi?:"--")
                setCompareInfo(5,it.compareData?.whf?:"--",it.data?.whf?:"--")
                setCompareInfo(6,it.compareData?.basicM?:"--",it.data?.basicM?:"--")
                setCompareInfo(7,it.compareData?.fatC?:"--",it.data?.fatC?:"--")
                setCompareInfo(8,it.compareData?.mc?:"--",it.data?.mc?:"--")
                setCompareInfo(9,it.compareData?.tweight?:"--",it.data?.tweight?:"--")
                setCompareInfo(10,it.compareData?.scoreS?:"--",it.data?.scoreS?:"--")

                setTopInfo(0,it.newData?.height?:"--")
                setTopInfo(1,it.newData?.weight?:"--")
                setTopInfo(2,it.newData?.bm?:"--")
                setTopInfo(3,it.newData?.bf?:"--")
                setTopInfo(4,it.newData?.bmi?:"--")
                setTopInfo(5,it.newData?.whf?:"--")
                setTopInfo(6,it.newData?.basicM?:"--")
                setTopInfo(7,it.newData?.fatC?:"--")
                setTopInfo(8,it.newData?.mc?:"--")
                setTopInfo(9,it.newData?.tweight?:"--")
                setTopInfo(10,it.newData?.scoreS?:"--")
            }
            mCompareAdapter.notifyDataSetChanged()
            setBodyInfo()
        }
    }

    private fun setTopInfo(position:Int, data:String) {
        mViewModel.infoList[position].apply {
            this.values=data
        }

    }

    private fun setBodyInfo() {
        binding.include1.mN1.text=   mViewModel.infoList[0].name
        binding.include1.mV1.text= "${mViewModel.infoList[0].values }${mViewModel.infoUnit[0]}"

        binding.include1.mN2.text=   mViewModel.infoList[1].name
        binding.include1.mV2.text= "${mViewModel.infoList[1].values }${mViewModel.infoUnit[1]}"

        binding.include1.mN3.text=   mViewModel.infoList[2].name
        binding.include1.mV3.text= "${mViewModel.infoList[2].values }${mViewModel.infoUnit[2]}"

        binding.include1.mN4.text=   mViewModel.infoList[3].name
        binding.include1.mV4.text= "${mViewModel.infoList[3].values }${mViewModel.infoUnit[3]}"

        binding.include2.mN1.text=   mViewModel.infoList[4].name
        binding.include2.mV1.text= "${mViewModel.infoList[4].values }${mViewModel.infoUnit[4]}"

        binding.include2.mN2.text=   mViewModel.infoList[5].name
        binding.include2.mV2.text= "${mViewModel.infoList[5].values }${mViewModel.infoUnit[5]}"

        binding.include2.mN3.text=   mViewModel.infoList[6].name
        binding.include2.mV3.text= "${mViewModel.infoList[6].values }${mViewModel.infoUnit[6]}"

        binding.include2.mN4.text=   mViewModel.infoList[7].name
        binding.include2.mV4.text= "${mViewModel.infoList[7].values }${mViewModel.infoUnit[7]}"

        binding.include3.mN1.text=   mViewModel.infoList[8].name
        binding.include3.mV1.text= "${mViewModel.infoList[8].values }${mViewModel.infoUnit[8]}"

        binding.include3.mN2.text=   mViewModel.infoList[9].name
        binding.include3.mV2.text= "${mViewModel.infoList[9].values }${mViewModel.infoUnit[9]}"

        binding.include3.mN3.text=   mViewModel.infoList[10].name
        binding.include3.mV3.text= "${mViewModel.infoList[10].values }${mViewModel.infoUnit[10]}"


    }

    private fun setCompareInfo(position:Int,compareData:String,data:String) {
        mViewModel.compareInfo[position].apply {
            this.compareData=compareData
            this.data=data
        }
    }



    /**
    * 前天的日期
    */
    private fun getYestoryYestoryDate():String {
        val calendar = Calendar.getInstance()
                 calendar.add(Calendar.DATE, -2)
           return RxTimeTool.simpleDateFormat("yyyy-MM-dd", calendar.time)
    }

    override fun setLayout()=binding.root

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun bodyInfo(bean: BodyEvent){
        var data= binding.mOrignData.text.toString()
        var compareData=binding.mCompareData.text.toString()
        mViewModel.findComparData(data, compareData,userId)
    }


}