package net.pokeranalytics.android.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AlertDialog
import com.google.android.material.bottomnavigation.BottomNavigationView
import kotlinx.android.synthetic.main.activity_home.*
import net.pokeranalytics.android.R
import net.pokeranalytics.android.ui.activity.components.PokerAnalyticsActivity
import net.pokeranalytics.android.ui.adapter.HomePagerAdapter
import net.pokeranalytics.android.util.Preferences
import timber.log.Timber


class HomeActivity : PokerAnalyticsActivity() {

	companion object {
		fun newInstance(context: Context, id: Int) {
			val intent = Intent(context, HomeActivity::class.java)
			intent.putExtra("id", 10)
			context.startActivity(intent)
		}
	}

	private var homeMenu: Menu? = null

	private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
		when (item.itemId) {
			net.pokeranalytics.android.R.id.navigation_history -> {
				displayFragment(0)
				return@OnNavigationItemSelectedListener true
			}
			net.pokeranalytics.android.R.id.navigation_stats -> {
				displayFragment(1)
				return@OnNavigationItemSelectedListener true
			}
			net.pokeranalytics.android.R.id.navigation_settings -> {
				displayFragment(2)
				return@OnNavigationItemSelectedListener true
			}
		}
		false
	}

	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		setContentView(R.layout.activity_home)

		initUI()
		checkFirstLaunch()
	}

	override fun onCreateOptionsMenu(menu: Menu?): Boolean {
		menuInflater.inflate(R.menu.home_menu, menu)
		this.homeMenu = menu
		return super.onCreateOptionsMenu(menu)
	}

	override fun onOptionsItemSelected(item: MenuItem?): Boolean {
		when (item?.itemId) {
			R.id.filter -> manageFilters()
		}
		return super.onOptionsItemSelected(item)
	}

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

		setSupportActionBar(toolbar)

		navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
		navigation.selectedItemId = net.pokeranalytics.android.R.id.navigation_history

		val homePagerAdapter = HomePagerAdapter(supportFragmentManager)
		viewPager.offscreenPageLimit = 5
		viewPager.enablePaging = false
		viewPager.adapter = homePagerAdapter

	}

	/**
	 * Check first launch
	 */
	private fun checkFirstLaunch() {
		if (Preferences.getBoolean(Preferences.Keys.FIRST_LAUNCH, this, true)) {
			Preferences.setBoolean(Preferences.Keys.FIRST_LAUNCH, false, this)
			val builder = AlertDialog.Builder(this)
				.setTitle(String.format(getString(R.string.welcome_in_), getString(R.string.app_name)))
				.setMessage(R.string.popup_message_welcome)
				.setNegativeButton(R.string.ok, null)
			builder.show()
		}
	}

	/**
	 * Display a new fragment
	 */
	private fun displayFragment(index: Int) {
		viewPager.setCurrentItem(index, false)
		updateToolbar(index)
	}

	/**
	 * Update toolbar
	 */
	private fun updateToolbar(index: Int) {
		when (index) {
			0 -> {
				toolbar.title = getString(R.string.title_history)
				homeMenu?.findItem(R.id.filter)?.isVisible = true
			}
			1 -> {
				toolbar.title = getString(R.string.title_stats)
				homeMenu?.findItem(R.id.filter)?.isVisible = false
			}
			2 -> {
				toolbar.title = getString(R.string.title_settings)
				homeMenu?.findItem(R.id.filter)?.isVisible = false
			}
		}
	}

	/**
	 * Manage filters
	 */
	private fun manageFilters() {

		val filterSelected = false

		val choices = ArrayList<CharSequence>()
		choices.add(getString(R.string.new_str))

		if (filterSelected) {
			choices.add(getString(R.string.modify_current_filter))
			choices.add(getString(R.string.load_from_db))
			choices.add(getString(R.string.remove_filter))
		}

		val builder = AlertDialog.Builder(this)
		builder.setTitle(R.string.filter_selection)
			.setCancelable(true)
			.setItems(choices.toTypedArray()) { _, which ->
				Timber.d("Click on $which")

				when(which) {
					0 -> FiltersActivity.newInstance(this@HomeActivity)
				}

			}
			.setNegativeButton(R.string.cancel) { _, which ->
				Timber.d("Click on cancel")
			}

		builder.show()

	}

}
