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.row_session_view.view.*
import net.pokeranalytics.android.R
import net.pokeranalytics.android.calculus.ComputedStat
import net.pokeranalytics.android.calculus.Stat
import net.pokeranalytics.android.model.TableSize
import net.pokeranalytics.android.model.TournamentType
import net.pokeranalytics.android.model.extensions.SessionState
import net.pokeranalytics.android.model.extensions.getState
import net.pokeranalytics.android.model.realm.Session
import net.pokeranalytics.android.ui.extensions.setTextFormat
import net.pokeranalytics.android.util.extensions.getDayNumber
import net.pokeranalytics.android.util.extensions.getShortDayName
import net.pokeranalytics.android.util.extensions.shortTime
import net.pokeranalytics.android.util.extensions.toCurrency

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

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

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

		val date = session.startDate ?: session.creationDate

		// Date
		rowSession.dateDay.text = date.getShortDayName()
		rowSession.dateNumber.text = date.getDayNumber()

		// Title / Game type

		var parameters = mutableListOf<String>()
		if (session.isTournament()) {

			session.tournamentEntryFee?.let {
				parameters.add(it.toCurrency(session.currency))
			}

			session.tournamentName?.let {
				parameters.add(it.name)
			} ?: run {
				parameters.add(session.getFormattedGame())
				session.tournamentType?.let { type ->
					parameters.add(TournamentType.values()[type].localizedTitle(context))
				}
			}

			if (parameters.size == 0) {
				parameters.add(context.getString(R.string.tournament).capitalize())
			}
		} else {
			if (session.cgSmallBlind != null && session.cgBigBlind != null) {
				parameters.add(session.getFormattedBlinds())
			}
			session.game?.let {
				parameters.add(session.getFormattedGame())
			}

			if (parameters.size == 0) {
				parameters.add(context.getString(R.string.cash_game).capitalize())
			}
		}
		val title = parameters.joinToString(separator = " ")

		rowSession.sessionTitle.text = title

		// Duration
		rowSession.sessionInfoDurationValue.text = session.getFormattedDuration()

		// Location
		rowSession.sessionInfoLocationIcon.isVisible = session.location != null
		rowSession.sessionInfoLocationValue.isVisible = session.location != null
		session.location?.let {
			rowSession.sessionInfoLocationValue.text = it.name
		}

		// Table size
		rowSession.sessionInfoTableIcon.isVisible = session.tableSize != null
		rowSession.sessionInfoTableValue.isVisible = session.tableSize != null
		session.tableSize?.let {
			rowSession.sessionInfoTableValue.text = TableSize(it).localizedTitle(context)
		}

		val state = session.getState()
		rowSession.sessionInfoDurationIcon.isVisible = state.hasStarted
		rowSession.sessionInfoDurationValue.isVisible = state.hasStarted

		// State
		if (state == SessionState.STARTED) {
			rowSession.gameResult.isVisible = false
			rowSession.infoIcon.isVisible = true
			rowSession.infoIcon.setImageResource(R.drawable.chip)
			rowSession.infoTitle.isVisible = true
			rowSession.infoTitle.text = context.getString(R.string.running_session_state)
		} else if (state == SessionState.PLANNED) {
			rowSession.gameResult.isVisible = false
			rowSession.infoIcon.isVisible = true
			rowSession.infoIcon.setImageResource(R.drawable.ic_planned)
			rowSession.infoTitle.isVisible = true
			rowSession.infoTitle.text = session.startDate!!.shortTime()
		} else if (state == SessionState.PENDING) {
			rowSession.gameResult.isVisible = false
			rowSession.infoIcon.isVisible = false
			rowSession.infoTitle.isVisible = false

		} else {
			rowSession.gameResult.isVisible = true
			rowSession.infoIcon.isVisible = false
			rowSession.infoTitle.isVisible = false

			val result = session.result?.net ?: 0.0
			val formattedStat = ComputedStat(Stat.NET_RESULT, result, currency = session.currency).format()
			rowSession.gameResult.setTextFormat(formattedStat, context)
		}

	}

}