package net.pokeranalytics.android.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.android.material.tabs.TabLayout
import kotlinx.android.synthetic.main.fragment_report_details.*
import kotlinx.android.synthetic.main.fragment_statistic_details.toolbar
import net.pokeranalytics.android.R
import net.pokeranalytics.android.calculus.AggregationType
import net.pokeranalytics.android.calculus.Report
import net.pokeranalytics.android.calculus.Stat
import net.pokeranalytics.android.ui.activity.components.PokerAnalyticsActivity
import net.pokeranalytics.android.ui.adapter.ReportPagerAdapter
import net.pokeranalytics.android.ui.fragment.components.PokerAnalyticsFragment

class ReportDetailsFragment : PokerAnalyticsFragment() {

	companion object {
		fun newInstance(report: Report?, reportTitle: String): ReportDetailsFragment {
			val fragment = ReportDetailsFragment()
			fragment.reportTitle = reportTitle
			report?.let {
				fragment.selectedReport = it
			}
			val bundle = Bundle()
			fragment.arguments = bundle
			return fragment

		}
	}

	private lateinit var parentActivity: PokerAnalyticsActivity
	private lateinit var selectedReport: Report

	private var reports: MutableMap<AggregationType, Report> = hashMapOf()
	private var stat: Stat = Stat.NET_RESULT
	private var displayAggregationChoices: Boolean = true
	private var reportTitle: String = ""

	override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
		return inflater.inflate(R.layout.fragment_report_details, container, false)
	}

	override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
		super.onViewCreated(view, savedInstanceState)
		initUI()
	}

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

		parentActivity = activity as PokerAnalyticsActivity

		// Avoid a bug during setting the title
		toolbar.title = ""

		parentActivity.setSupportActionBar(toolbar)
		parentActivity.supportActionBar?.setDisplayHomeAsUpEnabled(true)
		setHasOptionsMenu(true)

		toolbar.title = reportTitle

		val reportPagerAdapter = ReportPagerAdapter(requireContext(), parentActivity.supportFragmentManager, selectedReport)
		viewPager.adapter = reportPagerAdapter
		viewPager.offscreenPageLimit = 3

		tabs.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
			override fun onTabSelected(tab: TabLayout.Tab) {
				viewPager.setCurrentItem(tab.position, false)
			}

			override fun onTabUnselected(tab: TabLayout.Tab) {
			}

			override fun onTabReselected(tab: TabLayout.Tab) {
			}
		})
	}


	/**
	 * Set data
	 */
	fun setData(report: Report) {
		this.selectedReport = report
	}

}