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.extensions.SessionState
import net.pokeranalytics.android.model.extensions.getState
import net.pokeranalytics.android.model.realm.Session
import net.pokeranalytics.android.util.CurrencyUtils
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 rowHistorySession: 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)
		rowHistorySession = layoutInflater.inflate(R.layout.row_session_view, this, false) as ConstraintLayout
		val layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT)
		addView(rowHistorySession, layoutParams)
	}

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

		// Date
		rowHistorySession.dateDay.text = session.creationDate.getShortDayName()
		rowHistorySession.dateNumber.text = session.creationDate.getDayNumber()

		// Title / Game type

		var title = ""
		if (session.isTournament()) {
			if (session.tournamentEntryFee != null) {
				title += session.tournamentEntryFee?.toCurrency(CurrencyUtils.getCurrency(session.bankroll))
			}
			session.game?.let {
				title += (if (title.isNotEmpty()) " " else "") + session.getGameTitle()
			}

			if (title.isEmpty()) {
				title = context.getString(R.string.tournament).capitalize()
			}

		} else {
			if (session.cgSmallBlind != null && session.cgBigBlind != null) {
				title += session.getBlinds()
			}
			session.game?.let {
				title += (if (title.isNotEmpty()) " " else "") + session.getGameTitle()
			}

			if (title.isEmpty()) {
				title = context.getString(R.string.cash_game).capitalize()
			}
		}

		rowHistorySession.sessionTitle.text = title

		// Duration
//		rowHistorySession.sessionInfoDurationIcon.isVisible = session.timeFrame != null
//		rowHistorySession.sessionInfoDurationValue.isVisible = session.timeFrame != null
//		session.timeFrame?.let {
//			rowHistorySession.sessionInfoDurationValue.text = session.getFormattedDuration()
//		}
		rowHistorySession.sessionInfoDurationValue.text = session.getFormattedDuration()

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

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

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

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

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

			val result = session.result?.net ?: 0.0
			val formattedStat = ComputedStat(Stat.NETRESULT, result, currency = CurrencyUtils.getCurrency(session.bankroll)).format(context)
			rowHistorySession.gameResult.setTextColor(formattedStat.getColor(context))
			rowHistorySession.gameResult.text = formattedStat.text
		}

	}

}