package com.zj365.mime.fragment

import android.graphics.Typeface
import android.os.Bundle
import android.util.TypedValue
import android.view.View
import android.widget.TextView
import androidx.appcompat.widget.AppCompatTextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.Gson
import com.hjq.toast.ToastUtils
import com.tamsiree.rxkit.RxKeyboardTool
import com.xty.base.fragment.BaseVmFrag
import com.xty.common.TimeSelect
import com.xty.message.adapter.SportDetailAdapter
import com.zj365.mime.R
import com.zj365.mime.act.SportDetailAct
import com.zj365.mime.adapter.DataAdapter
import com.zj365.mime.databinding.FragDetailProgrammeBinding
import com.zj365.mime.vm.SportDetailVm
import com.xty.network.model.WatchData
import org.greenrobot.eventbus.EventBus


class SportDetailProgrammeFrag: BaseVmFrag<SportDetailVm>() {
    val binding by lazy { FragDetailProgrammeBinding.inflate(layoutInflater) }
     var mAdapter= SportDetailAdapter()
    var mDataAdapter= DataAdapter()
    var nowStatus=0
    private val timeSelect by lazy { TimeSelect(requireContext() ){

    }  }
    override fun setViewModel()= SportDetailVm()

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

       mViewModel. userId = requireArguments() .getString("userId").toString()
        mViewModel.id = requireArguments().getString("id").toString()

        binding.mRecycle.adapter=mDataAdapter
        binding.mRecycle.layoutManager= GridLayoutManager(requireContext(),3)
        mDataAdapter.setNewInstance(mutableListOf("","","","","",""))

        binding.mRv2.adapter=mAdapter
        binding.mRv2.layoutManager=LinearLayoutManager(requireContext())
        mAdapter.addChildClickViewIds(R.id.mTime2,R.id.mTime3)
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            RxKeyboardTool.hideSoftInput(requireActivity())
            timeSelect.apply {
                showYear=true
                showMonth=true
                showDay=true
                showHour=true
                showMonth=true
                showSec=false
            }.selectDefault(null,view as TextView, format = "yyyy-MM-dd HH:mm")
            // timeSelect.selectTime(view, format = "HH:mm" )
        }
       mViewModel.findSportsPlanDetail()

        binding.mSend.setOnClickListener {
            when(nowStatus){
                1->{binding.mSend.apply {
                    mViewModel.udpateSportsStatus(nowStatus+1)
                }}
                2->{ binding.mSend.apply {

                    var bean= mViewModel.detailLive.value!!.data
                    var iterator=mAdapter.mapView.iterator()
                    while (iterator.hasNext()){
                        var map=iterator.next()
                        bean.actionList[map.key].realTime=map.value[0].text.toString()
                        bean.actionList[map.key].realEndTime=map.value[1].text.toString()
                        var listGroupInfo=mAdapter.getChildInfo(map.key) //可能有很多组
                        // var childIterator= mAdapter. map2Adapter[map.key]
                        bean.actionList[map.key].groupList.forEachIndexed { i, group ->
                            group.realDataValue1=listGroupInfo[i*2]
                            group.realDataValue2=listGroupInfo[i*2+1]
                        }
                    }
                    bean.waitSend="3"

                    mViewModel.updateSportsPlan(bean)

                }}
            }

        }
    }




    override fun observer() {
        mViewModel.statusLive.observe(this){
            EventBus.getDefault().post("refresh")
            activity?.finish()
        }
        mViewModel.sportFish.observe(this){
            EventBus.getDefault().post("refresh")
            (activity as SportDetailAct).toResultPage()
            mViewModel.findSportsPlanDetail()
        }
        mViewModel.detailLive.observe(this){
            it.data.let {
                when(it.status){
                    1->{binding.mSend.apply {
                        visibility=View.VISIBLE
                        text="开始运动"
                    }}
                    2->{ binding.mSend.apply {
                        visibility=View.VISIBLE
                        text="运动完成"
                    }}
                    else->binding.mSend.visibility=View.GONE
                }
                nowStatus=it.status
                addTitleAdnState(it.planName,it.status)
                binding.mOrgName.text=it.orgName
                binding.mCreateTime.text=it.createTime
                binding.mTime.setValues(it.startTime)
                binding.mSubject.setValues(it.lessonNumber)
                binding.mV1.setValues(it.beforeStamina)
                binding.mV2.setValues(it.beforeSleep)
                binding.mV3.setValues(it.beforeDiet)
                binding.mV4.setValues(it.beforeWarmUp)
                binding.mV5.setValues(it.beforeMusclePain)
                binding.mV6.setValues(it.beforeStretchArea)
                binding.mV7.setValues(it.beforeTargetMuscle)

                var watchBean=Gson().fromJson(it.beforeWatchDate,WatchData::class.java)
                mDataAdapter.setAdapterData(watchBean)
                when(it.status){
                    1,2,3,4-> {
                        it.actionList.forEach { b ->
                            b.itemType = it.status
                            b.groupList.forEach { child ->
                                child.itemType = it.status
                            }
                        }
                    }
                }
             mAdapter.setNewInstance(  it.actionList)
            }

        }
    }
    private fun addTitleAdnState(title:String,status:Int) {

        binding.mView.removeAllViews()

        var tv1=AppCompatTextView(requireContext()).apply {
            text=title
            setTextColor(getColor(R.color.col_313))
            setTextSize(TypedValue.COMPLEX_UNIT_SP, 21f)
            typeface = Typeface.create("sans-serif", Typeface.BOLD)
        }
        var statusBg=if(status!=4) R.drawable.shape_waiting_bg else R.drawable.shape_normal_bg
        var statusCol=if(status!=4) R.color.col_96D else R.color.col_25c

        var statusStr=when(status){
            0->"待发送"
            1->"待运动"
            2->"运动中"
            3->"运动完成"
            4-> "已完成"
            else -> ""
        }

        var text=AppCompatTextView(requireContext()).apply {
            text=statusStr
            setBackgroundResource(statusBg)
            setTextColor(getColor(statusCol))
            setTextSize(TypedValue.COMPLEX_UNIT_SP, 10f)
            setPadding(10,0,10,0)
        }
        binding.mView.addView(tv1)
        binding.mView.addView(text)

    }

}