package com.xhly.manageapp.contentprovider

import android.content.ContentProvider
import android.content.ContentValues
import android.content.UriMatcher
import android.database.Cursor
import android.database.MatrixCursor
import android.net.Uri
import com.xhly.corelib.Const
import com.xhly.corelib.utils.SharedPreferencesUtils
import com.xhly.manageapp.bean.user.UserBean


class UserProvider : ContentProvider() {
    private var sputils: SharedPreferencesUtils? = null
    var matcher = UriMatcher(UriMatcher.NO_MATCH)
    var str="com.xhly.manageapp.userprovider"
    override fun onCreate(): Boolean {
        sputils = SharedPreferencesUtils.getInstance(context)
        matcher.addURI(str,"user",999)
        return true
    }

    override fun query(
        uri: Uri,
        projection: Array<out String>?,
        selection: String?,
        selectionArgs: Array<out String>?,
        sortOrder: String?
    ): Cursor? {
        var match = matcher.match(uri)
        try {
            sputils?.let { sp ->
                val userBean = sp.getFromJson(Const.USERINFO, UserBean().javaClass) as UserBean
                val cursor = MatrixCursor(arrayOf("userid", "schoolid", "regionid"))
                cursor.addRow(arrayListOf(userBean.userid, userBean.schoolid, userBean.regionid))
                return cursor
            }
        } catch (e: Exception) {

        }
        return null
    }

    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<out String>?): Int {
        return 0
    }

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