package com.laseroptek.raman.ui.screens.home.standby

import android.content.res.Configuration
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.laseroptek.raman.R
import com.laseroptek.raman.data.datasource.db.RamanDatabase
import com.laseroptek.raman.data.source.db.RamanDatabaseService
import com.laseroptek.raman.repository.DatabaseRepository
import com.laseroptek.raman.repository.PreferenceRepository
import com.laseroptek.raman.repository.SerialPortRepository
import com.laseroptek.raman.ui.screens.main.MainViewModel
import com.laseroptek.raman.ui.theme.RobotoTypography
import com.laseroptek.raman.utils.DefaultDispatcherProvider
import com.laseroptek.raman.utils.ext.dropShadow
import com.laseroptek.raman.utils.ext.noRippleClickable
import com.laseroptek.raman.utils.ext.px


@Composable
fun StandByButton(
    modifier: Modifier = Modifier,
    laserStatus: Int = 0x53,
    //mainViewModel: MainViewModel = hiltViewModel<MainViewModel>(),
    onClick: () -> Unit = {},
) {
    //val laserStatus by mainViewModel.laserStatus.collectAsState()
    //val handPiece by mainViewModel.handPiece.collectAsState()

    // 0x53: standBy (default)
    val buttonText = when (laserStatus) {
        0x52 -> stringResource(id = R.string.ready)
        0x49, 0x4C -> stringResource(id = R.string.laseron)
        else -> stringResource(id = R.string.standby)
    }

    val iconResource = when (laserStatus) {
        0x52 -> R.drawable.ic_ready
        0x49, 0x4C -> R.drawable.ic_laseron
        else -> R.drawable.ic_standby
    }

    val textBrush = when (laserStatus) {
        0x52 -> {
            SolidColor(Color(0, 0, 0))
        }
        0x49, 0x4C -> {
            SolidColor(Color(255, 255, 255))
        }
        else -> {
            SolidColor(Color(248,193,15))
        }
        /*
        else -> {
            Brush.linearGradient(
                colors = listOf(
                    Color(248,193,15),
                    Color(255,245,212),
                    Color(251,225,137)
                ),
                start = Offset(0f, 50f),
                end = Offset(100f, 50f)
            )
        }
        */
    }

    val backgroundBrush = when (laserStatus) {
        0x52 -> {
            SolidColor(Color(243, 199, 54))
            /*
            Brush.linearGradient(
                colors = listOf(
                    Color(29,78,216),
                    Color(30,58,138),
                ),
                start = Offset(0f, 50f),
                end = Offset(100f, 50f)
            )
            */
        }
        0x49, 0x4C -> {
            Brush.linearGradient(
                colors = listOf(
                    Color(163,17,17),
                    Color(204,20,20),
                    Color(112,9,9),
                ),
                start = Offset(0f, 50f),
                end = Offset(100f, 50f)
            )
        }
        else -> {
            Brush.linearGradient(
                colors = listOf(
                    Color(43,37,18),
                    Color(23,23,22),
                ),
                start = Offset(0f, 50f),
                end = Offset(100f, 50f)
            )
        }
    }

    val borderBrush = when (laserStatus) {
        0x52 -> {
            Brush.linearGradient(
                colors = listOf(
                    Color(222,216,185),
                    Color(255,255,255)
                ),
                start = Offset(0f, 50f),
                end = Offset(100f, 50f)
            )
        }
        0x49, 0x4C -> {
            Brush.linearGradient(
                colors = listOf(
                    Color(222,216,185),
                    Color(255,255,255)
                ),
                start = Offset(0f, 50f),
                end = Offset(100f, 50f)
            )
        }
        else -> {
            Brush.linearGradient(
                colors = listOf(
                    Color(222,216,185),
                    Color(255,255,255)
                ),
                //start = Offset(0f, 50f),
                //end = Offset(100f, 50f)
            )
        }
    }

    val dropShadowColor = when (laserStatus) {
        0x52 -> {
            Color(30,64,175)
        }
        0x49, 0x4C -> {
            Color(123,49,49).copy(alpha = 0.8f)
        }
        else -> {
            Color(0,0,0)
        }
    }

    val buttonShape = RoundedCornerShape(
        topStart = 60.px.dp,
        topEnd = 20.px.dp,
        bottomStart = 20.px.dp,
        bottomEnd = 60.px.dp
    )

    Row(
        modifier = modifier
            .noRippleClickable(onClick = { onClick.invoke() })
            .size(280.px.dp, 80.px.dp)
            .clip(
                buttonShape
            )
            .background(
                brush = backgroundBrush
            )
            .border(
                width = 0.5.px.dp,
                brush = borderBrush,
                shape = buttonShape
            ).dropShadow(
                shape = RoundedCornerShape(
                    topStart = 60.px.dp,
                    topEnd = 20.px.dp,
                    bottomStart = 20.px.dp,
                    bottomEnd = 60.px.dp
                ),
                color = dropShadowColor,
                blur = 0.px.dp,
                offsetX = 0.px.dp,
                offsetY = 1.66.px.dp,
                spread = 0.px.dp
            ),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.Center
    ) {
        Image(
            painter = painterResource(id = iconResource),
            contentDescription = null,
            modifier = Modifier
                .size(28.px.dp, 32.px.dp),
            contentScale = ContentScale.Crop,
        )

        Spacer(modifier = Modifier.width(5.px.dp))

        Text(
            text = buttonText,
            style = RobotoTypography.bodySmall.copy(
                brush = textBrush,
                fontWeight = FontWeight.ExtraLight,
                letterSpacing = (0).sp,
            ),
            fontSize = 36.px.sp
        )
    }
}


@Preview(
    apiLevel = 34,
    uiMode = Configuration.UI_MODE_NIGHT_NO,
    showBackground = false,
    device = "spec:width=1920dp,height=1080dp,dpi=150,isRound=false,chinSize=0dp,orientation=landscape"
)
@Composable
fun StandByButtonPreview(
    mainViewModel:MainViewModel = MainViewModel(
        preferenceRepository = PreferenceRepository(LocalContext.current),
        serialPortRepository = SerialPortRepository.createWithFakeRepository(),
        databaseRepository = DatabaseRepository(RamanDatabaseService(RamanDatabase.getInstance(LocalContext.current))),
        dispatcherProvider = DefaultDispatcherProvider(),
        applicationContext = LocalContext.current
    )
) {
    StandByButton(
        //laserStatus = 0x52, // ready
        laserStatus = 0x49, // laser on
    )
}