/*
 * ImageToolbox is an image editor for android
 * Copyright (c) 2026 T8RIN (Malik Mukhametzyanov)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * You should have received a copy of the Apache License
 * along with this program.  If not, see <http://www.apache.org/licenses/LICENSE-2.0>.
 */

package com.t8rin.imagetoolbox.core.ui.theme

import android.annotation.SuppressLint
import android.os.Build
import androidx.compose.animation.core.tween
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialExpressiveTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Shapes
import androidx.compose.material3.Surface
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import com.t8rin.dynamic.theme.ColorTuple
import com.t8rin.dynamic.theme.DynamicTheme
import com.t8rin.dynamic.theme.rememberDynamicThemeState
import com.t8rin.imagetoolbox.core.settings.presentation.provider.LocalSettingsState
import com.t8rin.imagetoolbox.core.settings.presentation.provider.rememberAppColorTuple
import com.t8rin.imagetoolbox.core.ui.utils.animation.FancyTransitionEasing
import com.t8rin.imagetoolbox.core.ui.utils.helper.DeviceInfo
import com.t8rin.imagetoolbox.core.ui.widget.modifier.AutoCornersShape

@SuppressLint("NewApi")
@Composable
fun ImageToolboxTheme(
    content: @Composable () -> Unit
) {
    val settingsState = LocalSettingsState.current
    val context = LocalContext.current

    DynamicTheme(
        typography = rememberTypography(settingsState.font),
        state = rememberDynamicThemeState(rememberAppColorTuple()),
        colorBlindType = settingsState.colorBlindType,
        defaultColorTuple = settingsState.appColorTuple,
        dynamicColor = settingsState.isDynamicColors,
        dynamicColorsOverride = {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.BAKLAVA && DeviceInfo.isPixel()) {
                val colors = dynamicLightColorScheme(context)

                ColorTuple(
                    primary = colors.primary,
                    secondary = colors.secondary,
                    tertiary = colors.tertiary,
                    surface = colors.surface,
                    neutralVariant = colors.surfaceVariant,
                    error = colors.error
                )
            } else null
        },
        amoledMode = settingsState.isAmoledMode,
        isDarkTheme = settingsState.isNightMode,
        contrastLevel = settingsState.themeContrastLevel,
        style = settingsState.themeStyle,
        isInvertColors = settingsState.isInvertThemeColors,
        colorAnimationSpec = tween(
            durationMillis = 400,
            easing = FancyTransitionEasing
        ),
        content = {
            MaterialExpressiveTheme(
                motionScheme = CustomMotionScheme,
                shapes = modifiedShapes(),
                content = content
            )
        }
    )
}

@Composable
fun ImageToolboxThemeSurface(
    content: @Composable BoxScope.() -> Unit
) {
    ImageToolboxTheme {
        Surface(
            modifier = Modifier.fillMaxSize(),
            content = {
                Box(
                    modifier = Modifier.fillMaxSize(),
                    content = content
                )
            }
        )
    }
}

@Composable
internal fun modifiedShapes(): Shapes {
    val shapes = MaterialTheme.shapes
    val shapesType = LocalSettingsState.current.shapesType

    return remember(shapes, shapesType) {
        derivedStateOf {
            shapes.copy(
                extraSmall = AutoCornersShape(
                    topStart = shapes.extraSmall.topStart,
                    topEnd = shapes.extraSmall.topEnd,
                    bottomEnd = shapes.extraSmall.bottomEnd,
                    bottomStart = shapes.extraSmall.bottomStart,
                    shapesType = shapesType
                ),
                small = AutoCornersShape(
                    topStart = shapes.small.topStart,
                    topEnd = shapes.small.topEnd,
                    bottomEnd = shapes.small.bottomEnd,
                    bottomStart = shapes.small.bottomStart,
                    shapesType = shapesType
                ),
                medium = AutoCornersShape(
                    topStart = shapes.medium.topStart,
                    topEnd = shapes.medium.topEnd,
                    bottomEnd = shapes.medium.bottomEnd,
                    bottomStart = shapes.medium.bottomStart,
                    shapesType = shapesType
                ),
                large = AutoCornersShape(
                    topStart = shapes.large.topStart,
                    topEnd = shapes.large.topEnd,
                    bottomEnd = shapes.large.bottomEnd,
                    bottomStart = shapes.large.bottomStart,
                    shapesType = shapesType
                ),
                extraLarge = AutoCornersShape(
                    topStart = shapes.extraLarge.topStart,
                    topEnd = shapes.extraLarge.topEnd,
                    bottomEnd = shapes.extraLarge.bottomEnd,
                    bottomStart = shapes.extraLarge.bottomStart,
                    shapesType = shapesType
                ),
                largeIncreased = AutoCornersShape(
                    topStart = shapes.largeIncreased.topStart,
                    topEnd = shapes.largeIncreased.topEnd,
                    bottomEnd = shapes.largeIncreased.bottomEnd,
                    bottomStart = shapes.largeIncreased.bottomStart,
                    shapesType = shapesType
                ),
                extraLargeIncreased = AutoCornersShape(
                    topStart = shapes.extraLargeIncreased.topStart,
                    topEnd = shapes.extraLargeIncreased.topEnd,
                    bottomEnd = shapes.extraLargeIncreased.bottomEnd,
                    bottomStart = shapes.extraLargeIncreased.bottomStart,
                    shapesType = shapesType
                ),
                extraExtraLarge = AutoCornersShape(
                    topStart = shapes.extraExtraLarge.topStart,
                    topEnd = shapes.extraExtraLarge.topEnd,
                    bottomEnd = shapes.extraExtraLarge.bottomEnd,
                    bottomStart = shapes.extraExtraLarge.bottomStart,
                    shapesType = shapesType
                )
            )
        }
    }.value
}

const val DisabledAlpha = 0.38f