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

import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import llc.arma.ble.app.ui.common.BaseViewModel
import llc.arma.ble.domain.usecase.GetBleAroundFlow
import javax.inject.Inject

@HiltViewModel
class BleListViewModel @Inject constructor(
    getBleAroundFlow: GetBleAroundFlow
) : BaseViewModel<BleListContract.State, BleListContract.Event, BleListContract.Effect>() {

    init {

        viewModelScope.launch {

            var job: Job? = null

            getBleAroundFlow().fold(
                onSuccess = {
                    it.onEach {
                        setState {
                            copy(
                                connectedBleList = emptyList(),
                                bleList = it
                            )
                        }
                    }.launchIn(viewModelScope)
                },
                onFailure = {
                    throw IllegalStateException()
                }
            )

            /*while (true) {

                job?.cancel()
                job = getBleAroundFlow().onEach {
                    it.fold(
                        onSuccess = {
                            setState {
                                copy(
                                    connectedBleList = emptyList(),
                                    bleList = it
                                )
                            }
                        },
                        onFailure = {

                        }
                    )

                }.launchIn(viewModelScope)

                delay(30_000)

            }*/

        }

    }

    override fun setInitialState(): BleListContract.State =
        BleListContract.State(emptyList(), emptyList(), BleListContract.State.Filter())

    override fun handleEvents(event: BleListContract.Event) {
        when(event){
            is BleListContract.Event.OnConnectToBle -> reduce(viewState.value, event)
            is BleListContract.Event.OnHideFilter -> reduce(viewState.value, event)
            is BleListContract.Event.OnMacFilterChanged -> reduce(viewState.value, event)
            is BleListContract.Event.OnNameFilterChanged -> reduce(viewState.value, event)
            is BleListContract.Event.OnResetFilter -> reduce(viewState.value, event)
            is BleListContract.Event.OnRssiRangeChanged -> reduce(viewState.value, event)
            is BleListContract.Event.OnShowFilter -> reduce(viewState.value, event)
            is BleListContract.Event.OnTypeChanged -> reduce(viewState.value, event)
            is BleListContract.Event.OnBatteryRangeChanged -> reduce(viewState.value, event)
            is BleListContract.Event.OnSortFieldChanged -> reduce(viewState.value, event)
            is BleListContract.Event.OnSortOrderChanged -> reduce(viewState.value, event)
        }
    }

    private fun reduce(
        state: BleListContract.State,
        event: BleListContract.Event.OnConnectToBle
    ) {
        setEffect {
            BleListContract.Effect.Navigation.NavigateToBle(serial = event.bleAddress)
        }
    }

    private fun reduce(
        state: BleListContract.State,
        event: BleListContract.Event.OnHideFilter
    ) {
        setEffect {
            BleListContract.Effect.HideFilter
        }
    }

    private fun reduce(
        state: BleListContract.State,
        event: BleListContract.Event.OnMacFilterChanged
    ) {
        setState {
            copy(
                filter = this.filter.copy(mac = event.mac)
            )
        }
    }

    private fun reduce(
        state: BleListContract.State,
        event: BleListContract.Event.OnSortOrderChanged
    ) {
        setState {
            copy(
                filter = this.filter.copy(sortOrder = event.order)
            )
        }
    }

    private fun reduce(
        state: BleListContract.State,
        event: BleListContract.Event.OnSortFieldChanged
    ) {
        setState {
            copy(
                filter = this.filter.copy(sortField = event.field)
            )
        }
    }

    private fun reduce(
        state: BleListContract.State,
        event: BleListContract.Event.OnNameFilterChanged
    ) {
        setState {
            copy(
                filter = this.filter.copy(name = event.name)
            )
        }
    }

    private fun reduce(
        state: BleListContract.State,
        event: BleListContract.Event.OnResetFilter
    ) {

        setState {
            copy(
                filter = BleListContract.State.Filter()
            )
        }

        setEffect {
            BleListContract.Effect.HideFilter
        }

    }

    private fun reduce(
        state: BleListContract.State,
        event: BleListContract.Event.OnRssiRangeChanged
    ) {
        setState {
            copy(
                filter = this.filter.copy(rssi = event.rssi)
            )
        }
    }

    private fun reduce(
        state: BleListContract.State,
        event: BleListContract.Event.OnBatteryRangeChanged
    ) {
        setState {
            copy(
                filter = this.filter.copy(battery = event.battery)
            )
        }
    }

    private fun reduce(
        state: BleListContract.State,
        event: BleListContract.Event.OnTypeChanged
    ) {
        setState {
            copy(
                filter = this.filter.copy(bleType = event.type)
            )
        }
    }

    private fun reduce(
        state: BleListContract.State,
        event: BleListContract.Event.OnShowFilter
    ) {
        setEffect {
            BleListContract.Effect.ShowFilter
        }
    }

}