package com.sinognss.gnss.demo

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import android.os.Build
import android.os.Bundle
import android.os.Looper
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.sinognss.gnss.demo.databinding.ActivityGnssLocationBinding

/**
 * @author: zhangchao
 * @date: 2026/7/1
 * @description:
 */
class MockLocationActivity : AppCompatActivity() {

    private lateinit var locationManager: LocationManager


    lateinit var binding: ActivityGnssLocationBinding

    private val locationListener = object : LocationListener {

        override fun onLocationChanged(location: Location) {
            // 高精度大地坐标
            val lat = location.latitude
            val lon = location.longitude
            val alt = location.altitude

            // 精度信息
            val horizontalAcc = location.accuracy

            val verticalAcc = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                location.verticalAccuracyMeters
            } else {
                null
            }

            // 速度与方向
            val speed = location.speed
            val bearing = location.bearing

            // 时间戳
            val time = location.time

            val locationTxt = "lat=$lat, lon=$lon, alt=$alt, hAcc=$horizontalAcc, vAcc=$verticalAcc, speed=$speed, bearing=$bearing, time=$time"
            Log.d(
                "GNSS_MOCK",
                locationTxt
            )

            runOnUiThread {
                binding.tvLocation.setText(locationTxt)
            }

            location.extras?.let {
                parseNmea(it)
            }
        }

        override fun onProviderEnabled(provider: String) {
            Log.d("GNSS_MOCK", "Provider enabled: $provider")
        }

        override fun onProviderDisabled(provider: String) {
            Log.w("GNSS_MOCK", "Provider disabled: $provider")
        }
    }

    private fun parseNmea(bundle: Bundle) {

        val gga = bundle.getString("gpgga")
        val rmc = bundle.getString("gnrmc")
        val vtg = bundle.getString("gnvtg")
        val gsa = bundle.getString("gngsa")
        val gsv = bundle.getString("gngsv")
        val gll = bundle.getString("gngll")
        val gst = bundle.getString("gngst")

        gga?.let {
            Log.d("GNSS_NMEA_GGA", it)
            // 可根据 GGA 判断固定解状态、卫星数、高程等
        }

        gst?.let {
            Log.d("GNSS_NMEA_GST", it)
            // 可根据 GST 判断定位误差统计信息
        }

        rmc?.let {
            Log.d("GNSS_NMEA_RMC", it)
            // 可根据 RMC 读取速度、航向、UTC 时间等
        }

        runOnUiThread {
            binding.tvNmea.setText("$gga\n$rmc\n$vtg\n$gsa\n$gsv\n$gll\n$gst")
        }
    }

    private val nmeaLocationListener = object : LocationListener {

        override fun onLocationChanged(location: Location) {

        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityGnssLocationBinding.inflate(layoutInflater)
        setContentView(binding.root)

        locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager

        requestMockLocation()

    }

    private fun requestMockLocation() {
        if (
            ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_FINE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
                1001
            )
            return
        }

        locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER,
            100L,
            0f,
            locationListener,
            Looper.getMainLooper()
        )
    }

    override fun onDestroy() {
        super.onDestroy()
        locationManager.removeUpdates(locationListener)
    }
}
