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

class EditableDataActivity : PokerAnalyticsActivity() {

	enum class IntentKey(val keyName: String) {
		DATA_TYPE("DATA_TYPE"),
		PRIMARY_KEY("PRIMARY_KEY");
	}

	companion object {
		/**
		 * Default constructor
		 */
		fun newInstance(context: Context, dataType: Int, primaryKey: String? = null) {
			val intent = Intent(context, EditableDataActivity::class.java)
			intent.putExtra(IntentKey.DATA_TYPE.keyName, dataType)
			primaryKey?.let {
				intent.putExtra(IntentKey.PRIMARY_KEY.keyName, it)
			}
			context.startActivity(intent)
		}

		/**
		 * Create a new instance for result
		 */
		fun newInstanceForResult(fragment: Fragment, dataType: LiveData, primaryKey: String? = null, requestCode: Int) {
			val intent = Intent(fragment.requireContext(), EditableDataActivity::class.java)
			intent.putExtra(IntentKey.DATA_TYPE.keyName, dataType.ordinal)
			primaryKey?.let {
				intent.putExtra(IntentKey.PRIMARY_KEY.keyName, it)
			}
			fragment.startActivityForResult(intent, requestCode)
		}

	}

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

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

		val dataType = intent.getIntExtra(IntentKey.DATA_TYPE.keyName, 0)
		val primaryKey = intent.getStringExtra(IntentKey.PRIMARY_KEY.keyName)

		val fragmentManager = supportFragmentManager
		val fragmentTransaction = fragmentManager.beginTransaction()
		val fragment: EditableDataFragment = when (dataType) {
			LiveData.BANKROLL.ordinal -> BankrollDataFragment()
			LiveData.LOCATION.ordinal -> LocationDataFragment()
			LiveData.TRANSACTION.ordinal -> TransactionDataFragment()
			LiveData.CUSTOM_FIELD.ordinal -> CustomFieldDataFragment()
			LiveData.TRANSACTION_TYPE.ordinal -> TransactionTypeDataFragment()
			else -> EditableDataFragment()
		}

		fragmentTransaction.add(R.id.container, fragment)
		fragmentTransaction.commit()
		fragment.setData(dataType, primaryKey)
	}

}