package net.pokeranalytics.android.ui.activity

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.content.Context
import android.content.Intent
import android.content.pm.ActivityInfo
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.ViewAnimationUtils
import kotlinx.android.synthetic.main.activity_new_data.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import net.pokeranalytics.android.R
import net.pokeranalytics.android.ui.activity.components.PokerAnalyticsActivity
import net.pokeranalytics.android.ui.extensions.px


class NewDataMenuActivity : PokerAnalyticsActivity() {

	enum class IntentKey(val keyName: String) {
		CHOICE("CHOICE"),
	}

	companion object {
		fun newInstance(context: Context) {
			val intent = Intent(context, NewDataMenuActivity::class.java)
			context.startActivity(intent)
		}
	}

	private var choiceSelected = false
	private var menuWillBeHidden = false
	private val fabSize = 48.px

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

		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { // used to fix Oreo crash
			requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
		}

		setContentView(R.layout.activity_new_data)
		initUI()
	}

	override fun onBackPressed() {
		hideMenu()
	}

	override fun onPause() {
		super.onPause()
		overridePendingTransition(0, 0)
	}

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

		overridePendingTransition(0, 0)

		container.viewTreeObserver.addOnGlobalLayoutListener {
			showMenu()
		}

		newCashGame.setOnClickListener {
			finishWithResult(0)
		}

		newTournament.setOnClickListener {
			finishWithResult(1)
		}

		newTransaction.setOnClickListener {
			finishWithResult(2)
		}

		container.setOnClickListener {
			hideMenu()
		}
	}

	/**
	 * Set the result and hide menu
	 */
	private fun finishWithResult(choice: Int) {

		if (choiceSelected) {
			return
		}
		choiceSelected = true

		val intent = Intent()
		intent.putExtra(IntentKey.CHOICE.keyName, choice)
		setResult(RESULT_OK, intent)
		GlobalScope.launch(Dispatchers.Main) {
			delay(200)
			hideMenu()
		}
	}

	/**
	 * Show menu
	 */
	private fun showMenu() {

		val cx = menuContainer.measuredWidth - fabSize / 2
		val cy = menuContainer.measuredHeight - fabSize / 2
		val finalRadius = Math.max(menuContainer.width, menuContainer.height)
		val anim = ViewAnimationUtils.createCircularReveal(menuContainer, cx, cy, 0f, finalRadius.toFloat())
		anim.duration = 150

		menuContainer.visibility = View.VISIBLE
		anim.start()
	}

	/**
	 * Hide menu
	 */
	private fun hideMenu() {

		if (menuWillBeHidden) {
			return
		}
		menuWillBeHidden = true

		val cx = menuContainer.measuredWidth - fabSize / 2
		val cy = menuContainer.measuredHeight - fabSize / 2
		val initialRadius = menuContainer.width
		val anim = ViewAnimationUtils.createCircularReveal(menuContainer, cx, cy, initialRadius.toFloat(), 0f)
		anim.duration = 150

		anim.addListener(object : AnimatorListenerAdapter() {
			override fun onAnimationEnd(animation: Animator?) {
				super.onAnimationEnd(animation)
				menuContainer.visibility = View.INVISIBLE
				finish()
			}
		})

		anim.start()
	}

}