package com.za.base.theme

import android.app.Activity
import android.view.ViewGroup
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material.ripple.RippleAlpha
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.LocalRippleConfiguration
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ProvideTextStyle
import androidx.compose.material3.RippleConfiguration
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalView
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updateLayoutParams
import androidx.core.view.updatePadding

private val LightColorScheme = lightColorScheme(
	primary = Purple40,
	secondary = PurpleGrey40,
	tertiary = Pink40,
	background = bgColor,
	onBackground = bgColor,
)

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DealerTheme(darkTheme : Boolean = isSystemInDarkTheme(), content : @Composable () -> Unit) {
	val colorScheme = when {
		darkTheme -> LightColorScheme
		else -> LightColorScheme
	}
	val view = LocalView.current
	if (! view.isInEditMode) {
		SideEffect {
			val window = (view.context as Activity).window
			window.statusBarColor = Color.Transparent.toArgb()
			WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = ! darkTheme
			WindowCompat.setDecorFitsSystemWindows(window, false)
			ViewCompat.setOnApplyWindowInsetsListener(window.decorView.findViewById(android.R.id.content)) { view, windowInsets ->
				val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
				view as ViewGroup
				view.setBackgroundColor(headBgColor.toArgb())
				view.updateLayoutParams<ViewGroup.MarginLayoutParams> {
					leftMargin = insets.left
					bottomMargin = insets.bottom
					rightMargin = insets.right
					topMargin = 0
				}

				view.updatePadding(top = insets.top)

				WindowInsetsCompat.CONSUMED
			}
		}
	}
	MaterialTheme(colorScheme = colorScheme) {
		CompositionLocalProvider(LocalRippleConfiguration provides RippleConfiguration(rippleAlpha = RippleAlpha(
			0f,
			0f,
			0f,
			0f))) {
			ProvideTextStyle(value = MaterialTheme.typography.bodyLarge, content = content)
		}
	}
}

//object NoRippleTheme : RippleTheme {
//
//    @Composable
//    override fun defaultColor(): Color {
//        return Color.Unspecified
//    }
//
//    @Composable
//    override fun rippleAlpha(): RippleAlpha {
//        return RippleAlpha(0f, 0f, 0f, 0f)
//    }
//}