package com.example.modifier.repo

import android.content.ContentProvider
import android.content.ContentValues
import android.content.Context
import android.database.Cursor
import android.database.MatrixCursor
import android.net.Uri
import androidx.datastore.preferences.preferencesDataStore
import com.example.modifier.BuildConfig
import kotlinx.coroutines.runBlocking

class SimInfoProvider : ContentProvider() {
    val Context.simInfoDataStore by preferencesDataStore(name = "${BuildConfig.APPLICATION_ID}.simInfo")

    override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?): Int {
        return 0
    }

    override fun getType(uri: Uri): String? {
        return null
    }

    override fun insert(uri: Uri, values: ContentValues?): Uri? {
        return null
    }

    override fun onCreate(): Boolean {
        return true
    }

    override fun query(
        uri: Uri, projection: Array<String>?, selection: String?,
        selectionArgs: Array<String>?, sortOrder: String?
    ): Cursor? {
        val matrixCursor = MatrixCursor(
            arrayOf(
                "mcc",
                "mnc",
                "iccid",
                "imsi",
                "imei",
                "number",
                "country",
                "carrier_id",
                "carrier_name",
                "upi_policy",
                "serial_no",
                "mac",
                "bssid"
            )
        );
        runBlocking {
            val simInfo = SpoofedInfoRepo.instance.spoofedSimInfo.value
            if (simInfo != null) {
                matrixCursor.addRow(
                    arrayOf(
                        simInfo.mcc,
                        simInfo.mnc,
                        simInfo.iccid,
                        simInfo.imsi,
                        simInfo.imei,
                        simInfo.number,
                        simInfo.country,
                        simInfo.carrierId,
                        simInfo.carrierName,
                        "upi-carrier-id-with-mo-sms-relax",
                        simInfo.serialNo,
                        simInfo.wifiMac,
                        simInfo.bssid
                    )
                )
            }

        }
        return matrixCursor
    }

    override fun update(
        uri: Uri, values: ContentValues?, selection: String?,
        selectionArgs: Array<String>?
    ): Int {
        return 0
    }
}