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

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import kotlinx.android.synthetic.main.fragment_loader.*
import net.pokeranalytics.android.R


class LoaderDialogFragment: DialogFragment() {

	companion object {

		const val ARGUMENT_MESSAGE_RES_ID = "ARGUMENT_MESSAGE_RES_ID"

		/**
		 * Create new instance
		 */
		fun newInstance(resId: Int? = null, isCancelable: Boolean = false): LoaderDialogFragment {
			val fragment = LoaderDialogFragment()
			fragment.isCancelable = isCancelable
			val bundle = Bundle()
			resId?.let {
				bundle.putInt(ARGUMENT_MESSAGE_RES_ID, resId)
			}
			fragment.arguments = bundle
			return fragment
		}

	}

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

	override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
		super.onViewCreated(view, savedInstanceState)
		arguments?.let {bundle ->
			if (bundle.containsKey(ARGUMENT_MESSAGE_RES_ID)) {
				loadingMessage.text = getString(bundle.getInt(ARGUMENT_MESSAGE_RES_ID))
			}
		}
	}

	override fun onStart() {
		super.onStart()
		val window = dialog.window
		window?.setBackgroundDrawableResource(android.R.color.transparent)
	}

}