package com.laseroptek.raman.ui.screens.main

import android.content.res.Configuration
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
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.utils.DefaultDispatcherProvider
import com.laseroptek.raman.utils.ext.noRippleClickable
import com.laseroptek.raman.utils.ext.px


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MainTopBar(
    mainViewModel: MainViewModel = hiltViewModel<MainViewModel>(),
    onClickWarning: () -> Unit = {},
    modifier: Modifier = Modifier,
) {
    //val handPiece by mainViewModel.handPiece.collectAsState()
    //val handPieceTitle = handPieceTypes.get(handPiece.type)
    val warning by mainViewModel.warning.collectAsState()

    Column {
        Row(
            modifier = modifier
                .fillMaxWidth()
                .height(45.px.dp)
                .padding(start = 30.px.dp, end = 25.px.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Image(
                painter = painterResource(id = R.drawable.logo),
                contentDescription = null,
                modifier = Modifier
                    .size(120.px.dp, 26.px.dp),
            )

            /*
            Spacer(Modifier.width(30.px.dp))

            Text(
                text = handPieceTitle,
                style = RobotoTypography.titleMedium,
                fontSize = 16.px.sp,
                lineHeight = 26.px.sp,
                letterSpacing = 0.92.px.sp,
                fontWeight = FontWeight.Normal,
                color = Color(255,255,255),
                textAlign = TextAlign.Center
            )
            */

            Spacer(Modifier.weight(1f))

            if (warning.code != 0) {
                Image(
                    painter = painterResource(id = R.drawable.ic_alert),
                    contentDescription = null,
                    modifier = Modifier
                        .noRippleClickable(onClick = onClickWarning)
                        .size(45.px.dp),
                )
            }

            /*
            Text(
                text = Date().formatToServerDateDefaults(),
                style = PretendardTypography.titleSmall,
                fontSize = 30.px.sp,
                color = Color("#54000000".toColorInt())
            )

            Spacer(Modifier.width(38.px.dp))

            Text(
                text = Date().formatToServerTimeDefaults(),
                style = PretendardTypography.titleSmall,
                fontSize = 30.px.sp,
                color = Color("#54000000".toColorInt())
            )
            */

            //Spacer(Modifier.width(57.px.dp))
        }

        //BottomShadow(alpha = .15f, height = 8.px.dp)
    }
}

@Preview(
    apiLevel = 34,
    uiMode = Configuration.UI_MODE_NIGHT_NO,
    showBackground = true,
    device = "spec:width=1280dp,height=720dp,dpi=150,isRound=false,chinSize=0dp,orientation=landscape"
)
@Composable
fun MainTopBarPreview(
    mainViewModel:MainViewModel = MainViewModel(
        preferenceRepository = PreferenceRepository(LocalContext.current),
        serialPortRepository = SerialPortRepository.createWithFakeRepository(),
        databaseRepository = DatabaseRepository(RamanDatabaseService(RamanDatabase.getInstance(LocalContext.current))),
        dispatcherProvider = DefaultDispatcherProvider(),
        applicationContext = LocalContext.current
    )
) {
    MainTopBar(
        mainViewModel = mainViewModel
    )
}
