package net.pokeranalytics.android.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.fragment_more.*
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.RowRepresentableAdapter
import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate
import net.pokeranalytics.android.ui.adapter.StaticRowRepresentableDataSource
import net.pokeranalytics.android.ui.fragment.components.PokerAnalyticsFragment
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.rowrepresentable.MoreTabRow

class MoreFragment : PokerAnalyticsFragment(), StaticRowRepresentableDataSource, RowRepresentableDelegate {

    companion object {

        /**
         * Create new instance
         */
        fun newInstance(): MoreFragment {
            val fragment = MoreFragment()
            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 moreAdapter: RowRepresentableAdapter


	// Life Cycle

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

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

	// 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() {

		moreAdapter = RowRepresentableAdapter(this, this)

		val viewManager = LinearLayoutManager(requireContext())

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

}