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

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.host.view.DisplayState
import llc.arma.ble.app.ui.screen.inspection.host.view.IntervalEdit
import llc.arma.ble.app.ui.screen.inspection.host.view.PowerEdit
import llc.arma.ble.app.ui.screen.inspection.host.view.ReadIntervalEdit
import llc.arma.ble.app.ui.screen.inspection.host.view.Write
import llc.arma.ble.domain.model.Ble

enum class SheetPage {
    WRITE, POWER_EDIT, INTERVAL_EDIT, READ_INTERVAL_EDIT
}

@Composable
fun HostScreen(
    ble: Ble.Host,
    onNavigationEvent: (HostContract.Effect.Navigation) -> Unit
) {

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

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

    val bottomDialog = rememberBottomDialogState()

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

                HostContract.Effect.HideIntervalPicker -> launch {
                    sheetPage = null
                }
                HostContract.Effect.ShowIntervalPicker -> launch {
                    sheetPage = null
                    delay(100)
                    sheetPage = SheetPage.INTERVAL_EDIT
                }

                HostContract.Effect.HideReadIntervalPicker -> launch {
                    sheetPage = null
                }
                HostContract.Effect.ShowReadIntervalPicker -> launch {
                    sheetPage = null
                    delay(100)
                    sheetPage = SheetPage.READ_INTERVAL_EDIT
                }
            }
        }.launchIn(this)
    }

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

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

                val currentState = viewModel.viewState.value

                if(currentState is HostContract.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 HostContract.State.Display) {
                    PowerEdit(
                        state = currentState.host,
                        onEvent = {
                            viewModel.setEvent(it)
                        }
                    )
                }
            }
            SheetPage.INTERVAL_EDIT -> bottomDialog.show {
                val currentState = viewModel.viewState.value

                if(currentState is HostContract.State.Display) {
                    IntervalEdit(
                        state = currentState.host,
                        onEvent = {
                            viewModel.setEvent(it)
                        }
                    )
                }
            }
            SheetPage.READ_INTERVAL_EDIT -> bottomDialog.show {
                val currentState = viewModel.viewState.value

                if(currentState is HostContract.State.Display) {
                    ReadIntervalEdit(
                        state = currentState.host,
                        onEvent = {
                            viewModel.setEvent(it)
                        }
                    )
                }
            }
            else -> {
                bottomDialog.hide()
            }
        }
    }

    Column {

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

@Composable
private fun LoadingState(){

    Box(modifier = Modifier.fillMaxSize()){

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

    }

}