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

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import kotlinx.android.synthetic.main.bottom_sheet_grid.*
import kotlinx.android.synthetic.main.fragment_bottom_sheet.view.*
import net.pokeranalytics.android.ui.adapter.TableSizeGridAdapter
import net.pokeranalytics.android.ui.view.GridSpacingItemDecoration
import net.pokeranalytics.android.util.px


class BottomSheetTableSizeGridFragment : BottomSheetFragment() {

	private var dataList: ArrayList<String> = ArrayList()
	private lateinit var dataAdapter: TableSizeGridAdapter
	private var defaultSize: Int? = null

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

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

	override fun getValue(): Any? {
		return defaultSize
	}

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

		val bottomSheetData = getData()
		if (bottomSheetData.isNotEmpty() && bottomSheetData.first().defaultValue != null) {
			defaultSize = bottomSheetData.first().defaultValue as Int?
		}

		dataList.add(getString(net.pokeranalytics.android.R.string.heads_up))
		for (i in 3..10) {
			dataList.add("$i-max")
		}

	}

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

		setAddButtonVisible(false)

		LayoutInflater.from(requireContext())
			.inflate(net.pokeranalytics.android.R.layout.bottom_sheet_grid, view?.bottomSheetContainer, true)

		val viewManager = GridLayoutManager(requireContext(), 3)
		dataAdapter = TableSizeGridAdapter(dataList)
		dataAdapter.onClickOnItem = { position ->
			bottomSheetDelegate.setValue(position + 2, row)
			dismiss()
		}

		val spanCount = 3
		val spacing = 2.px
		val includeEdge = false

		reyclerView.apply {
			setHasFixedSize(true)
			layoutManager = viewManager
			adapter = dataAdapter
			addItemDecoration(GridSpacingItemDecoration(spanCount, spacing, includeEdge))
		}

	}

}