package com.base.locationsharewhite.ui.locationmap

import android.graphics.Color
import android.view.LayoutInflater
import androidx.activity.addCallback
import com.base.locationsharewhite.R
import com.base.locationsharewhite.databinding.ActivityLocationMapBinding
import com.base.locationsharewhite.helper.BaseActivity
import com.base.locationsharewhite.map.MapUtils.addLocationMarker
import com.base.locationsharewhite.map.MapUtils.cameraMovePointsBounds
import com.base.locationsharewhite.map.MapUtils.getLastKnowLatLng
import com.base.locationsharewhite.utils.BarUtils
import com.base.locationsharewhite.utils.LogEx
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.PolylineOptions

class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapReadyCallback {

    private val TAG = "LocationMapActivity"
    private var map: GoogleMap? = null

    override val binding: ActivityLocationMapBinding by lazy {
        ActivityLocationMapBinding.inflate(layoutInflater)
    }


    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
//        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())


        val mapFragment =
            supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
        mapFragment?.getMapAsync(this)
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            finishToMain()
        }
        binding.ivFanhui.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
    }

    override fun onMapReady(googleMap: GoogleMap) {
        map = googleMap


        val sydney = LatLng(-33.87365, 151.20689)
//        showMeAndFriendLocation(sydney)

        val chunxilu = LatLng(30.655782251943407, 104.07701072527895)
        showMeAndFriendLocation(chunxilu)
    }


    private fun showMeAndFriendLocation(friendLatLng: LatLng) {
        val myPair: LatLng = getLastKnowLatLng()
        LogEx.logDebug(TAG, "getLastKnowLocation ${myPair.latitude} ${myPair.longitude}")

        val myAvatar = LayoutInflater.from(this).inflate(R.layout.avatar_me, null)
        val friendAvatar = LayoutInflater.from(this).inflate(R.layout.avatar_friend, null)

        map?.addLocationMarker(myAvatar, myPair, "my")
        map?.addLocationMarker(friendAvatar, friendLatLng, "friend")

        val padding = resources.getDimensionPixelOffset(R.dimen.dp_150)
        map?.cameraMovePointsBounds(myPair, friendLatLng, padding)

        val list = arrayListOf<LatLng>()
        list.addAll(friendTestLine)
        list.add(friendLatLng)
        addFriendPolyline(list)
    }


    val friendTestLine = listOf(
        LatLng(30.65988197028939, 104.07775588286886),//總府皇冠假日酒店
        LatLng(30.658174607262318, 104.06581096754789),//天府广场
        LatLng(30.6531557462031, 104.06660595172475),//航天科技大廈
        LatLng(30.64867742925485, 104.06548317653692),//錦江賓館
        LatLng(30.654908885860806, 104.08053620625755),//ifs
    )

    fun addFriendPolyline(list: List<LatLng>) {
        map?.addPolyline(PolylineOptions().apply {
            list.forEach { add(it) }
            width(5.toFloat())
            color(Color.BLUE)
            geodesic(true)
            clickable(true)
        })
    }

}