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.ui.activity.components.PokerAnalyticsActivity
import net.pokeranalytics.android.ui.fragment.FiltersFragment

class FiltersActivity  : PokerAnalyticsActivity() {

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

	private lateinit var fragment: FiltersFragment

	companion object {
		/**
		 * Default constructor
		 */
		fun newInstance(context: Context, filterId: String? = null) {
			val intent = Intent(context, FiltersActivity::class.java)
			filterId?.let {
				intent.putExtra(IntentKey.FILTER_ID.keyName, it)
			}
			context.startActivity(intent)
		}

		/**
		 * Create a new instance for result
		 */
		fun newInstanceForResult(fragment: Fragment, requestCode: Int) {
			val intent = Intent(fragment.requireContext(), FiltersActivity::class.java)
			fragment.startActivityForResult(intent, requestCode)
		}
	}

	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		setContentView(R.layout.activity_filters)
		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)

		fragment = FiltersFragment()
		fragmentTransaction.add(R.id.container, fragment)
		fragmentTransaction.commit()
		fragment.setData(filterId)

	}

}