package net.pokeranalytics.android.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import net.pokeranalytics.android.R
import net.pokeranalytics.android.model.realm.Filter
import net.pokeranalytics.android.ui.activity.components.PokerAnalyticsActivity
import net.pokeranalytics.android.ui.fragment.FilterDetailsFragment

class FilterDetailsActivity : PokerAnalyticsActivity() {

	enum class IntentKey(val keyName: String) {
		FILTER_ID("FILTER_ID"),
		FILTER_CATEGORY_ORDINAL("FILTER_CATEGORY_ORDINAL")
	}

	companion object {

		/**
		 * Default constructor
		 */
		fun newInstance(context: Context, filterId: String, filterCategoryOrdinal: Int) {
			val intent = Intent(context, FilterDetailsActivity::class.java)
			intent.putExtra(IntentKey.FILTER_ID.keyName, filterId)
			intent.putExtra(IntentKey.FILTER_CATEGORY_ORDINAL.keyName, filterCategoryOrdinal)
			context.startActivity(intent)
		}

		/**
		 * Create a new instance for result
		 */
		fun newInstanceForResult(fragment: Fragment, filterId: String, filterCategoryOrdinal: Int, requestCode: Int, filter: Filter? = null) {

			val intent = Intent(fragment.requireContext(), FilterDetailsActivity::class.java)
			intent.putExtra(IntentKey.FILTER_ID.keyName, filterId)
			intent.putExtra(IntentKey.FILTER_CATEGORY_ORDINAL.keyName, filterCategoryOrdinal)
			fragment.startActivityForResult(intent, requestCode)
		}
	}

	private lateinit var fragment: FilterDetailsFragment

	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		setContentView(R.layout.activity_filter_details)
		initUI()
	}

	override fun onBackPressed() {
		fragment.onBackPressed()
	}

	/**
	 * Init UI
	 */
	private fun initUI() {

		val fragmentManager = supportFragmentManager
		val fragmentTransaction = fragmentManager.beginTransaction()
		val filterId = intent.getStringExtra(IntentKey.FILTER_ID.keyName)
		val filterCategoryOrdinal = intent.getIntExtra(IntentKey.FILTER_CATEGORY_ORDINAL.keyName, 0)

		fragment = FilterDetailsFragment()
		fragmentTransaction.add(R.id.container, fragment)
		fragmentTransaction.commit()
		fragment.setData(filterId, filterCategoryOrdinal)
	}

}