package edu.byu.support.payment.controller

import android.app.AlertDialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import edu.byu.support.ByuClickListener
import edu.byu.support.ByuViewHolder
import edu.byu.support.R
import edu.byu.support.activity.ByuActivity
import edu.byu.support.activity.ByuRecyclerViewActivity
import edu.byu.support.adapter.ByuRecyclerAdapter
import edu.byu.support.payment.model.PaymentAccount
import edu.byu.support.retrofit.ByuSuccessCallback
import edu.byu.support.utils.setPositiveButton
import retrofit2.Call
import retrofit2.Response

/**
 * Created by geogor37 on 2/9/18
 */
abstract class PaymentAccountsActivity: ByuRecyclerViewActivity() {
	protected val adapter: PaymentMethodAdapter = PaymentMethodAdapter()

	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState, R.layout.payment_accounts_activity)

		setAdapter(adapter)

		showProgressDialog()
		loadPaymentMethods()

		findViewById<Button>(R.id.manageSourcesButton).setOnClickListener { startChromeTabView(getString(R.string.manage_sources_url)) }
	}

	abstract fun loadPaymentMethods()

	abstract fun continuePaymentProcess(selectedPaymentAccount: PaymentAccount)

	protected fun showNoAccountsDialog() {
		AlertDialog.Builder(this)
				.setTitle(R.string.no_payment_account_title)
				.setMessage(R.string.no_payment_account_message)
				.setPositiveButton { _, _ ->
					onBackPressed()
					startChromeTabView(getString(R.string.manage_sources_url))
				}
				.setNegativeButton(getString(android.R.string.no)) { _, _ -> onBackPressed() }
				.show()
	}

	protected inner class PaymentMethodAdapter(list: List<PaymentAccount>? = null): ByuRecyclerAdapter<PaymentAccount>(list) {
		override fun getItemViewType(position: Int): Int {
			return R.layout.payment_method_list_item
		}

		override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ByuViewHolder<PaymentAccount> {
			return PaymentMethodViewHolder(LayoutInflater.from(this@PaymentAccountsActivity).inflate(viewType, parent, false))
		}

		private inner class PaymentMethodViewHolder(itemView: View): ByuViewHolder<PaymentAccount>(itemView) {
			private val accountTypeImageView = itemView.findViewById<ImageView>(R.id.accountTypeImage)
			private val accountNumberTextView = itemView.findViewById<TextView>(R.id.accountNumberText)
			private val accountNicknameTextView = itemView.findViewById<TextView>(R.id.accountNickNameText)
			private val financialInstitutionTextView = itemView.findViewById<TextView>(R.id.financialInstitutionText)
			private val processingFeeTextView = itemView.findViewById<TextView>(R.id.processingFeeText)

			override fun bind(data: PaymentAccount, listener: ByuClickListener<PaymentAccount>?) {
				accountTypeImageView.setImageResource(data.getImage())
				accountNumberTextView.text = data.getFormattedAccountNumber()
				accountNicknameTextView.text = data.nickname ?: ""
				financialInstitutionTextView.text = data.financialInstitution
				processingFeeTextView.visibility = if (data.isCreditCard()) View.VISIBLE else View.GONE

				itemView.setOnClickListener { continuePaymentProcess(data) }
			}
		}
	}

	protected abstract inner class PaymentAccountsCallback<T>(callManager: ByuActivity,  pathToReadableError: String? = null, defaultErrorMessage: String = ERROR_MESSAGE):
			ByuSuccessCallback<T>(callManager, defaultErrorMessage, pathToReadableError) {

		abstract fun parseResponse(response: Response<T>?): List<PaymentAccount>?

		override fun onSuccess(call: Call<T>?, response: Response<T>?) {
			dismissProgressDialog()
			adapter.list = parseResponse(response)
			if (adapter.isEmpty) {
				showNoAccountsDialog()
			}
		}
	}
}