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.ui.extensions.setTextFormat
import net.pokeranalytics.android.ui.fragment.GraphFragment
import net.pokeranalytics.android.util.TextFormat

interface LegendContent

data class DefaultLegendValues(
	var title: String,
	var leftFormat: TextFormat,
	var rightFormat: TextFormat? = null,
	var leftName: String? = null
) : LegendContent

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

//	open class Values(var titleResId: String, var leftFormat: TextFormat, var rightFormat: TextFormat? = null)
//	class MultiLineValues(
//		var firstTitle: String,
//		var secondTitle: String,
//		leftFormat: TextFormat,
//		rightFormat: TextFormat? = null
//	) : Values("", leftFormat, rightFormat)

	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()
	}

	protected open fun getResourceLayout(): Int {
		return R.layout.layout_legend_default
	}

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

	/**
	 * Set the stat data to the view
	 */
	open fun prepareWithStat(stat: Stat, counter: Int? = null, style: GraphFragment.Style) {

		when (style) {
			GraphFragment.Style.BAR -> {
				this.stat1Name.text = stat.localizedTitle(context)
				this.stat2Name.text = context.getString(R.string.sessions)
				this.counter.isVisible = false
			}
			GraphFragment.Style.LINE -> {
				if (stat.graphSignificantIndividualValue) {
					this.stat1Name.text = stat.localizedTitle(context)
					this.stat2Name.text = stat.cumulativeLabelResId(context)
				} else {
					this.stat1Name.text = stat.cumulativeLabelResId(context)
					this.stat2Name.isVisible = false
				}

				counter?.let {
					val counterText = "$it ${context.getString(R.string.points)}"
					this.counter.text = counterText
					this.counter.isVisible = stat.graphShouldShowNumberOfSessions
				}
			}
			else -> {
			}
		}

	}

	/**
	 *
	 */
	open fun setItemData(content: LegendContent, color: Int?) {

		if (content is DefaultLegendValues) {

			this.title.text = content.title.capitalize()

			this.stat1Value.setTextFormat(content.leftFormat, context)
			content.rightFormat?.let {
				this.stat2Value.setTextFormat(it, context)
			}
			content.leftName?.let { name ->
				this.stat1Name.text = name
			}

		}


	}

}