package net.pokeranalytics.android.ui.activity

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import kotlinx.android.synthetic.main.activity_session.*
import net.pokeranalytics.android.R
import net.pokeranalytics.android.ui.activity.components.PokerAnalyticsActivity
import net.pokeranalytics.android.ui.fragment.SessionFragment

class SessionActivity: PokerAnalyticsActivity() {

	enum class IntentKey(val keyName : String) {
		IS_TOURNAMENT("IS_TOURNAMENT"),
		SESSION_ID("SESSION_ID");
	}

	companion object {

		fun newInstance(context: Context, isTournament: Boolean? = false, sessionId: String? = "") {
			val intent = Intent(context, SessionActivity::class.java)
			isTournament?.let {
				intent.putExtra(IntentKey.IS_TOURNAMENT.keyName, isTournament)
			}
			sessionId?.let {
				intent.putExtra(IntentKey.SESSION_ID.keyName, sessionId)
			}
			context.startActivity(intent)
		}

		fun newInstanceforResult(fragment: Fragment, isTournament: Boolean? = false, sessionId: String? = "", requestCode: Int) {
			val intent = Intent(fragment.requireContext(), SessionActivity::class.java)
			isTournament?.let {
				intent.putExtra(IntentKey.IS_TOURNAMENT.keyName, isTournament)
			}
			sessionId?.let {
				intent.putExtra(IntentKey.SESSION_ID.keyName, sessionId)
			}

			fragment.startActivityForResult(intent, requestCode)
		}
	}

	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		setContentView(R.layout.activity_session)

		initUI()
	}

	override fun onBackPressed() {
		setResult(Activity.RESULT_OK)
		super.onBackPressed()
		(sessionFragment as SessionFragment).onBackPressed()
	}

	/**
	 * Init UI
	 */
	private fun initUI() {
		val sessionId = intent.getStringExtra(IntentKey.SESSION_ID.keyName)
		val isTournament = intent.getBooleanExtra(IntentKey.IS_TOURNAMENT.keyName, false)
		val fragment = sessionFragment as SessionFragment
		fragment.setData(isTournament, sessionId)
	}

}