package com.lukouguoji.aerologic.page.car.chart

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.github.mikephil.charting.components.AxisBase
import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.data.BarData
import com.github.mikephil.charting.data.BarDataSet
import com.github.mikephil.charting.data.BarEntry
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.formatter.IAxisValueFormatter
import com.github.mikephil.charting.formatter.IValueFormatter
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet
import com.github.mikephil.charting.utils.ViewPortHandler
import com.lukouguoji.aerologic.R
import com.lukouguoji.aerologic.databinding.ActivityCarChartBinding
import com.lukouguoji.module_base.base.BaseBindingActivity
import com.lukouguoji.module_base.bean.CarBarBean
import com.lukouguoji.module_base.ktx.loge
import dev.utils.app.SizeUtils

class CarChartActivity : BaseBindingActivity<ActivityCarChartBinding, CarChartViewModel>() {

    override fun layoutId(): Int {
        return R.layout.activity_car_chart
    }

    override fun viewModelClass(): Class<CarChartViewModel> {
        return CarChartViewModel::class.java
    }

    override fun initOnCreate(savedInstanceState: Bundle?) {
        setBackArrow("平板车统计")
        binding.viewModel = viewModel

        viewModel.onCreated()

        viewModel.carBarList.observe(this) {
            if (it == null) return@observe
            handleData(it)
        }
    }

    private fun handleData(list: List<CarBarBean>) {
        val titleList =
            (viewModel.locationList.value?.filter { it.value.isNotEmpty() } ?: emptyList())
        // X轴设置
        binding.barChart.xAxis.let {
            it.setDrawGridLines(false)
            it.position = XAxis.XAxisPosition.BOTTOM
            it.valueFormatter =
                IAxisValueFormatter { value, _ -> titleList[value.toInt() % titleList.size].key }
            it.textSize = 14f
            it.yOffset = 10f
        }
        // Y轴设置
        binding.barChart.axisLeft.let {
            it.textSize = 14f
        }

        binding.barChart.description.let {
            it.text = ""
        }
        binding.barChart.legend.let {
            it.textSize = SizeUtils.sp2px(16f).toFloat()
            it.verticalAlignment = Legend.LegendVerticalAlignment.TOP
            it.horizontalAlignment = Legend.LegendHorizontalAlignment.LEFT
        }
        loge("加载图表数据")
        val data = arrayListOf<IBarDataSet>()
        val values = arrayListOf<BarEntry>()

        titleList.mapIndexed { index, bean ->
            BarEntry(
                index.toFloat(),
                (list.find { b -> b.position == bean.value }?.count ?: "0").toFloat()
            )
        }
            .forEach {
                values.add(it)
            }

        val dataSet = BarDataSet(values, "平板车数量")
        dataSet.color = resources!!.getColor(R.color.green)
        dataSet.label
        data.add(dataSet)
        val barData = BarData(data)
        barData.setValueFormatter { value, entry, dataSetIndex, viewPortHandler ->
            value.toInt().toString()
        }
        barData.barWidth = 0.5f
        binding.barChart.data = barData
        barData.notifyDataChanged()
        binding.barChart.notifyDataSetChanged()
        binding.barChart.invalidate()
    }

    companion object {
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, CarChartActivity::class.java)
            context.startActivity(starter)
        }
    }
}