package com.base.locationsharewhite.ui.main

import android.Manifest
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.view.LayoutInflater
import androidx.activity.addCallback
import androidx.core.view.updatePadding
import com.base.locationsharewhite.R
import com.base.locationsharewhite.databinding.ActivityMainBinding
import com.base.locationsharewhite.helper.BaseActivity
import com.base.locationsharewhite.helper.MyApplication
import com.base.locationsharewhite.location.LocationLoginUtils
import com.base.locationsharewhite.map.MapUtils.addLocationMarker
import com.base.locationsharewhite.map.MapUtils.getLastKnowLatLng
import com.base.locationsharewhite.ui.howuse.HowUseActivity
import com.base.locationsharewhite.ui.locationmap.LocationMapActivity
import com.base.locationsharewhite.ui.set.SettingActivity
import com.base.locationsharewhite.ui.views.DialogView.showAppExitDialog
import com.base.locationsharewhite.utils.BarUtils
import com.base.locationsharewhite.utils.LogEx
import com.base.locationsharewhite.utils.PermissionUtils.checkLocationPermission
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment

class MainActivity : BaseActivity<ActivityMainBinding>(), OnMapReadyCallback {

    private val TAG = "MainActivity"

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

    private var map: GoogleMap? = null


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

        if (!checkLocationPermission()) {
            launcher.launch(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION)) {

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    launcher.launch(arrayOf(Manifest.permission.ACCESS_BACKGROUND_LOCATION)) {

                    }
                }

            }
        }
        //访问运动数据
        //https://developer.android.com/develop/sensors-and-location/location/transitions?hl=zh-cn
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            launcher.launch(arrayOf(Manifest.permission.ACTIVITY_RECOGNITION)) {

            }
        }

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

        LocationLoginUtils.login("wanglei")
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            showAppExitDialog()
        }
        binding.llMapLocation.setOnClickListener {
            startActivity(Intent(this, LocationMapActivity::class.java))
        }
        binding.ivFangda.setOnClickListener {
            startActivity(Intent(this, LocationMapActivity::class.java))
        }
        binding.llMap.setOnClickListener {
            startActivity(Intent(this, LocationMapActivity::class.java))
        }
        binding.flMyLocationCode.setOnClickListener {
            startActivity(Intent(this, LocationCodeActivity::class.java))
        }
        binding.flShareMyLocation.setOnClickListener {
            startActivity(Intent(this, LocationShareActivity::class.java))
        }
        binding.flHowUse.setOnClickListener {
            startActivity(Intent(this, HowUseActivity::class.java))
        }
        binding.flSettings.setOnClickListener {
            startActivity(Intent(this, SettingActivity::class.java))
        }

    }

    override fun onResume() {
        super.onResume()
        val isRecreate = updateAppLanguage(MyApplication.mainLanguage, TAG) {
            MyApplication.mainLanguage = it
        }
        if (isRecreate) {
            return
        }
        LogEx.logDebug(TAG, "onResume isRecreate=$isRecreate")
    }

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

        val latLng = getLastKnowLatLng()
        LogEx.logDebug(TAG, "getLastKnowLocation ${latLng.latitude} ${latLng.longitude}")

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

        map?.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15f))
        map?.addLocationMarker(myAvatar, latLng, "my")
    }
}