package pxds.czbe.twvj

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Color
import android.os.Bundle
import android.os.SystemClock
import androidx.core.content.ContextCompat
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.components.YAxis
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineData
import com.github.mikephil.charting.data.LineDataSet
import com.github.mikephil.charting.formatter.IFillFormatter
import com.test.basd.supercleanermax.R
import com.test.basd.supercleanermax.databinding.EwzcbgBinding
import pxds.etmo.Jmut
import pxds.lmmd.zmwf.Khko.setTrackedOnClickListener
import pxds.lmmd.zmwf.Khko.string
import pxds.lmmd.zmwf.Wecj
import java.util.Calendar
import java.util.Date

class Ntwq : Wecj<EwzcbgBinding>() {

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

    private lateinit var batteryReceiver: BatteryReceiver

    override fun onPause() {
        super.onPause()
        requireContext().unregisterReceiver(batteryReceiver)
    }

    fun testChart() {
        binding.trfmaea.setViewPortOffsets(0f, 0f, 0f, 0f)
        binding.trfmaea.setBackgroundColor(Color.parseColor(R.string.ޥޜޥޤޜ_D.string()))
        binding.trfmaea.description.isEnabled = false
        binding.trfmaea.setDrawGridBackground(false)

        val x: XAxis = binding.trfmaea.xAxis
        x.setDrawGridLines(false)
        x.isEnabled = false

        val y: YAxis = binding.trfmaea.axisLeft
        y.setLabelCount(6, false)
        y.textColor = Color.TRANSPARENT
        y.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART)
        y.setDrawGridLines(false)
        y.axisLineColor = Color.TRANSPARENT
        binding.trfmaea.axisRight.isEnabled = false

        val values = ArrayList<Entry>()

        for (i in 0 until 10) {
            val value = (Math.random() * (50 + 1)).toFloat() + 20
            values.add(Entry(i.toFloat(), value))

            var set1: LineDataSet
            if (binding.trfmaea.data != null &&
                binding.trfmaea.data.getDataSetCount() > 0
            ) {
                set1 = binding.trfmaea.data.getDataSetByIndex(0) as LineDataSet
                set1.setValues(values)
                binding.trfmaea.data.notifyDataChanged()
                binding.trfmaea.notifyDataSetChanged()
            } else {
                set1 = LineDataSet(values, "")
                set1.mode = LineDataSet.Mode.CUBIC_BEZIER
                set1.setCubicIntensity(0.2f)
                set1.setDrawCircles(false)
                set1.setLineWidth(1.8f)
                set1.circleRadius = 4f
                set1.setDrawFilled(true)
                set1.fillDrawable = ContextCompat.getDrawable(requireContext(), R.drawable.mwegiroy)

                set1.fillAlpha = 100
                set1.setDrawHorizontalHighlightIndicator(false)
                set1.fillFormatter = IFillFormatter { _, _ -> binding.trfmaea.axisLeft.axisMinimum }

                val data = LineData(set1)
                data.setValueTextSize(9f)
                data.setDrawValues(true)

                binding.trfmaea.setData(data)
            }
        }
    }

    override fun onResume() {
        super.onResume()
        val filter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
        requireContext().registerReceiver(batteryReceiver, filter)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        batteryReceiver = BatteryReceiver()
    }

    private inner class BatteryReceiver : BroadcastReceiver() {

        override fun onReceive(context: Context?, intent: Intent?) {
            val current = intent?.extras?.getInt(R.string.ޥޝގޥށ_D.string()) ?: 0
            val total = intent?.extras?.getInt(R.string.ޚގތހޢ_D.string()) ?: 0
            val percent = current * 100 / total
            binding.gxuourp.text = R.string.ދޙޅޤޝ_D.string((percent).toString())
            when {
                percent >= 80 -> {
                    binding.lvpkqxi.setImageResource(R.mipmap.tlzgidko)
                }

                percent > 50 -> {
                    binding.lvpkqxi.setImageResource(R.mipmap.pdgblxeb)
                }

                percent >= 30 -> {
                    binding.lvpkqxi.setImageResource(R.mipmap.fousdrtp)
                }
            }

            val uptime = SystemClock.elapsedRealtime()
            val currantTime = Calendar.getInstance().time
            val batteryUseTime = currantTime.time - uptime
            val batteryDate = Date(batteryUseTime)
            binding.smokwld.text = R.string.ޠޔވޑޜ_D.string((batteryDate.hours).toString(), (batteryDate.minutes).toString())
        }
    }

    override fun setView() {
        binding.bjlzzkz.setTrackedOnClickListener {
            requireContext().startActivity(Intent(requireContext(), Jmut::class.java))
        }

    }
}