package com.zxhyis.toolss.notification.provider

import android.content.ContentProvider
import android.content.ContentValues
import android.content.UriMatcher
import android.database.Cursor
import android.database.MatrixCursor
import android.net.Uri

class MyContentProvider : ContentProvider() {
    private var myIntValue = 0
    override fun onCreate(): Boolean {
        return true
    }

    override fun query(
        uri: Uri,
        projection: Array<String>?,
        selection: String?,
        selectionArgs: Array<String>?,
        sortOrder: String?
    ): Cursor {
        var cursor: Cursor? = null
        cursor = when (uriMatcher.match(uri)) {
            1 -> {
                val matrixCursor = MatrixCursor(arrayOf("myIntValue"))
                matrixCursor.addRow(arrayOf<Any>(myIntValue))
                matrixCursor
            }

            else -> throw IllegalArgumentException("Unsupported URI: $uri")
        }
        return cursor
    }

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

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

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

    override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array<String>?): Int {
        myIntValue = when (uriMatcher.match(uri)) {
            1 -> values!!.getAsInteger("myIntValue")
            else -> throw IllegalArgumentException("Unsupported URI: $uri")
        }
        return 1
    }

    companion object {
        // 定义 UriMatcher
        private val uriMatcher = UriMatcher(UriMatcher.NO_MATCH)

        init {
            uriMatcher.addURI("com.zxhyis.toolss", "myIntValue", 1)
        }
    }
}
