package com.zj365.health.act.familyhealth

import android.annotation.SuppressLint
import android.os.Bundle
import android.os.CountDownTimer
import android.util.Log
import android.view.View
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.maps.AMap
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.UiSettings
import com.amap.api.maps.model.BitmapDescriptor
import com.amap.api.maps.model.BitmapDescriptorFactory
import com.amap.api.maps.model.CameraPosition
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.Marker
import com.amap.api.maps.model.MarkerOptions
import com.amap.api.maps.model.MyLocationStyle
import com.xty.base.act.BaseVmAct
import com.xty.common.arouter.ARouterUrl
import com.xty.network.model.WatchCurrentTimeBean
import com.zj365.health.R
import com.zj365.health.adapter.familyhealth.FamilyHealthRealTimeAdapter
import com.zj365.health.databinding.ActFamilyHealthRealTimeBinding
import com.zj365.health.vm.FamilyHealthRealTimeVm
import java.util.Timer
import java.util.TimerTask

/**
 * 家人健康-实时
 */
@Route(path = ARouterUrl.FAMILY_HEALTH_REAL_TIME_ACT)
class FamilyHealthRealTimeAct : BaseVmAct<FamilyHealthRealTimeVm>() {

    val binding by  lazy { ActFamilyHealthRealTimeBinding.inflate(layoutInflater) }

    val mAdapter by lazy { FamilyHealthRealTimeAdapter() }

    private var bitmapDescriptor: BitmapDescriptor? = null
    private var aMap: AMap? = null

    var userId:Long = 0

    var measureBeanList = ArrayList<WatchCurrentTimeBean.MeasureBean>()
    var myLocationStyle = MyLocationStyle()

    var countDownTimer:CountDownTimer? = null


    override fun liveObserver() {
        mViewModel.watchCurrentTimeLiveData.observe(this){
            measureBeanList.clear()
           var  latLng = LatLng(it.data.gps.lat, it.data.gps.lng)
            locationPoint(R.mipmap.icon_location_market,latLng)

            measureBeanList.add(WatchCurrentTimeBean.MeasureBean(1,it.data.tiwen.time,it.data.tiwen.v))
            measureBeanList.add(WatchCurrentTimeBean.MeasureBean(2,it.data.xinlv.time,it.data.xinlv.v))
            measureBeanList.add(WatchCurrentTimeBean.MeasureBean(3,it.data.xueya.time,it.data.xueya.v))

            measureBeanList.add(WatchCurrentTimeBean.MeasureBean(4,it.data.xueyang.time,it.data.xueyang.v))
            binding.tvTips.visibility = View.GONE

            mAdapter.setNewInstance(measureBeanList)



        }
    }

    fun timeStart() {
        countDownTimer =object : CountDownTimer(60*1000,1000){//1000ms运行一次onTick里面的方法
            override fun onFinish() {
            loadingView.dismiss()
            mViewModel.getWatchCurrentTime(userId)
            }

            override fun onTick(millisUntilFinished: Long) {
                loadingView.binding.tipTextView.text = "测量中"
                loadingView.show()
            }
        }.start()

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.mapView.onCreate(savedInstanceState)
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text ="实时详情"
        userId = intent.extras!!.getLong("id")
        binding.title.mIvBack.setOnClickListener {
            finish()
        }
        initAdapter()
        myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.mipmap.icon_location_market))
        mViewModel.getWatchCurrentTime(userId)
        aMap = binding.mapView.map

    }

    fun initAdapter(){
        binding.recycler.layoutManager = LinearLayoutManager(this)
        binding.recycler.adapter = mAdapter
        var dividerItemDecoration = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
        dividerItemDecoration.setDrawable(resources.getDrawable(R.drawable.shape_ebf_0_5))
        binding.recycler.addItemDecoration(dividerItemDecoration)
        mAdapter.addChildClickViewIds(R.id.tv_real_time_mearsure)
        mAdapter.setOnItemChildClickListener{adapter, view, position ->
            val measureBean = adapter.data[position] as WatchCurrentTimeBean.MeasureBean
            timeStart()
            mViewModel.measureData(userId,measureBean.type)
            binding.tvTips.visibility = View.VISIBLE
        }

    }

    override fun setLayout() = binding.root

    private fun locationPoint(makerRes:Int,latLng:LatLng) {

        aMap?.let {
            bitmapDescriptor=BitmapDescriptorFactory
                .fromResource(makerRes)

            val cameraUpdate = CameraUpdateFactory.newCameraPosition(CameraPosition(latLng,15f,0f,0f))
            it.moveCamera(cameraUpdate)

            it.clear()
           it.addMarker(MarkerOptions().position(latLng)
                .icon(bitmapDescriptor)
            )
        }



    }

    override fun onDestroy() {
        super.onDestroy()
        binding.mapView.onDestroy()
        countDownTimer?.cancel()
        countDownTimer = null
    }

    override fun onPause() {
        super.onPause()
        binding.mapView.onPause()
    }


    override fun onResume() {
        super.onResume()
        binding.mapView.onResume()
    }

    @SuppressLint("MissingSuperCall")
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        binding.mapView.onSaveInstanceState(outState)
    }
}