package net.pokeranalytics.android.ui.fragment

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.mikephil.charting.data.Entry
import com.google.android.material.tabs.TabLayout
import kotlinx.android.synthetic.main.fragment_calendar_details.*
import net.pokeranalytics.android.R
import net.pokeranalytics.android.calculus.Stat
import net.pokeranalytics.android.model.filter.QueryCondition
import net.pokeranalytics.android.ui.activity.CalendarDetailsActivity
import net.pokeranalytics.android.ui.activity.components.PokerAnalyticsActivity
import net.pokeranalytics.android.ui.adapter.RowRepresentableAdapter
import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate
import net.pokeranalytics.android.ui.adapter.StaticRowRepresentableDataSource
import net.pokeranalytics.android.ui.extensions.toast
import net.pokeranalytics.android.ui.fragment.components.PokerAnalyticsFragment
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowViewType
import net.pokeranalytics.android.ui.view.rowrepresentable.CustomizableRowRepresentable
import net.pokeranalytics.android.ui.view.rowrepresentable.GraphRow
import net.pokeranalytics.android.ui.view.rowrepresentable.StatDoubleRow

class CalendarDetailsFragment : PokerAnalyticsFragment(), StaticRowRepresentableDataSource, RowRepresentableDelegate {

	companion object {
		fun newInstance(context: Context) {
			val intent = Intent(context, CalendarDetailsFragment::class.java)
			context.startActivity(intent)
		}
	}

	private lateinit var parentActivity: PokerAnalyticsActivity
	private lateinit var statsAdapter: RowRepresentableAdapter

	private var rowRepresentables: ArrayList<RowRepresentable> = ArrayList()

	//private var stat: Stat = Stat.NETRESULT
	//private var entries: List<Entry> = ArrayList()

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

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

	override fun adapterRows(): List<RowRepresentable>? {
		return rowRepresentables
	}

	/**
	 * 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 = CalendarDetailsActivity.title

		tabs.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
			override fun onTabSelected(tab: TabLayout.Tab) {
				toast("Tab: ${tab.position}")


			}

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

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

		CalendarDetailsActivity.sessionTypeCondition?.let {
			when(it) {
				QueryCondition.CASH -> tabs.getTabAt(1)?.select()
				QueryCondition.TOURNAMENT -> tabs.getTabAt(2)?.select()
				else -> tabs.getTabAt(0)?.select()
			}
		}

		CalendarDetailsActivity.computedResults?.let { computedResults ->

			rowRepresentables.add(CustomizableRowRepresentable(RowViewType.HEADER_TITLE, resId = R.string.net_result))
			rowRepresentables.add(GraphRow(Stat.NETRESULT, null, ""))
			rowRepresentables.add(StatDoubleRow(computedResults.computedStat(Stat.NETRESULT), computedResults.computedStat(Stat.HOURLY_RATE)))
			rowRepresentables.add(StatDoubleRow(computedResults.computedStat(Stat.LOCATIONS_PLAYED), computedResults.computedStat(Stat.LONGEST_STREAKS)))
			rowRepresentables.add(CustomizableRowRepresentable(RowViewType.HEADER_TITLE, resId = R.string.distribution))
			rowRepresentables.add(GraphRow(Stat.NETRESULT, null, ""))
			rowRepresentables.add(StatDoubleRow(computedResults.computedStat(Stat.WIN_RATIO)))
			rowRepresentables.add(CustomizableRowRepresentable(RowViewType.HEADER_TITLE, resId = R.string.volume))
			rowRepresentables.add(GraphRow(Stat.NETRESULT, null, ""))
			rowRepresentables.add(StatDoubleRow(computedResults.computedStat(Stat.DURATION), computedResults.computedStat(Stat.AVERAGE_DURATION)))
			rowRepresentables.add(StatDoubleRow(computedResults.computedStat(Stat.DAYS_PLAYED)))

			//computedResults.group.conditions

		}

		statsAdapter = RowRepresentableAdapter(this, this)



		val viewManager = LinearLayoutManager(requireContext())

		recyclerView.apply {
			setHasFixedSize(true)
			layoutManager = viewManager
			adapter = statsAdapter
		}

		/*
		toolbar.title = stat.localizedTitle(requireContext())

		val fragmentManager = parentActivity.supportFragmentManager
		val fragmentTransaction = fragmentManager.beginTransaction()
		val fragment = GraphFragment()

		fragmentTransaction.add(R.id.container, fragment)
		fragmentTransaction.commit()

		StatisticDetailsActivity.parameters?.let {
			fragment.setData(it.stat, it.report)
			StatisticDetailsActivity.parameters = null
		} ?: run {
			throw Exception("Missing graph parameters")
		}
		*/
	}

	/**
	 * Set data
	 */
	fun setData(stat: Stat, entries: List<Entry>) {
		//this.stat = stat
		//this.entries = entries
	}

}