package llc.arma.ble.app.ui.screen.inspection.beacon

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.hilt.navigation.compose.hiltViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import llc.arma.ble.app.ui.common.rememberBottomDialogState
import llc.arma.ble.app.ui.screen.inspection.beacon.view.DisplayState
import llc.arma.ble.app.ui.screen.inspection.beacon.view.PowerEdit
import llc.arma.ble.app.ui.screen.inspection.beacon.view.Write
import llc.arma.ble.domain.model.Ble

enum class SheetPage {
    WRITE, POWER_EDIT
}

@Composable
fun BeaconScreen(
    ble: Ble.Beacon,
    onNavigationEvent: (BeaconContract.Effect.Navigation) -> Unit
) {

    val viewModel = hiltViewModel<BeaconViewModel>()
    val state = viewModel.viewState.value

    var sheetPage by rememberSaveable {
        mutableStateOf<SheetPage?>(null)
    }

    val bottomDialog = rememberBottomDialogState()

    LaunchedEffect("effect"){
        viewModel.effect.onEach {
            when(it){
                is BeaconContract.Effect.Navigation -> onNavigationEvent(it)
                BeaconContract.Effect.HideWriteBlePreview -> launch {
                    sheetPage = null
                }
                BeaconContract.Effect.ShowWriteBlePreview -> launch {
                    sheetPage = null
                    delay(100)
                    sheetPage = SheetPage.WRITE
                }
                BeaconContract.Effect.HidePowerPicker -> launch {
                    sheetPage = null
                }
                BeaconContract.Effect.ShowPowerPicker -> launch {
                    sheetPage = null
                    delay(100)
                    sheetPage = SheetPage.POWER_EDIT
                }
            }
        }.launchIn(this)
    }

    LaunchedEffect(ble){
        viewModel.setEvent(BeaconContract.Event.OnBleChanged(ble))
    }

    LaunchedEffect(sheetPage){
        when(sheetPage){
            SheetPage.WRITE -> bottomDialog.show {

                val currentState = viewModel.viewState.value

                if(currentState is BeaconContract.State.Display && currentState.writeState != null) {

                    Write(
                        state = currentState.writeState,
                        onEvent = {
                            viewModel.setEvent(it)
                        }
                    )

                }

            }
            SheetPage.POWER_EDIT -> bottomDialog.show {
                val currentState = viewModel.viewState.value

                if(currentState is BeaconContract.State.Display) {
                    PowerEdit(
                        state = currentState.beacon,
                        onEvent = {
                            viewModel.setEvent(it)
                        }
                    )
                }
            }
            else -> {
                bottomDialog.hide()
            }
        }
    }

    Column {

        when(state){
            is BeaconContract.State.Display -> DisplayState(
                onEvent = {
                    viewModel.setEvent(it)
                },
                ble = state.beacon,
                origin = state.origin
            )
            is BeaconContract.State.Loading -> LoadingState()
        }
        
    }
    
}

@Composable
private fun LoadingState(){

    Box(modifier = Modifier.fillMaxSize()){

        CircularProgressIndicator(modifier = Modifier.align(Alignment.Center))

    }

}