package net.pokeranalytics.android.ui.fragment.data

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import io.realm.RealmModel
import kotlinx.android.synthetic.main.fragment_editable_data.*
import kotlinx.android.synthetic.main.fragment_editable_data.view.*
import net.pokeranalytics.android.R
import net.pokeranalytics.android.model.LiveData
import net.pokeranalytics.android.model.interfaces.Editable
import net.pokeranalytics.android.ui.adapter.RowRepresentableAdapter
import net.pokeranalytics.android.ui.adapter.RowRepresentableDataSource
import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate
import net.pokeranalytics.android.ui.fragment.components.bottomsheet.BottomSheetFragment
import net.pokeranalytics.android.ui.view.RowRepresentable


open class EditableDataFragment : DataManagerFragment(), RowRepresentableDelegate {

	lateinit var rowRepresentableAdapter: RowRepresentableAdapter

	var shouldOpenKeyboard = true
	var isUpdating = false

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

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

	open fun initData() {

		val dataSource = getDataSource()
		this.rowRepresentableAdapter = RowRepresentableAdapter(getDataSource(), this)
		//this.rowRepresentableAdapter.setHasStableIds(true)
		this.recyclerView.adapter = rowRepresentableAdapter

		// When creating an object, open automatically the keyboard for the first row
		if (!deleteButtonShouldAppear && shouldOpenKeyboard) {
			val row = dataSource.adapterRows()?.firstOrNull()
			row?.let {
				onRowSelected(0, it)
			}
		}
	}

	/**
	 * Set fragment data
	 */
	fun setData(dataType: Int, primaryKey: String?) {
		this.dataType = dataType
		this.liveDataType = LiveData.values()[dataType]
		this.primaryKey = primaryKey
	}

	override fun onRowSelected(position: Int, row: RowRepresentable, fromAction: Boolean) {
		BottomSheetFragment.create(fragmentManager, row, this, getDataSource().editDescriptors(row))
	}

	override fun onRowValueChanged(value: Any?, row: RowRepresentable) {
		this.getRealm().executeTransaction {
			(this.item as Editable).updateValue(value, row)
		}
		rowRepresentableAdapter.refreshRow(row)
	}

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

		setDisplayHomeAsUpEnabled(true)

		val proxyItem: RealmModel? = this.liveDataType.getData(this.getRealm(), primaryKey)
		proxyItem?.let {
			this.appBar.toolbar.title = this.liveDataType.updateEntityLocalizedTitle(requireContext())
			deleteButtonShouldAppear = true
			isUpdating = true
		} ?: run {
			this.appBar.toolbar.title = this.liveDataType.newEntityLocalizedTitle(requireContext())
		}

		val viewManager = LinearLayoutManager(requireContext())
		recyclerView.apply {
			setHasFixedSize(true)
			layoutManager = viewManager
		}
	}

	/**
	 * Return the data source
	 */
	open fun getDataSource(): RowRepresentableDataSource {
		return this.item as RowRepresentableDataSource
	}

}