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

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.fragment.app.FragmentManager
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import kotlinx.android.synthetic.main.fragment_bottom_sheet.*
import net.pokeranalytics.android.R
import net.pokeranalytics.android.ui.view.RowRepresentable

enum class BottomSheetType {
	NONE,
	LIST,
	LIST_GAME,
	DOUBLE_LIST,
	GRID,
	EDIT_TEXT,
	DOUBLE_EDIT_TEXT,
	SUM
}

interface BottomSheetDelegate {
	fun clickOnAdd(row: RowRepresentable)
	fun clickOnClear(row: RowRepresentable)
	fun setValue(value: Any?, row: RowRepresentable)
}


open class BottomSheetFragment : BottomSheetDialogFragment() {

	lateinit var row: RowRepresentable
	lateinit var bottomSheetDelegate: BottomSheetDelegate

	private var data: ArrayList<BottomSheetData> = ArrayList()

	companion object {
		fun create(
			fragmentManager: FragmentManager?,
			row: RowRepresentable,
			bottomSheetDelegate: BottomSheetDelegate,
			data: ArrayList<BottomSheetData>
		): BottomSheetFragment {

			val bottomSheetFragment = when (row.bottomSheetType) {
				BottomSheetType.LIST -> BottomSheetListFragment()
				BottomSheetType.LIST_GAME -> BottomSheetListGameFragment()
				BottomSheetType.GRID -> BottomSheetTableSizeGridFragment()
				BottomSheetType.DOUBLE_LIST -> BottomSheetListGameFragment()
				BottomSheetType.EDIT_TEXT -> BottomSheetEditTextFragment()
				BottomSheetType.DOUBLE_EDIT_TEXT -> BottomSheetDoubleEditTextFragment()
				BottomSheetType.SUM -> BottomSheetSumFragment()
				else -> BottomSheetFragment()
			}

			bottomSheetFragment.show(fragmentManager, "bottomSheet")
			bottomSheetFragment.row = row
			bottomSheetFragment.bottomSheetDelegate = bottomSheetDelegate
			bottomSheetFragment.data = data
			return bottomSheetFragment
		}
	}

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

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

	override fun onActivityCreated(savedInstanceState: Bundle?) {
		super.onActivityCreated(savedInstanceState)

		// To display correctly the keyboard
		dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
	}

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

		row.let {
			bottomSheetToolbar.title = row.localizedTitle(requireContext())
			bottomSheetToolbar.inflateMenu(net.pokeranalytics.android.R.menu.bottom_sheet_menu)
			bottomSheetToolbar.setOnMenuItemClickListener {
				false
			}

			// Menu
			bottomSheetToolbar.menu.findItem(R.id.actionClear).setOnMenuItemClickListener {
				bottomSheetDelegate.clickOnClear(row)
				dismiss()
				true
			}
			bottomSheetToolbar.menu.findItem(R.id.actionAdd).setOnMenuItemClickListener {
				bottomSheetDelegate.clickOnAdd(row)
				true
			}
			bottomSheetToolbar.menu.findItem(R.id.actionCheck).setOnMenuItemClickListener {
				bottomSheetDelegate.setValue(getValue(), row)
				dismiss()
				true
			}
		}
	}

	/**
	 * Return the data list
	 */
	fun getData(): ArrayList<BottomSheetData> {
		return data
	}

	open fun getValue(): Any? {
		return null
	}

	/**
	 * Set clear button visibility
	 */
	fun setClearButtonVisibile(visible: Boolean) {
		bottomSheetToolbar.menu.findItem(R.id.actionClear).isVisible = visible
	}

	/**
	 * Set check button visibility
	 */
	fun setCheckButtonVisibile(visible: Boolean) {
		bottomSheetToolbar.menu.findItem(R.id.actionCheck).isVisible = visible
	}

	/**
	 * Set add button visibility
	 */
	fun setAddButtonVisible(visible: Boolean) {
		bottomSheetToolbar.menu.findItem(R.id.actionAdd).isVisible = visible
	}

}