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.calculus.Report
import net.pokeranalytics.android.calculus.Stat
import net.pokeranalytics.android.ui.activity.components.ReportActivity
import net.pokeranalytics.android.ui.activity.components.ReportParameters
import net.pokeranalytics.android.ui.activity.components.RequestCode
import net.pokeranalytics.android.ui.fragment.report.ProgressReportFragment

class ProgressReportActivity : ReportActivity() {

	companion object {

		// Unparcel fails when setting a custom Parcelable object on Entry so we use a static reference to passe objects

		/**
		 * Default constructor
		 */
		fun newInstance(context: Context, report: Report, title: String, stat: Stat? = null, displayAggregationChoices: Boolean = true) {
			parameters = ReportParameters(report, title, stat, showAggregationChoices = displayAggregationChoices)
			val intent = Intent(context, ProgressReportActivity::class.java)
			context.startActivity(intent)
		}

		fun newInstanceForResult(fragment: Fragment, report: Report, title: String, stat: Stat? = null, displayAggregationChoices: Boolean = true) {
			parameters = ReportParameters(report, title, stat, showAggregationChoices = displayAggregationChoices)
			val intent = Intent(fragment.context, ProgressReportActivity::class.java)
			fragment.startActivityForResult(intent, RequestCode.DEFAULT.value)
		}

	}

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

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

		val fragmentTransaction = supportFragmentManager.beginTransaction()
		val statisticDetailsFragment = ProgressReportFragment()
		fragmentTransaction.add(R.id.statisticDetailsContainer, statisticDetailsFragment)
		fragmentTransaction.commit()

		parameters?.let {
			val report = it.report
			val stat = it.stat ?: report.options.stats.first()
			statisticDetailsFragment.setData(report, stat, it.showAggregationChoices, it.title)
			parameters = null
		}

	}

}
