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

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.content.Intent
import android.text.TextUtils
import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.Animation
import androidx.activity.viewModels
import androidx.fragment.app.commit
import com.abbidot.baselibrary.list.BaseRecyclerAdapter
import com.abbidot.baselibrary.util.AppUtils
import com.abbidot.tracker.R
import com.abbidot.tracker.adapter.SelectMapListDialogAdapter
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.bean.HistoryDataBean
import com.abbidot.tracker.bean.MapDeviceBean
import com.abbidot.tracker.bean.MenuTxtBean
import com.abbidot.tracker.bean.PetBean
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.databinding.ActivityHomeMapNavigationBinding
import com.abbidot.tracker.dialog.SelectMapListDialog
import com.abbidot.tracker.ui.common.map.NavigationMapCommon
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.util.ViewUtil
import com.abbidot.tracker.vm.FencesMapViewModel
import com.abbidot.tracker.vm.GeoCoderViewModel


/**
 * 首页跳转导航
 */
class HomeMapNavigationActivity :
    BaseActivity<ActivityHomeMapNavigationBinding>(ActivityHomeMapNavigationBinding::inflate) {

    val mFencesMapViewModel: FencesMapViewModel by viewModels()
    private val mGeoCoderViewModel: GeoCoderViewModel by viewModels()

    private val mMapGooglePackageName = "com.google.android.apps.maps"
    private val mMapGaoDePackageName = "com.autonavi.minimap"

    private lateinit var mSelectMapListDialogAdapter: SelectMapListDialogAdapter
    private var mSelectMapListDialog: SelectMapListDialog? = null
    var mMapDeviceBean: MapDeviceBean? = null

    private lateinit var mNavigationMapCommon: NavigationMapCommon

    override fun getTopBar() = mViewBinding.homeMapNavigationBar.titleTopBar

    override fun initData() {
        super.initData()
        var historyDataBean: HistoryDataBean? = null
        var pet: PetBean? = null
        intent.extras?.apply {
            pet = Util.getParcelableAdaptive(intent, ConstantString.Pet, PetBean::class.java)
            historyDataBean = Util.getParcelableAdaptive(
                intent, ConstantString.HistoryDataBean, HistoryDataBean::class.java
            )
            mMapDeviceBean = Util.getParcelableAdaptive(
                intent, ConstantString.MapDeviceBean, MapDeviceBean::class.java
            )
        }
        if (null == historyDataBean) historyDataBean = HistoryDataBean()
        if (null == pet) pet = PetBean()
        if (null == mMapDeviceBean) mMapDeviceBean = MapDeviceBean()
//
//        val title = String.format(getString(R.string.map_navigate), pet!!.petName)
//        setTopBarTitle(title)

        mNavigationMapCommon = NavigationMapCommon()
        val fragment = mNavigationMapCommon.getMapFragment(mContext, pet!!.imgurl)
        supportFragmentManager.commit {
            add(R.id.home_map_navigation_map_view, fragment)
        }

        val mapList = mutableListOf<MenuTxtBean>()
        ViewUtil.instance.addMenuBean(
            mapList,
            getString(R.string.map_navigate_map_google),
            mMapGooglePackageName,
            R.drawable.ico_map_google
        )
        ViewUtil.instance.addMenuBean(
            mapList,
            getString(R.string.map_navigate_map_gaode),
            mMapGaoDePackageName,
            R.drawable.ico_map_gaode
        )

        mSelectMapListDialogAdapter = SelectMapListDialogAdapter(mContext!!, mapList)
        mSelectMapListDialogAdapter.setOnItemClickListener(object :
            BaseRecyclerAdapter.OnItemClickListener {
            override fun onItemClick(itemView: View?, pos: Int) {
                selectMapNavigation(mapList[pos])
            }
        })

        setOnClickListenerViews(mViewBinding.selectMapNavigationBtn)


//        val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).build()
//        val mGoogleSignInClient: GoogleSignInClient = GoogleSignIn.getClient(this, gso)
//        val mGoogleApiClient = mGoogleSignInClient.asGoogleApiClient()


        ViewUtil.instance.imageLoadUrl(
            mViewBinding.homeMapNavigationPetHead.appHeadImage, pet!!.imgurl
        )
        if (TextUtils.isEmpty(historyDataBean!!.address)) {
            mGeoCoderViewModel.getLatLonAddress(
                mMapDeviceBean!!.latitude, mMapDeviceBean!!.longitude
            )
        } else mViewBinding.homeMapNavigationAddress.text = historyDataBean!!.address

        warningOutFence()
    }

    override fun liveDataObserve() {
        //地理反编译成功返回
        mGeoCoderViewModel.mLatLonAddressLiveData.observe(this) {
            mViewBinding.homeMapNavigationAddress.text = it
        }
    }

    /***
     * 设置距离
     */
    fun setDistanceData(distanceStr: String, minStr: String) {
        mViewBinding.apply {
            mapNavigationDistanceValue.text = distanceStr
            mapNavigationWalkValue.text = minStr
        }
    }

    /**
     * 宠物出围栏警告
     */
    private fun warningOutFence() {
        //没有出围栏
//        if (mMapDeviceBean!!.inFence == ConstantInt.InFence) {
//            return
//        }
        val animation =
            ViewUtil.instance.viewLoadAnimation(mContext, mViewBinding.homeMapNavigationPetLayout)
        animation.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationStart(animation: Animation?) {
                mViewBinding.homeMapNavigationPetLayout.visibility = View.VISIBLE
            }

            override fun onAnimationEnd(animation: Animation?) {
                val rotationObjectAnimator = ObjectAnimator.ofFloat(
                    mViewBinding.homeMapNavigationPetLayout, "rotation", 0f, 1.6f, 0f - 1.6f
                )
                rotationObjectAnimator.duration = 120
                rotationObjectAnimator.interpolator = AccelerateDecelerateInterpolator()
                rotationObjectAnimator.repeatMode = ValueAnimator.REVERSE
                rotationObjectAnimator.repeatCount = ValueAnimator.INFINITE
                rotationObjectAnimator.start()

                val animation2 = ViewUtil.instance.viewLoadAnimation(
                    mContext!!,
                    mViewBinding.homeMapNavigationPetLayout,
                    R.anim.scale_out_center,
                    startOffset = 8000
                )
                animation2.setAnimationListener(object : Animation.AnimationListener {
                    override fun onAnimationStart(animation: Animation?) {
                    }

                    override fun onAnimationEnd(animation: Animation?) {
                        mViewBinding.homeMapNavigationPetLayout.visibility = View.GONE
                    }

                    override fun onAnimationRepeat(animation: Animation?) {
                    }
                })
            }

            override fun onAnimationRepeat(animation: Animation?) {
            }
        })
    }

    /**
     * 选择地图开始跳转
     */
    private fun selectMapNavigation(item: MenuTxtBean) {
        mSelectMapListDialog?.dismiss()
        //自动弹出手机中已经有安装地图的应用
//        val mUri: Uri = Uri.parse("geo:227.15" + ",212.55" + "?q=fhfhfg")
//        val mIntent = Intent(Intent.ACTION_VIEW, mUri)
//        startActivity(mIntent)
        if (AppUtils.isAppInstalled(mContext!!, item.menuValue)) {
            if (null == mMapDeviceBean) {

            } else {
                when (item.menuValue) {
                    //选择高德地图
                    mMapGaoDePackageName -> {
                        val uri = "androidamap://navi?sourceApplication=" + getString(
                            R.string.app_name
                        ) + "&lat=${mMapDeviceBean!!.latitude}&lon=${mMapDeviceBean!!.longitude}&dev=0"
                        val intent = Intent.parseUri(uri, 0)
                        startActivity(intent)
                    }
                    //谷歌地图
                    mMapGooglePackageName -> {
//                        val latitude = 22.56025
//                        val longitude = 113.954032
                        val uri =
                            "google.navigation:q=${mMapDeviceBean!!.latitude},${mMapDeviceBean!!.longitude}"
                        val intent = Intent.parseUri(uri, 0)
                        startActivity(intent)
                    }
                }
            }
        } else {
            showToast(getString(R.string.txt_no_install))
        }
    }

    override fun onClick(v: View?) {
        when (v!!) {
            mViewBinding.selectMapNavigationBtn -> {
                if (null == mSelectMapListDialog) {
                    mSelectMapListDialog =
                        SelectMapListDialog(mContext!!, mSelectMapListDialogAdapter)
                }
                mSelectMapListDialog!!.show()
            }
        }
    }
}