package org.thoughtcrime.securesms.database

import android.content.Context
import android.database.Cursor
import androidx.core.content.contentValuesOf
import org.signal.core.util.CursorUtil
import org.signal.core.util.SqlUtil
import org.signal.core.util.money.FiatMoney
import org.thoughtcrime.securesms.database.model.DonationReceiptRecord
import java.math.BigDecimal
import java.util.Currency

class DonationReceiptDatabase(context: Context, databaseHelper: SignalDatabase) : Database(context, databaseHelper) {
  companion object {
    private const val TABLE_NAME = "donation_receipt"

    private const val ID = "_id"
    private const val TYPE = "receipt_type"
    private const val DATE = "receipt_date"
    private const val AMOUNT = "amount"
    private const val CURRENCY = "currency"
    private const val SUBSCRIPTION_LEVEL = "subscription_level"

    @JvmField
    val CREATE_TABLE = """
      CREATE TABLE $TABLE_NAME (
        $ID INTEGER PRIMARY KEY AUTOINCREMENT,
        $TYPE TEXT NOT NULL,
        $DATE INTEGER NOT NULL,
        $AMOUNT TEXT NOT NULL,
        $CURRENCY TEXT NOT NULL,
        $SUBSCRIPTION_LEVEL INTEGER NOT NULL
      )
    """.trimIndent()

    val CREATE_INDEXS = arrayOf(
      "CREATE INDEX IF NOT EXISTS donation_receipt_type_index ON $TABLE_NAME ($TYPE)",
      "CREATE INDEX IF NOT EXISTS donation_receipt_date_index ON $TABLE_NAME ($DATE)"
    )
  }

  fun addReceipt(record: DonationReceiptRecord) {
    require(record.id == -1L)

    val values = contentValuesOf(
      AMOUNT to record.amount.amount.toString(),
      CURRENCY to record.amount.currency.currencyCode,
      DATE to record.timestamp,
      TYPE to record.type.code,
      SUBSCRIPTION_LEVEL to record.subscriptionLevel
    )

    writableDatabase.insert(TABLE_NAME, null, values)
  }

  fun getReceipt(id: Long): DonationReceiptRecord? {
    readableDatabase.query(TABLE_NAME, null, ID_WHERE, SqlUtil.buildArgs(id), null, null, null).use { cursor ->
      return if (cursor.moveToNext()) {
        readRecord(cursor)
      } else {
        null
      }
    }
  }

  fun getReceipts(type: DonationReceiptRecord.Type?): List<DonationReceiptRecord> {
    val (where, whereArgs) = if (type != null) {
      "$TYPE = ?" to SqlUtil.buildArgs(type.code)
    } else {
      null to null
    }

    readableDatabase.query(TABLE_NAME, null, where, whereArgs, null, null, "$DATE DESC").use { cursor ->
      val results = ArrayList<DonationReceiptRecord>(cursor.count)
      while (cursor.moveToNext()) {
        results.add(readRecord(cursor))
      }

      return results
    }
  }

  private fun readRecord(cursor: Cursor): DonationReceiptRecord {
    return DonationReceiptRecord(
      id = CursorUtil.requireLong(cursor, ID),
      type = DonationReceiptRecord.Type.fromCode(CursorUtil.requireString(cursor, TYPE)),
      amount = FiatMoney(
        BigDecimal(CursorUtil.requireString(cursor, AMOUNT)),
        Currency.getInstance(CursorUtil.requireString(cursor, CURRENCY))
      ),
      timestamp = CursorUtil.requireLong(cursor, DATE),
      subscriptionLevel = CursorUtil.requireInt(cursor, SUBSCRIPTION_LEVEL)
    )
  }
}
