package com.reactnativetink.activity import android.app.Activity import android.content.Intent import android.os.Bundle import androidx.activity.OnBackPressedCallback import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat import com.facebook.react.bridge.ActivityEventListener import com.reactnativetink.R import com.reactnativetink.api.OnGetAuthorizationCodeListener import com.reactnativetink.api.TinkAuthorizationCode import com.reactnativetink.models.TinkLinkError import com.reactnativetink.utils.TAG import com.reactnativetink.utils.UPDATE_CONSENT_REQUEST_CODE import com.reactnativetink.utils.Utils import com.tink.moneymanagerui.OverviewFeature import com.tink.moneymanagerui.OverviewFeatures import com.tink.moneymanagerui.StatisticType import com.tink.moneymanagerui.TinkMoneyManager import com.tink.moneymanagerui.accounts.AccountEditConfiguration import com.tink.moneymanagerui.accounts.EditAccountField import com.tink.moneymanagerui.accounts.FilterAll import com.tink.moneymanagerui.accounts.GroupingByKind import com.tink.moneymanagerui.entrypoints.EntryPoint import com.tink.moneymanagerui.insights.actionhandling.InsightActionHandler import timber.log.Timber class FinanceOverviewActivity : AppCompatActivity(), InsightActionHandler, OnGetAuthorizationCodeListener, ActivityEventListener { private lateinit var clientId: String private lateinit var userAccessToken: String private lateinit var appAccessToken: String private lateinit var userId: String private lateinit var idHint: String private lateinit var actorClientId: String private lateinit var scope: String private lateinit var redirectUri: String private val overviewEntrypoint = EntryPoint.Overview( overviewFeatures = OverviewFeatures( listOf( OverviewFeature.ActionableInsights, OverviewFeature.Statistics( listOf( StatisticType.EXPENSES, StatisticType.INCOME, StatisticType.LEFT_TO_SPEND, ), ), OverviewFeature.Accounts( FilterAll, GroupingByKind, AccountEditConfiguration( listOf( EditAccountField.NAME, EditAccountField.KIND, EditAccountField.IS_INCLUDED, EditAccountField.IS_SHARED, ), ), ), OverviewFeature.LatestTransactions, OverviewFeature.Budgets, OverviewFeature.RecommendedBudgets, ), ), toolbarVisible = true, featureSpecificThemes = emptyMap(), insightActionHandler = this, ) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_finance_overview) window.decorView.setOnApplyWindowInsetsListener { view, insets -> view.setBackgroundColor(ContextCompat.getColor(this, R.color.tink_colorPrimary)) // Adjust padding to avoid overlap view.setPadding(0, 0, 0, 0) insets } onBackPressedDispatcher.addCallback( this, object : OnBackPressedCallback(true) { override fun handleOnBackPressed() { setResult(RESULT_OK, intent) finish() } }, ) ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.fragment_container_activity)) { v, insets -> val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) insets } // Retrieve the data from the intent clientId = intent.getStringExtra("clientId") ?: throw IllegalArgumentException("clientId not provided") userAccessToken = intent.getStringExtra("userAccessToken") ?: throw IllegalArgumentException("userAccessToken not provided") appAccessToken = intent.getStringExtra("appAccessToken") ?: throw IllegalArgumentException("appAccessToken not provided") userId = intent.getStringExtra("userId") ?: throw IllegalArgumentException("userId not provided") idHint = intent.getStringExtra("idHint") ?: throw IllegalArgumentException("idHint not provided") actorClientId = intent.getStringExtra("actorClientId") ?: throw IllegalArgumentException("actorClientId not provided") scope = intent.getStringExtra("scope") ?: throw IllegalArgumentException("scope not provided") redirectUri = intent.getStringExtra("redirectUri") ?: throw IllegalArgumentException("redirectUri not provided") openFinanceOverview() } override fun refreshCredentials( credentialId: String, onComplete: (isActionDone: Boolean) -> Unit, ): Boolean { TinkAuthorizationCode().getAuthorizationCode(appAccessToken, userId, idHint, actorClientId, scope, credentialId, this) return true } override fun onGetAuthorizationCodeSuccess( code: String, credentialId: String, ) { val intent = Intent(this, ConnectAccountActivity::class.java) intent.putExtra("ClientID", clientId) intent.putExtra("RedirectUri", redirectUri) intent.putExtra("AuthorizationCode", code) intent.putExtra("CredentialsId", credentialId) intent.putExtra("IsUpdateConsent", true) this.startActivityForResult( intent, UPDATE_CONSENT_REQUEST_CODE, ) } override fun onGetAuthorizationCodeFailure() { TODO("Not yet implemented") } override fun onActivityResult( activity: Activity?, requestCode: Int, resultCode: Int, intent: Intent?, ) { if (requestCode == UPDATE_CONSENT_REQUEST_CODE && intent != null) { if (resultCode == Activity.RESULT_OK) { val credentialsId: String? = intent.getStringExtra("credentialsId") } else { val error: TinkLinkError = Utils().getSerializable(intent, "error", TinkLinkError::class.java) } } } override fun onNewIntent(p0: Intent) { super.onNewIntent(p0) } private fun openFinanceOverview() { TinkMoneyManager.init( accessToken = userAccessToken, styleResId = R.style.MoneyManagerStyle, backPressedListener = { Timber.tag(TAG).d("User navigated back") }, editPendingTransaction = false, enableTransactionDetail = true, enableRecommendedBudget = true, enableBudgetCreationSuccessScreen = true, entryPoint = overviewEntrypoint, containerId = R.id.fragment_container_activity, fragmentManager = supportFragmentManager, enableSubscriptions = false ) } }