package net.pokeranalytics.android.ui.fragment.report

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 net.pokeranalytics.android.R
import net.pokeranalytics.android.calculus.Report
import net.pokeranalytics.android.ui.adapter.ReportPagerAdapter

class ComparisonReportFragment : AbstractReportFragment() {

	companion object {

		fun newInstance(report: Report, reportTitle: String): ComparisonReportFragment {
			val fragment = ComparisonReportFragment()
			fragment.reportTitle = reportTitle
			fragment.setReport(report)
			val bundle = Bundle()
			fragment.arguments = bundle
			return fragment
		}
	}

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

	override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
		super.onCreateView(inflater, container, savedInstanceState)
		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() {

		setDisplayHomeAsUpEnabled(true)
		setToolbarTitle(reportTitle)

		parentActivity?.let {
			val reportPagerAdapter = ReportPagerAdapter(requireContext(), it.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) {
			}
		})
	}

}