package com.optix.android.ai

import android.content.BroadcastReceiver
import android.content.ComponentName
import android.content.ContentResolver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.IntentSender
import android.content.ServiceConnection
import android.content.SharedPreferences
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.content.res.AssetManager
import android.content.res.Configuration
import android.content.res.Resources
import android.database.DatabaseErrorHandler
import android.database.sqlite.SQLiteDatabase
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.os.UserHandle
import android.view.Display
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.InputStream

/**
 * Minimal android.content.Context stub for unit tests. Only getSystemService is used
 * by NetworkUtils (returns null so it degrades gracefully). All other abstract members
 * are unreachable in tests and simply return safe defaults or throw.
 */
private open class FakeContext : Context() {
    override fun getSystemService(name: String): Any? = null
    override fun getApplicationContext(): Context = this
    @Deprecated("Deprecated in Java")
    override fun getSharedPreferences(name: String, mode: Int): SharedPreferences? = null
    override fun getPackageName(): String = "com.optix.android.test"
    override fun getContentResolver(): ContentResolver? = null
    override fun getAssets(): AssetManager? = null
    override fun getResources(): Resources? = null
    override fun getPackageManager(): PackageManager? = null
    override fun getMainLooper(): Looper? = null
    override fun getApplicationContextName(): String? = null
    override fun getFilesDir(): File? = null
    override fun getCacheDir(): File? = null
    override fun getExternalCacheDir(): File? = null
    override fun getDatabasePath(name: String?): File? = null
    override fun openFileInput(name: String?): FileInputStream? = null
    override fun openFileOutput(name: String?, mode: Int): FileOutputStream? = null
    override fun deleteFile(name: String?): Boolean = false
    override fun getFileStreamPath(name: String?): File? = null
    override fun getDir(name: String?, mode: Int): File? = null
    override fun openOrCreateDatabase(name: String?, mode: Int, factory: SQLiteDatabase.CursorFactory?): SQLiteDatabase? = null
    override fun openOrCreateDatabase(name: String?, mode: Int, factory: SQLiteDatabase.CursorFactory?, errorHandler: DatabaseErrorHandler?): SQLiteDatabase? = null
    override fun deleteDatabase(name: String?): Boolean = false
    override fun getString(resId: Int): String = ""
    override fun getString(resId: Int, vararg formatArgs: Any?): String = ""
    override fun getText(resId: Int): CharSequence = ""
    override fun getColor(resId: Int): Int = 0
    override fun getDrawable(resId: Int): Drawable? = null
    override fun getColorStateList(resId: Int): android.content.res.ColorStateList? = null
    override fun startActivity(intent: Intent?) {}
    override fun startActivity(intent: Intent?, options: Bundle?) {}
    @Deprecated("Deprecated in Java")
    override fun startIntentSender(intent: IntentSender?, fillInIntent: Intent?, flagsMask: Int, flagsValues: Int, extraFlags: Int) {}
    override fun startIntentSender(intent: IntentSender?, fillInIntent: Intent?, flagsMask: Int, flagsValues: Int, extraFlags: Int, options: Bundle?) {}
    override fun sendBroadcast(intent: Intent?) {}
    override fun sendBroadcast(intent: Intent?, receiverPermission: String?) {}
    override fun sendOrderedBroadcast(intent: Intent?, receiverPermission: String?) {}
    override fun sendOrderedBroadcast(intent: Intent?, receiverPermission: String?, resultReceiver: BroadcastReceiver?, scheduler: Handler?, initialCode: Int, initialData: String?, initialExtras: Bundle?) {}
    override fun sendBroadcastAsUser(intent: Intent?, user: UserHandle?) {}
    override fun sendBroadcastAsUser(intent: Intent?, user: UserHandle?, receiverPermission: String?) {}
    override fun sendOrderedBroadcastAsUser(intent: Intent?, user: UserHandle?, receiverPermission: String?, resultReceiver: BroadcastReceiver?, scheduler: Handler?, initialCode: Int, initialData: String?, initialExtras: Bundle?) {}
    override fun registerReceiver(receiver: BroadcastReceiver?, filter: IntentFilter?): Intent? = null
    override fun registerReceiver(receiver: BroadcastReceiver?, filter: IntentFilter?, flags: Int): Intent? = null
    override fun registerReceiver(receiver: BroadcastReceiver?, filter: IntentFilter?, broadcastPermission: String?, scheduler: Handler?): Intent? = null
    override fun registerReceiver(receiver: BroadcastReceiver?, filter: IntentFilter?, broadcastPermission: String?, scheduler: Handler?, flags: Int): Intent? = null
    override fun unregisterReceiver(receiver: BroadcastReceiver?) {}
    override fun bindService(service: Intent?, conn: ServiceConnection, flags: Int): Boolean = false
    override fun unbindService(conn: ServiceConnection) {}
    override fun startService(service: Intent?): ComponentName? = null
    override fun stopService(service: Intent?): Boolean = false
    override fun getSystemServiceName(serviceClass: Class<*>?): String? = null
    override fun getDisplay(): Display? = null
    override fun createDisplayContext(display: Display?): Context? = null
    override fun createConfigurationContext(overrideConfiguration: Configuration?): Context? = null
    override fun createContextForSplit(splitName: String?): Context? = null
    override fun getSharedPreferencesName(): String? = null
    override fun getOpPackageName(): String? = null
    override fun getAttributionTag(): String? = null
    override fun checkPermission(permission: String?, pid: Int, uid: Int): Int = PackageManager.PERMISSION_DENIED
    override fun checkCallingPermission(permission: String?): Int = PackageManager.PERMISSION_DENIED
    override fun checkCallingOrSelfPermission(permission: String?): Int = PackageManager.PERMISSION_DENIED
    override fun enforcePermission(permission: String?, pid: Int, uid: Int, message: String?) {}
    override fun enforceCallingPermission(permission: String?, message: String?) {}
    override fun enforceCallingOrSelfPermission(permission: String?, message: String?) {}
    override fun checkUriPermission(uri: Uri?, pid: Int, uid: Int, modeFlags: Int): Int = PackageManager.PERMISSION_DENIED
    override fun checkCallingUriPermission(uri: Uri?, modeFlags: Int): Int = PackageManager.PERMISSION_DENIED
    override fun checkCallingOrSelfUriPermission(uri: Uri?, modeFlags: Int): Int = PackageManager.PERMISSION_DENIED
    override fun checkUriPermission(uri: Uri?, readPermission: String?, writePermission: String?, pid: Int, uid: Int, modeFlags: Int): Int = PackageManager.PERMISSION_DENIED
    override fun enforceUriPermission(uri: Uri?, pid: Int, uid: Int, modeFlags: Int, message: String?) {}
    override fun enforceCallingUriPermission(uri: Uri?, modeFlags: Int, message: String?) {}
    override fun enforceCallingOrSelfUriPermission(uri: Uri?, modeFlags: Int, message: String?) {}
    override fun enforceUriPermission(uri: Uri?, readPermission: String?, writePermission: String?, pid: Int, uid: Int, modeFlags: Int, message: String?) {}
    override fun getPackageCodePath(): String = ""
    override fun getPackageResourcePath(): String = ""
    override fun getWallpaper(): Drawable? = null
    override fun peekWallpaper(): Drawable? = null
    override fun setWallpaper(bitmap: Bitmap?) {}
    override fun setWallpaper(data: InputStream?) {}
    override fun clearWallpaper() {}
    override fun getWallpaperDesiredMinimumWidth(): Int = 0
    override fun getWallpaperDesiredMinimumHeight(): Int = 0
    override fun getApplicationInfo(): ApplicationInfo? = null
    override fun getTheme(): android.content.res.Resources.Theme? = null
    override fun setTheme(resId: Int) {}
    override fun obtainStyledAttributes(attrs: IntArray?): android.content.res.TypedArray? = null
    override fun obtainStyledAttributes(resid: Int, attrs: IntArray?): android.content.res.TypedArray? = null
    override fun obtainStyledAttributes(set: android.util.AttributeSet?, attrs: IntArray?): android.content.res.TypedArray? = null
    override fun obtainStyledAttributes(set: android.util.AttributeSet?, attrs: IntArray?, defStyleAttr: Int, defStyleRes: Int): android.content.res.TypedArray? = null
    override fun getClassLoader(): ClassLoader? = javaClass.classLoader
    override fun getBasePackageName(): String? = null
    override fun getPackageResourcePathName(): String? = null
}
