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 kotlinx.android.synthetic.main.row_transaction_view.view.*
import net.pokeranalytics.android.R
import net.pokeranalytics.android.calculus.ComputedStat
import net.pokeranalytics.android.calculus.Stat
import net.pokeranalytics.android.model.realm.Transaction
import net.pokeranalytics.android.ui.extensions.setTextFormat
import net.pokeranalytics.android.util.extensions.getDayNumber
import net.pokeranalytics.android.util.extensions.getShortDayName

/**
 * Display a transaction row
 */
class TransactionRowView : FrameLayout {

	private lateinit var rowTransaction: 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)
		rowTransaction = layoutInflater.inflate(R.layout.row_transaction_view, this, false) as ConstraintLayout
		val layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT)
		addView(rowTransaction, layoutParams)
	}

	/**
	 * Set the session data to the view
	 */
	fun setData(transaction: Transaction) {

		// Date
		rowTransaction.transactionDateDay.text = transaction.date.getShortDayName()
		rowTransaction.transactionDateNumber.text = transaction.date.getDayNumber()

		// Title / Game type
		var title = transaction.type?.name ?: "" + " " + transaction.comment
		var subtitle = transaction.bankroll?.name

		rowTransaction.transactionTitle.text = title
		rowTransaction.transactionSubtitle.text = subtitle

		// Amount
		val formattedStat = ComputedStat(Stat.NET_RESULT, transaction.amount).format()
		rowTransaction.transactionAmount.setTextFormat(formattedStat, context)

	}

}