package net.pokeranalytics.android.ui.graph

import android.content.Context
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import com.github.mikephil.charting.charts.BarChart
import com.github.mikephil.charting.charts.BarLineChartBase
import com.github.mikephil.charting.components.XAxis
import net.pokeranalytics.android.R
import net.pokeranalytics.android.ui.extensions.px

enum class AxisFormatting {
	DEFAULT,
	X_DURATION,
	Y_DURATION,
}

fun BarLineChartBase<*>.setStyle(
	small: Boolean,
	axisFormatting: AxisFormatting = AxisFormatting.DEFAULT,
	context: Context
) {

	this.legend.isEnabled = false
	this.description.isEnabled = false

	// X Axis
	this.xAxis.axisLineColor = ContextCompat.getColor(context, R.color.chart_default)
	this.xAxis.enableGridDashedLine(3.0f.px, 5.0f.px, 1.0f.px)
	this.xAxis.position = XAxis.XAxisPosition.BOTTOM
	this.xAxis.setDrawGridLines(true)
	this.xAxis.isGranularityEnabled = true
	this.xAxis.granularity = 1.0f
	this.xAxis.textColor = ContextCompat.getColor(context, R.color.chart_default)
	this.xAxis.typeface = ResourcesCompat.getFont(context, R.font.roboto_medium)
	this.xAxis.labelCount = 4
	this.xAxis.textSize = 12f
	this.xAxis.isEnabled = true

	when (this) {
		is BarChart -> {
			this.xAxis.setDrawLabels(false)
		}
		else -> {
			this.xAxis.setDrawLabels(true)
		}
	}

	// Y Axis
	this.axisLeft.setDrawAxisLine(false)
	this.axisLeft.setDrawGridLines(true)
	this.axisLeft.enableGridDashedLine(3.0f.px, 5.0f.px, 1.0f.px)

	this.axisLeft.setDrawZeroLine(true)
	this.axisLeft.zeroLineColor = ContextCompat.getColor(context, R.color.chart_default)

	this.axisLeft.isGranularityEnabled = true
	this.axisLeft.granularity = 1.0f

	this.axisLeft.textColor = ContextCompat.getColor(context, R.color.chart_default)
	this.axisLeft.typeface = ResourcesCompat.getFont(context, R.font.roboto_medium)
	this.axisLeft.labelCount =
		if (small) 1 else 7 // @todo not great if interval is [0..2] for number of records as we get decimals
	this.axisLeft.textSize = 12f
	this.axisLeft.valueFormatter = LargeNumberFormatter()

	this.axisRight.isEnabled = false

	this.data?.isHighlightEnabled = !small

	when (axisFormatting) {
		AxisFormatting.DEFAULT -> {
			this.axisLeft.valueFormatter = LargeNumberFormatter()
		}
		AxisFormatting.X_DURATION -> {
			this.xAxis.valueFormatter = HourFormatter()
		}
		AxisFormatting.Y_DURATION -> {
			this.axisLeft.valueFormatter = HourFormatter()
		}
	}

}
