package com.test.mydemo3.activity

import android.content.ContentResolver
import android.content.Context
import android.content.Intent
import android.os.BatteryManager
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.SystemClock
import android.provider.Settings
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.test.mydemo3.R
import com.test.mydemo3.util.BatteryUtil
import com.test.mydemo3.util.PermissionUtil
import com.test.mydemo3.util.SystemServerState
import java.util.Calendar
import java.util.Date

class BatteryActivity : AppCompatActivity() {

    private lateinit var mQuitView: ImageView
    private lateinit var mBatteryCapacity: TextView
    private lateinit var mBatteryUseTime: TextView
    private lateinit var mBatteryTemperature: TextView
    private lateinit var mBatteryVolatile: TextView
    private lateinit var mBatteryPower: TextView
    private lateinit var mHotspotTitle: TextView
    private lateinit var mHotspotHint: TextView
    private lateinit var mHotspotSwitch: TextView
    private lateinit var mBluetoothTitle: TextView
    private lateinit var mBluetoothHint: TextView
    private lateinit var mBluetoothSwitch: TextView
    private lateinit var mAutoBrightTitle: TextView
    private lateinit var mAutoBrightHint: TextView
    private lateinit var mAutoBrightSwitch: TextView
    private lateinit var mGpsTitle: TextView
    private lateinit var mGpsHint: TextView
    private lateinit var mGpsSwitch: TextView
    private lateinit var mAnimation: View

    private var isHotspotOpen = false
    private var isBluetoothOpen = false
    private var isAutoBrightOpen = false
    private var isGpsOpen = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_battery)
        BatteryUtil.ReceiverBatteryOhterInfo(this)
        PermissionUtil.requestLocationPermission(this)
        initView()
        setOnClickListener()

    }

    private fun setOnClickListener() {
        mQuitView.setOnClickListener {
            finish()
        }
        mHotspotSwitch.setOnClickListener {


//            isHotspotOpen = !isHotspotOpen
//            setViewSwitch(mHotspotSwitch, isHotspotOpen)
            startActivity(Intent(Settings.ACTION_DATA_ROAMING_SETTINGS))
        }
        mBluetoothSwitch.setOnClickListener {
//            isBluetoothOpen = !isBluetoothOpen
//            setViewSwitch(mBluetoothSwitch, isBluetoothOpen)
            startActivity(Intent(Settings.ACTION_BLUETOOTH_SETTINGS))
        }
        mAutoBrightSwitch.setOnClickListener {
//            isAutoBrightOpen = !isAutoBrightOpen
//            setViewSwitch(mAutoBrightSwitch, isAutoBrightOpen)
            startActivity(Intent(Settings.ACTION_DISPLAY_SETTINGS))
        }
        mGpsSwitch.setOnClickListener {
//            isGpsOpen = !isGpsOpen
//            setViewSwitch(mGpsSwitch, isGpsOpen)
            startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))

        }
        findViewById<View>(R.id.battery_quit_btn_2).setOnClickListener {
            finish()
        }
    }
    private fun setViewSwitch(textView: TextView, isOpen: Boolean){
        textView.text = if (isOpen) "Off" else "On"
        textView.setTextColor(if (isOpen) getColor(R.color.style_grey)
        else getColor(R.color.style_background))
    }

    private fun getBatteryInfo() {
        val batteryManager = getSystemService(Context.BATTERY_SERVICE) as BatteryManager
        mBatteryCapacity.text = BatteryUtil.level.toString()
//        mBatteryPower.text = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER).toString()

        val uptime = SystemClock.elapsedRealtime() // 获取系统启动时间
        val currantTime = Calendar.getInstance().time
        val batteryUseTime = currantTime.time - uptime
        val batteryDate = Date(batteryUseTime)

        mBatteryUseTime.text = "${batteryDate.day} day, ${batteryDate.hours} hours ${batteryDate.minutes} min "

        val mAh =
            (batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER) / (batteryManager.getIntProperty(
                BatteryManager.BATTERY_PROPERTY_CAPACITY
            ).toFloat() / 100f)) / 1000f
        mBatteryPower.text = mAh.toInt().toString()

        mBatteryTemperature.text = BatteryUtil.temperature.toString()
        mBatteryVolatile.text = BatteryUtil.voltage.toString()

//        mBatteryPower.text = batteryManager.getIntProperty(
//            BatteryManager.BATTERY_PROPERTY_CAPACITY).toString()
    }

    private fun initView() {
        mQuitView = findViewById(R.id.battery_quit_btn)
        mBatteryCapacity = findViewById(R.id.battery_num_text)
        mBatteryUseTime = findViewById(R.id.battery_use_time_text)
        mBatteryTemperature = findViewById(R.id.battery_temperature)
        mBatteryVolatile = findViewById(R.id.battery_voltage_text)
        mBatteryPower = findViewById(R.id.battery_power_text)
        mHotspotTitle = findViewById(R.id.hotspot_title_text)
        mHotspotHint = findViewById(R.id.hotspot_hint_text)
        mHotspotSwitch = findViewById(R.id.hotspot_switch_text)
        mBluetoothTitle = findViewById(R.id.bluetooth_title_text)
        mBluetoothHint = findViewById(R.id.bluetooth_hint_text)
        mBluetoothSwitch = findViewById(R.id.bluetooth_switch_text)
        mAutoBrightTitle = findViewById(R.id.auto_bright_title_text)
        mAutoBrightHint = findViewById(R.id.auto_bright_hint_text)
        mAutoBrightSwitch = findViewById(R.id.auto_bright_switch_text)
        mGpsTitle = findViewById(R.id.gps_title_text)
        mGpsHint = findViewById(R.id.gps_hint_text)
        mGpsSwitch = findViewById(R.id.gps_switch_text)
        mAnimation = findViewById(R.id.battery_animation_view)
    }

    override fun onResume() {
        super.onResume()
        getBatteryInfo()
        Handler(Looper.getMainLooper()).postDelayed(Runnable {
            mAnimation.visibility = View.GONE
        }, 2000)

        setViewSwitch(mBluetoothSwitch, SystemServerState.isBluetoothEnabled())
        setViewSwitch(mHotspotSwitch, SystemServerState.isHotspotEnabled(this))
        setViewSwitch(mAutoBrightSwitch, SystemServerState.isAutoBrightnessEnabled(contentResolver))
        setViewSwitch(mGpsSwitch, SystemServerState.isGPSEnabled(this))

        mHotspotTitle.text = String.format(getString(R.string.turn_off_the_hotspot), if (SystemServerState.isHotspotEnabled(this)) "On" else "Off")
        mBluetoothTitle.text = String.format(getString(R.string.turn_off_bluetooth), if (SystemServerState.isBluetoothEnabled()) "On" else "Off")
        mAutoBrightTitle.text = String.format(getString(R.string.turn_on_auto_brightness), if (SystemServerState.isAutoBrightnessEnabled(contentResolver)) "On" else "Off")
        mGpsTitle.text = String.format(getString(R.string.turn_off_gps), if (SystemServerState.isGPSEnabled(this)) "On" else "Off")

    }
}