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

import llc.arma.ble.app.ui.common.ViewEvent
import llc.arma.ble.app.ui.common.ViewSideEffect
import llc.arma.ble.app.ui.common.ViewState
import llc.arma.ble.app.ui.model.BleView
import llc.arma.ble.app.ui.screen.inspection.accelerometer.view.RealtimeViewMode
import llc.arma.ble.domain.model.Ble
import llc.arma.ble.domain.model.BleInfo
import llc.arma.ble.domain.usecase.AccelScale
import llc.arma.ble.domain.usecase.AccelViewMode
import llc.arma.ble.domain.usecase.FftAxis
import llc.arma.ble.domain.usecase.FftFrequency
import llc.arma.ble.domain.usecase.FftViewMode

class AccelerometerContract {

    sealed class Event : ViewEvent {

        object OnShowAccelerometerAccel : Event()
        object OnHideAccelerometerAccel : Event()

        object OnHideAccelerometerSpectre : Event()

        object OnShowAccelerometerHistory : Event()
        object OnHideAccelerometerHistory : Event()

        object OnRealtimeViewModeEdit : Event()

        data class OnAccelViewModeEdit(
            val next: Next
        ) : Event()

        enum class Next {
            ACCEL, HISTORY
        }

        data class OnAccelScaleEdit(
            val next: Next
        ) : Event()

        object OnAccelEdit : Event()

        object OnHistoryEdit : Event()
        object OnFftFrequencyEdit : Event()

        object OnFftAxisEdit : Event()
        object OnFftModeEdit : Event()

        object OnPowerEdit : Event()

        object OnShowWriteBlePreview : Event()

        object OnWriteBle : Event()

        object OnHideWriteBlePreview : Event()

        object OnChangePassword : Event()
        object OnSaveIntervalEdit : Event()
        object OnHideHistoryEdit : Event()

        data class OnRealtimeViewModeChanged(
            val mode: RealtimeViewMode
        ) : Event()

        data class OnBleChanged(
            val ble: Ble.Accelerometer,
        ): Event()

        data class OnPowerChanged(
            val tx: BleView.BleState.TX
        ) : Event()

        data class OnAccelViewModelChanged(
            val mode: AccelViewMode
        ) : Event()

        data class OnHistoryViewModeChanged(
            val mode: AccelViewMode
        ) : Event()

        data class OnFftFrequencyChanged(
            val frequency: FftFrequency
        ) : Event()

        data class OnFftAxisChanged(
            val axis: FftAxis
        ) : Event()

        data class OnFftModeChanged(
            val mode: FftViewMode
        ) : Event()

        data class OnAccelScaleChanged(
            val scale: AccelScale
        ) : Event()

        data class OnHistoryScaleChanged(
            val scale: AccelScale
        ) : Event()

        data class OnSaveHistoryChanged(
            val save: Boolean
        ) : Event()

        data class OnSaveIntervalChanged(
            val interval: Long
        ) : Event()

    }

    sealed class State : ViewState {

        object Loading : State()

        data class Display(
            val origin: Ble.Accelerometer,
            val accelerometer: BleView.Accelerometer,
            val writeState: WriteState?,
            val accelViewMode: AccelViewMode,
            val accelRealtimeViewMode: RealtimeViewMode,
            val accelScale: AccelScale,
            val fftViewMode: FftViewMode,
            val fftAxis: FftAxis,
            val fftFrequency: FftFrequency,
        ) : State() {

            sealed class WriteState {

                data class DisplayPreview(
                    val writeRequest: Ble.Accelerometer.WriteRequest
                ) : WriteState()

                data class Writing(
                    val writeRequest: Ble.Accelerometer.WriteRequest
                ) : WriteState()

                object Success : WriteState()

                object Failure : WriteState()

            }

        }

    }

    sealed class Effect : ViewSideEffect {

        object ShowRealtimeViewModeEdit : Effect()

        object ShowPowerPicker : Effect()
        object HidePowerPicker : Effect()

        object ShowWriteBle : Effect()
        object HideWriteBle : Effect()

        object HideHistoryEdit : Effect()

        data class ShowAccelViewEdit(
            val next: Event.Next
        ) : Effect()

        data class ShowAccelScaleEdit(
            val next: Event.Next
        ) : Effect()

        object ShowAccelEdit : Effect()
        object HideAccelEdit : Effect()

        object ShowFftFrequencyEdit : Effect()
        object ShowFftAxisEdit : Effect()
        object ShowFftModeEdit : Effect()


        object HideIntervalPicker : Effect()
        object ShowIntervalPicker : Effect()

        object ShowHistoryEdit : Effect()

        sealed class Navigation : Effect() {

            object NavigateToChangePassword : Navigation()

            data class NavigateToAccelHistory(
                val ble: BleInfo,
                val accelScale: AccelScale,
                val accelMode: AccelViewMode,
                val fftAxis: FftAxis,
                val fftMode: FftViewMode,
                val frequency: FftFrequency
            ) : Navigation()

            data class NavigateToAccelRealtime(
                val ble: BleInfo,
                val accelScale: AccelScale,
                val accelMode: AccelViewMode,
                val fftAxis: FftAxis,
                val fftMode: FftViewMode,
                val frequency: FftFrequency
            ) : Navigation()

            data class NavigateToAccelSpectre(
                val ble: BleInfo,
                val accelScale: AccelScale,
                val accelMode: AccelViewMode,
                val fftAxis: FftAxis,
                val fftMode: FftViewMode,
                val frequency: FftFrequency
            ) : Navigation()

        }

    }

}