package net.pokeranalytics.android.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
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.fragment.HistoryFragment
import net.pokeranalytics.android.ui.fragment.SettingsFragment
import net.pokeranalytics.android.ui.fragment.StatsFragment



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 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(net.pokeranalytics.android.R.layout.activity_home)

		initUI()
	}

	/**
	 * Init UI
	 */
	private fun initUI() {
		navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
		navigation.selectedItemId = net.pokeranalytics.android.R.id.navigation_history
	}

	/**
	 * Display a new fragment
	 */
	private fun displayFragment(index: Int) {

		toolbar.title = when (index) {
			0 -> getString(R.string.title_history)
			1 -> getString(R.string.title_stats)
			2 -> getString(R.string.title_settings)
			else -> ""
		}

		val fragment = when(index) {
			0 -> HistoryFragment()
			1 -> StatsFragment()
			else -> SettingsFragment()
		}

		val fragmentManager = supportFragmentManager
		val count = fragmentManager.backStackEntryCount
		for (i in 0 until count) {
			fragmentManager.popBackStack()
		}

		val fragmentTransaction = fragmentManager.beginTransaction()
		fragmentTransaction.replace(net.pokeranalytics.android.R.id.container, fragment)
		fragmentTransaction.commit()

	}


}
