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_settings.*
import net.pokeranalytics.android.R
import net.pokeranalytics.android.ui.activity.DataListActivity
import net.pokeranalytics.android.ui.adapter.components.*
import net.pokeranalytics.android.ui.fragment.components.PokerAnalyticsFragment
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.SettingRow

class SettingsFragment : PokerAnalyticsFragment(), RowRepresentableDataSource, RowRepresentableDelegate {

	companion object {

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

	private lateinit var settingsAdapterRow: RowRepresentableAdapter


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

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

	override fun adapterRows(): ArrayList<RowRepresentable> {
		val rows = ArrayList<RowRepresentable>()
		rows.addAll(SettingRow.values())
		return rows
	}

	override fun onRowSelected(row: RowRepresentable) {
		DataListActivity.newInstance(requireContext(), (row as SettingRow).ordinal)
	}

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

		val viewManager = LinearLayoutManager(requireContext())
		settingsAdapterRow = RowRepresentableAdapter(
			this, this
		)

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