package net.pokeranalytics.android.ui.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import kotlinx.android.synthetic.main.layout_legend_default.view.*
import net.pokeranalytics.android.R
import net.pokeranalytics.android.calculus.Stat
import net.pokeranalytics.android.calculus.TextFormat
import net.pokeranalytics.android.ui.extensions.setTextFormat

/**
 * Display a row session
 */
class LegendView : FrameLayout {

	private lateinit var legendLayout: ConstraintLayout

	/**
	 * Constructors
	 */
	constructor(context: Context) : super(context) {
		init()
	}

	constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
		init()
	}

	constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
		init()
	}

	/**
	 * Init
	 */
	private fun init() {
		val layoutInflater = LayoutInflater.from(context)
		legendLayout = layoutInflater.inflate(R.layout.layout_legend_default, this, false) as ConstraintLayout
		val layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
		addView(legendLayout, layoutParams)
	}

	/**
	 * Set the stat data to the view
	 */
	fun prepareWithStat(stat: Stat, counter: Int) {

		if (stat.significantIndividualValue) {
			this.stat1Name.text = stat.localizedTitle(context)
			this.stat2Name.text = stat.cumulativeLabelResId(context)
		} else {
			this.stat1Name.text = stat.cumulativeLabelResId(context)
			this.stat2Name.isVisible = false
		}

		this.counter.text = "$counter ${context.getString(R.string.sessions)}"

	}

	/**
	 *
	 */
	fun setItemData(stat: Stat, title: String, statFormat1: TextFormat, statFormat2: TextFormat) {

		this.title.text = title

		if (stat.significantIndividualValue) {
			this.stat1Value.setTextFormat(statFormat1, context)
			this.stat2Value.setTextFormat(statFormat2, context)
		} else {
			this.stat1Value.setTextFormat(statFormat2, context)
		}

	}

}