package net.pokeranalytics.android.ui.fragment

import android.os.Bundle
import android.view.*
import kotlinx.android.synthetic.main.fragment_comparison_chart.*
import net.pokeranalytics.android.R
import net.pokeranalytics.android.ui.activity.BankrollActivity
import net.pokeranalytics.android.ui.activity.SettingsActivity
import net.pokeranalytics.android.ui.adapter.ComparisonChartPagerAdapter
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.rowrepresentable.MoreTabRow

class ComparisonChartFragment : PokerAnalyticsFragment(), StaticRowRepresentableDataSource, RowRepresentableDelegate {

    companion object {

        /**
         * Create new instance
         */
        fun newInstance(): ComparisonChartFragment {
            val fragment = ComparisonChartFragment()
            val bundle = Bundle()
            fragment.arguments = bundle
            return fragment
        }

	    val rowRepresentation: List<RowRepresentable> by lazy {
		    val rows = ArrayList<RowRepresentable>()
		    rows.addAll(MoreTabRow.values())
		    rows
	    }

    }

	private lateinit var viewPagerAdapter: ComparisonChartPagerAdapter
	private var comparisonChartMenu: Menu? = null


	// Life Cycle

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

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

	override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {
		menu?.clear()
		inflater?.inflate(R.menu.toolbar_comparison_chart, menu)
		this.comparisonChartMenu = menu
		super.onCreateOptionsMenu(menu, inflater)
	}

	override fun onOptionsItemSelected(item: MenuItem?): Boolean {
		when (item!!.itemId) {
			R.id.settings -> openChangeStatistics()
		}
		return true
	}

	// Rows
	override fun adapterRows(): List<RowRepresentable>? {
		return rowRepresentation
	}

	override fun onRowSelected(position: Int, row: RowRepresentable, fromAction: Boolean) {
		super.onRowSelected(position, row, fromAction)
		when(row) {
			MoreTabRow.BANKROLL -> BankrollActivity.newInstance(requireContext())
			MoreTabRow.SETTINGS -> SettingsActivity.newInstance(requireContext())
		}
	}

    // Business

    /**
     * Init data
     */
    private fun initData() {
    }

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

		setDisplayHomeAsUpEnabled(true)
		setToolbarTitle(getString(R.string.comparison_chart))

		parentActivity?.let {
			viewPagerAdapter = ComparisonChartPagerAdapter(requireContext(), it.supportFragmentManager)
			viewPager.adapter = viewPagerAdapter
			viewPager.offscreenPageLimit = 2
			tabs.setupWithViewPager(viewPager)
		}

	}

	/**
	 * Open change statistics
	 */
	private fun openChangeStatistics() {
		//TODO
		toast("Open change statistics")
	}

}