package com.abbidot.tracker.deprecated.ui.activity.data

import android.text.TextUtils
import android.view.View
import androidx.activity.viewModels
import androidx.fragment.app.commit
import com.abbidot.baselibrary.constant.MMKVKey
import com.abbidot.baselibrary.util.MMKVUtil
import com.abbidot.baselibrary.util.Utils
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.bean.HistoryDataBean
import com.abbidot.tracker.bean.PetBean
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.constant.GetResultCallback
import com.abbidot.tracker.databinding.ActivityHistoryDataBinding
import com.abbidot.tracker.ui.common.map.HistoryDataMapCommon
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.vm.DataDetailViewModel
import com.abbidot.tracker.vm.GeoCoderViewModel
import com.abbidot.tracker.widget.MapMarkerInfoView
import com.abbidot.tracker.widget.VerticalTopToBottomSeekBar

/**
 * 历史数据
 */
class HistoryDataActivity :
    BaseActivity<ActivityHistoryDataBinding>(ActivityHistoryDataBinding::inflate) {

    val mDataDetailViewModel: DataDetailViewModel by viewModels()
    val mGeoCoderViewModel: GeoCoderViewModel by viewModels()

    //方便保存地理反编译信息
    private lateinit var mHistoryDataList: MutableList<HistoryDataBean>

    lateinit var mPet: PetBean

    private lateinit var mHistoryDataMapCommon: HistoryDataMapCommon

    override fun getTopBar() = mViewBinding.activeHistoryDataBar.titleTopBar

    override fun initData() {
        super.initData()
        //设置沉浸状态栏颜色
        mViewBinding.activeHistoryDataBar.titleTopBarLayout.setBackgroundResource(
            R.color.sleep_time_gray_color
        )
        setTopBarTitle(R.string.data_active_history)

        mPet = Util.getParcelableAdaptive(intent, ConstantString.Pet, PetBean::class.java)!!
        mHistoryDataList = mutableListOf()

        mHistoryDataMapCommon = HistoryDataMapCommon()
        val fragment = mHistoryDataMapCommon.getMapFragment(
            this, MapMarkerInfoView(mContext), mGeoCoderViewModel,VerticalTopToBottomSeekBar(mContext)
        ) {}
        supportFragmentManager.commit {
            add(R.id.history_data_map_view, fragment)
        }

        val today = Utils.getCurrentTime(Utils.DATE_FORMAT_PATTERN_CN)
        mViewBinding.historyDataHorizontalCalenderView.setOnDaySelectedListener { hasChanged, year, month, day, week, item ->
            if (hasChanged) {
                val date = if (item.isCurrent) {
                    today
                } else {
                    mViewBinding.historyDataHorizontalCalenderView.fillingDate(
                        "-", year, month, day
                    )
                }
                mViewBinding.historyDataDate.text = date
                initState()
                mDataDetailViewModel.getHistoryByDay(this, mPet.deviceId, 1, 1)
            }
        }

        val type = MMKVUtil.getInt(MMKVKey.LineSelectPosition, ConstantInt.Line)
        if (type == ConstantInt.Line) {
            mViewBinding.historyDateMapLine.isChecked = true
        } else {
            mViewBinding.historyDateMapNumber.isChecked = true
        }

        initEven()

        initState()
    }

    override fun liveDataObserve() {
        mDataDetailViewModel.mHistoryDetailLive.observe(this) {
            dealRequestResult(it, object : GetResultCallback {
                override fun onResult(any: Any) {
                    val data = it.getOrNull()!!
                    setLatLngData(data)
                }
            })
        }

        //地理反编译成功返回
        mGeoCoderViewModel.mLatLonAddressLiveData.observe(this) {
            mViewBinding.historyDataAddress.apply {
                visibility = if (TextUtils.isEmpty(it)) {
                    View.GONE
                } else {
                    View.VISIBLE
                }
                text = it
                mHistoryDataList[mViewBinding.seekMapLine.progress].address = it
            }
        }
    }

    private fun initEven() {
        mViewBinding.seekMapLine.setOnSeekBarChangeProgressListener(object :
            VerticalTopToBottomSeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(progress: Int) {
                mHistoryDataMapCommon.slideChanged(progress)
            }

            override fun onStopChanged(progress: Int) {
                val historyDataBean = mHistoryDataList[progress]
                showDate(historyDataBean.timeStamp * 1000)

                val address = historyDataBean.address
                if (TextUtils.isEmpty(address)) {
                    mHistoryDataMapCommon.slideStopChanged(progress)
                } else {
                    mViewBinding.historyDataAddress.text = address
                }
            }
        })

        mViewBinding.historyDateMapTypeGroup.setOnCheckedChangeListener { _, checkedId ->
            mViewBinding.seekMapLine.progress = mViewBinding.seekMapLine.max
            when (checkedId) {
                mViewBinding.historyDateMapLine.id -> {
                    MMKVUtil.putInt(MMKVKey.LineSelectPosition, ConstantInt.Line)
                    mHistoryDataMapCommon.addSetLine()
                }

                mViewBinding.historyDateMapNumber.id -> {
                    MMKVUtil.putInt(MMKVKey.LineSelectPosition, ConstantInt.Point)
                    mHistoryDataMapCommon.addSetNumber()
                }
            }
        }
    }

    private fun initState() {
        mViewBinding.apply {
            historyDataAddress.visibility = View.GONE
            historyDataDate.visibility = View.GONE
            seekMapLine.progress = 0
        }
        mHistoryDataMapCommon.clearAllMarker()
        setSeekBarMax(0)
    }

    private fun setLatLngData(data: MutableList<HistoryDataBean>) {
        mHistoryDataList = data
        if (data.size == 0) return

        val max = data.size - 1
        setSeekBarMax(max)
        showDate(data[max].timeStamp * 1000)

        mHistoryDataMapCommon.setLatLngData(data)
    }

    private fun showDate(time: Long) {
        val date = Utils.formatTime(time, Utils.DATE_FORMAT_PATTERN_CN3)
        mViewBinding.historyDataDate.apply {
            visibility = View.VISIBLE
            text = date
        }
    }

    private fun setSeekBarMax(max: Int) {
        mViewBinding.seekMapLine.let {
            it.max = max
            //定位中心显示最后一个经纬度
            it.progress = max
        }
    }
}