package net.pokeranalytics.android.ui.fragment

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.fragment_data_list.*
import net.pokeranalytics.android.R
import net.pokeranalytics.android.ui.adapter.RowRepresentableAdapter
import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate
import net.pokeranalytics.android.ui.adapter.StaticRowRepresentableDataSource
import net.pokeranalytics.android.ui.fragment.components.PokerAnalyticsFragment
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowViewType
import net.pokeranalytics.android.ui.view.rowrepresentable.SeparatorRow
import java.util.*

class CurrenciesFragment : PokerAnalyticsFragment(), StaticRowRepresentableDataSource, RowRepresentableDelegate {

	companion object {

		const val INTENT_CURRENCY_CODE = "INTENT_CURRENCY_CODE"

		val rowRepresentation: List<RowRepresentable> by lazy {
			val rows = ArrayList<RowRepresentable>()
			rows.addAll(mostUsedCurrencies)
			rows.add(SeparatorRow())
			rows.addAll(availableCurrencies)
			rows
		}

		private val mostUsedCurrencyCodes = arrayListOf("EUR", "USD", "CAD", "GBP", "AUD", "CNY")
		private val systemCurrencies = Currency.getAvailableCurrencies()

		private val mostUsedCurrencies = this.mostUsedCurrencyCodes.map { code ->
			CurrencyRow(
				this.systemCurrencies.filter {
					it.currencyCode == code
				}.first()
			)
		}

		private val availableCurrencies = this.systemCurrencies.filter {
			!mostUsedCurrencyCodes.contains(it.currencyCode)
		}.filter {
			Locale.getAvailableLocales().filter { locale ->
				try {
					Currency.getInstance(locale).currencyCode == it.currencyCode
				} catch (e: Exception) {
					false
				}
			}.isNotEmpty()
		}.sortedBy {
			it.displayName
		}.map {
			CurrencyRow(it)
		}
	}

	private class CurrencyRow(var currency: Currency) : RowRepresentable {

		override fun getDisplayName(context: Context): String {
			return currency.getDisplayName(Locale.getDefault()).capitalize()
		}

		var currencyCode: String = currency.currencyCode
		var currencySymbole: String = currency.getSymbol(Locale.getDefault())
		var currencyCodeAndSymbol: String = "${this.currencyCode} (${this.currencySymbole})"

		override val viewType: Int = RowViewType.TITLE_VALUE.ordinal
	}


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

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


	// StaticRowRepresentableDataSource

	override fun adapterRows(): List<RowRepresentable>? {
		return CurrenciesFragment.rowRepresentation

	}

	override fun stringForRow(row: RowRepresentable): String {
		return (row as CurrencyRow).currencyCodeAndSymbol
	}

	// RowRepresentableDelegate
	override fun onRowSelected(position: Int, row: RowRepresentable, fromAction: Boolean) {
		val intent = Intent()
		intent.putExtra(INTENT_CURRENCY_CODE, (row as CurrencyRow).currency.currencyCode)
		this.activity?.setResult(Activity.RESULT_OK, intent)
		this.activity?.finish()
	}

	private fun initData() {
	}

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

		setDisplayHomeAsUpEnabled(true)
		setToolbarTitle(getString(R.string.currency))

		val viewManager = LinearLayoutManager(requireContext())
		val dataListAdapter = RowRepresentableAdapter(this, this)

		recyclerView.apply {
			setHasFixedSize(true)
			layoutManager = viewManager
			adapter = dataListAdapter
		}
	}
}