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

import androidx.compose.foundation.layout.Column
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.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.thermometer.view.DisplayState
import llc.arma.ble.app.ui.screen.inspection.thermometer.view.IntervalEdit
import llc.arma.ble.app.ui.screen.inspection.thermometer.view.LoadingState
import llc.arma.ble.app.ui.screen.inspection.thermometer.view.PowerEdit
import llc.arma.ble.app.ui.screen.inspection.thermometer.view.TemperatureHistory
import llc.arma.ble.app.ui.screen.inspection.thermometer.view.Write
import llc.arma.ble.domain.model.Ble

enum class SheetPage {
    INTERVAL, POWER, TEMPERATURE_HISTORY, WRITE
}

val Boolean.localizedName: String
    get() {
        return if(this){
            "Включено"
        } else {
            "Выключено"
        }
    }

val Ble.BleState.TX.localizedName: String
    get() {
        return when(this){
            Ble.BleState.TX.MINUS_40 -> "-40"
            Ble.BleState.TX.MINUS_20 -> "-20"
            Ble.BleState.TX.MINUS_16 -> "-16"
            Ble.BleState.TX.MINUS_12 -> "-12"
            Ble.BleState.TX.MINUS_8 -> "-8"
            Ble.BleState.TX.MINUS_4 -> "-4"
            Ble.BleState.TX.ZERO -> "0"
            Ble.BleState.TX.PLUS_3 -> "3"
            Ble.BleState.TX.PLUS_4 -> "4"
        }.toString()

    }

@Composable
fun ThermometerScreen(
    ble: Ble.Thermometer,
    onNavigationEvent: (ThermometerContract.Effect.Navigation) -> Unit
) {

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

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

    val bottomDialog = rememberBottomDialogState()

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

                val currentState = viewModel.viewState.value

                if(currentState is ThermometerContract.State.Display) {
                    IntervalEdit(
                        state = currentState.thermometer,
                        onEvent = {
                            viewModel.setEvent(it)
                        }
                    )
                }

            }
            SheetPage.POWER -> bottomDialog.show {

                val currentState = viewModel.viewState.value

                if(currentState is ThermometerContract.State.Display) {
                    PowerEdit(
                        state = currentState.thermometer,
                        onEvent = {
                            viewModel.setEvent(it)
                        }
                    )
                }

            }
            SheetPage.TEMPERATURE_HISTORY -> bottomDialog.show {

                val currentState = viewModel.viewState.value

                if (currentState is ThermometerContract.State.Display) {
                    TemperatureHistory(
                        ble = currentState.thermometer.info
                    )
                }
            }
            SheetPage.WRITE -> bottomDialog.show {

                val currentState = viewModel.viewState.value

                if (currentState is ThermometerContract.State.Display) {

                    currentState.writeState?.let {

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

                    }

                }
            }
            else -> {
                bottomDialog.hide()
            }
        }
    }

    LaunchedEffect("effect"){
        viewModel.effect.onEach {
            when(it){
                is ThermometerContract.Effect.Navigation -> {
                    sheetPage = null
                    onNavigationEvent(it)
                }
                is ThermometerContract.Effect.HideIntervalPicker -> launch {
                    sheetPage = null
                    delay(100)
                }
                is ThermometerContract.Effect.ShowIntervalPicker -> launch {
                    sheetPage = null
                    delay(100)
                    sheetPage = SheetPage.INTERVAL
                }
                is ThermometerContract.Effect.HidePowerPicker -> launch {
                    sheetPage = null
                    delay(100)
                }
                is ThermometerContract.Effect.ShowPowerPicker -> launch {
                    sheetPage = null
                    delay(100)
                    sheetPage = SheetPage.POWER
                }
                is ThermometerContract.Effect.HideTemperatureHistory -> launch {
                    sheetPage = null
                    delay(100)
                }
                is ThermometerContract.Effect.ShowTemperatureHistory -> launch {
                    sheetPage = null
                    delay(100)
                    sheetPage = SheetPage.TEMPERATURE_HISTORY
                }
                is ThermometerContract.Effect.HideWriteBle -> {
                    sheetPage = null
                    delay(100)
                }
                is ThermometerContract.Effect.ShowWriteBle -> {
                    sheetPage = null
                    delay(100)
                    sheetPage = SheetPage.WRITE
                }
            }
        }.launchIn(this)

    }

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

    Column {

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

    }

}