package com.zxhyis.toolss.ad

import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import java.util.Timer
import java.util.TimerTask
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.time.Duration.Companion.seconds
import kotlin.time.DurationUnit


/**
 * 重量传感器
 */
class GravitySensorManager(val context: Context) {

    private var sensorManager: SensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager
    private var mySensorEventListener: MySensorEventListener? = null

    private var xWave = 0f
    private var yWave = 0f
    private var zWave = 0f

    private val initValue = AtomicBoolean(false)

    companion object {
        private var x: Float = 0f
        private var y: Float = 0f
        private var z: Float = 0f
        var sensorParam: String = "$x:$y:$z"
    }

    init {
        val time = Timer()
        val timerTask = object : TimerTask() {
            override fun run() {

            }
        }
        time.schedule(timerTask, 5.seconds.toLong(DurationUnit.MILLISECONDS), 20.seconds.toLong(DurationUnit.MILLISECONDS))
    }

    inner class MySensorEventListener : SensorEventListener {

        /**
         * 传感器实时测量出来的变化值
         */
        override fun onSensorChanged(event: SensorEvent) {
            if (event.sensor.type == Sensor.TYPE_ACCELEROMETER) {
//                if (!initValue.get()) {
//                    x = event.values[SensorManager.DATA_X]
//                    y = event.values[SensorManager.DATA_Y]
//                    z = event.values[SensorManager.DATA_Z]
//                    initValue.set(true)
//                } else {
//
//                    val changeX = x - event.values[SensorManager.DATA_X]
//                    val changeY = y - event.values[SensorManager.DATA_Y]
//                    val changeZ = z - event.values[SensorManager.DATA_Z]
//
//                    xWave = changeX.coerceAtLeast(xWave)
//                    yWave = changeY.coerceAtLeast(yWave)
//                    zWave = changeZ.coerceAtLeast(zWave)
//
//                }

                x = event.values[SensorManager.DATA_X]
                y = event.values[SensorManager.DATA_Y]
                z = event.values[SensorManager.DATA_Z]
                sensorParam = "$x:$y:$z"
//                Log.e("GravitySensorManager", "$x:$y:$z")
            }
        }

        override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
        }

    }


    /**
     * onResume() 注册
     */
    fun registerListener() {
        if (mySensorEventListener == null) {
            mySensorEventListener = MySensorEventListener()
        }
        val sensorAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
        sensorManager.registerListener(mySensorEventListener, sensorAccelerometer, SensorManager.SENSOR_DELAY_UI)
    }


    /**
     * onPause()注销
     */
    fun unregisterListener() {
        sensorManager.unregisterListener(mySensorEventListener)
    }
}