package net.pokeranalytics.android.ui.fragment.components.bottomsheet

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import io.realm.RealmResults
import kotlinx.android.synthetic.main.bottom_sheet_list.*
import kotlinx.android.synthetic.main.fragment_bottom_sheet.view.*
import net.pokeranalytics.android.R
import net.pokeranalytics.android.exceptions.RowRepresentableEditDescriptorException
import net.pokeranalytics.android.ui.adapter.LiveRowRepresentableDataSource
import net.pokeranalytics.android.ui.adapter.RowRepresentableAdapter
import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowViewType

open class BottomSheetListFragment : BottomSheetFragment(), LiveRowRepresentableDataSource, RowRepresentableDelegate {

	lateinit var dataAdapter: RowRepresentableAdapter
	var realmData: RealmResults<RowRepresentable>? = null

	override fun adapterRows(): List<RowRepresentable>? {
		return this.realmData
	}

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

	override fun onResume() {
		super.onResume()
		dataAdapter.notifyDataSetChanged()
	}

	override fun rowRepresentableForPosition(position: Int): RowRepresentable? {
		realmData?.let {
			return it[position] as RowRepresentable
		}
		throw IllegalStateException("Need to implement Data Source")
	}

	override fun numberOfRows(): Int {
		realmData?.let {
			return it.size
		}
		throw IllegalStateException("Need to implement Data Source")
	}

	override fun viewTypeForPosition(position: Int): Int {
		return RowViewType.BOTTOM_SHEET_DATA.ordinal
	}

	override fun onRowSelected(position: Int, row: RowRepresentable, fromAction: Boolean) {
		realmData?.let {
			val selectedData = it[position]
			selectedData?.let {data ->
				this.delegate.onRowValueChanged(data, this.row)
				dismiss()
			}
		}
		super.onRowSelected(position, row, fromAction)
	}

	/**
	 * Init data
	 */
	open fun initData() {
		val bottomSheetData = getData()?:throw RowRepresentableEditDescriptorException("RowRepresentableEditDescriptor not found")
		if (bottomSheetData.size != 1) {
			throw RowRepresentableEditDescriptorException("RowRepresentableEditDescriptor inconsistency")
		}
		if (bottomSheetData.first().data == null) {
			throw RowRepresentableEditDescriptorException("RowRepresentableEditDescriptor inconsistency")
		}
		this.realmData = bottomSheetData.first().data as RealmResults<RowRepresentable>
	}

	/**
	 * Init UI
	 */
	open fun initUI() {
		LayoutInflater.from(requireContext()).inflate(R.layout.bottom_sheet_list, view?.bottomSheetContainer, true)

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

		reyclerView.apply {
			setHasFixedSize(true)
			layoutManager = viewManager
			adapter = dataAdapter
		}

	}

}