package net.pokeranalytics.android.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import io.realm.RealmResults
import net.pokeranalytics.android.R
import net.pokeranalytics.android.model.realm.Bankroll
import net.pokeranalytics.android.model.realm.ComputableResult
import net.pokeranalytics.android.model.realm.Transaction
import net.pokeranalytics.android.ui.activity.components.PokerAnalyticsActivity

class BankrollActivity : PokerAnalyticsActivity() {

    private lateinit var computableResults: RealmResults<ComputableResult>
    private lateinit var bankrolls: RealmResults<Bankroll>
    private lateinit var transactions: RealmResults<Transaction>

    companion object {
        fun newInstance(context: Context) {
            val intent = Intent(context, BankrollActivity::class.java)
            context.startActivity(intent)
        }

        /**
         * Create a new instance for result
         */
        fun newInstanceForResult(fragment: Fragment, requestCode: Int) {
            val intent = Intent(fragment.requireContext(), BankrollActivity::class.java)
            fragment.startActivityForResult(intent, requestCode)
        }

    }

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

//        this.computableResults = getRealm().where(ComputableResult::class.java).findAll() // ComputableResult are existing only if sessions are ended
//        this.computableResults.addChangeListener { t, changeSet ->
//
//            val bankrolls = mutableSetOf<Bankroll>()
//            val indexes = mutableSetOf<Int>()
//            indexes.addAll(changeSet.changes.toList())
//            indexes.addAll(changeSet.insertions.toList())
//            indexes.addAll(changeSet.deletions.toList())
//            indexes.forEach { index ->
//                t[index]?.session?.bankroll?.let { br ->
//                    bankrolls.add(br)
//                }
//            }
//            this.computeBankrollReports(bankrolls)
//        }
//        this.bankrolls = getRealm().where(Bankroll::class.java).findAll() // ComputableResult are existing only if sessions are ended
//        this.bankrolls.addChangeListener { _, changeSet ->
//
//
//
//
//
//        }
//        this.transactions = getRealm().where(Transaction::class.java).findAll() // ComputableResult are existing only if sessions are ended
//        this.transactions.addChangeListener { t, changeSet ->
//
//            val bankrolls = mutableSetOf<Bankroll>()
//            val indexes = mutableSetOf<Int>()
//            indexes.addAll(changeSet.changes.toList())
//            indexes.addAll(changeSet.insertions.toList())
//            indexes.addAll(changeSet.deletions.toList())
//            indexes.forEach { index ->
//                if (t.isNotEmpty()) {
//                    t[index]?.bankroll?.let { br ->
//                        bankrolls.add(br)
//                    }
//                }
//            }
//            this.computeBankrollReports(bankrolls)
//        }

    }

    fun computeBankrollReports(bankrolls: Collection<Bankroll>) {




    }

}