package ru.myitschool.work.ui.screen.room

import android.util.Log
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.navigation.toRoute
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import ru.myitschool.work.data.repo.AuthRepository
import ru.myitschool.work.data.repo.RoomBookingRepository
import ru.myitschool.work.domain.room.DeleteRoomBookingsUseCase
import ru.myitschool.work.domain.room.GetRoomBookingsUseCase
import ru.myitschool.work.domain.room.SendRoomBookingRequestUseCase
import ru.myitschool.work.ui.nav.RoomScreenDestination


class RoomViewModel(
    savedStateHandle: SavedStateHandle,
): ViewModel() {

    private val roomScreen = savedStateHandle.toRoute<RoomScreenDestination>()

    private val roomBookingRepository = RoomBookingRepository(
        AuthRepository
    )
    private val getRoomBookingsDataUseCase by lazy {
        GetRoomBookingsUseCase(
            RoomBookingRepository(
                AuthRepository
            )
        )
    }
    private val deleteRoomBookingRequestUseCase by lazy {
        DeleteRoomBookingsUseCase(
            roomBookingRepository
        )
    }

    private val sendRoomBookingRequestUseCase by lazy {
        SendRoomBookingRequestUseCase(
            roomBookingRepository
        )
    }
    private val _uiState = MutableStateFlow<RoomState>(RoomState.Loading)
    val uiState: StateFlow<RoomState> = _uiState.asStateFlow()

    init {
        refresh(roomScreen.roomId)
//        Timer().schedule(object : TimerTask() {
//            override fun run() {
//                Log.d("mytimer", "A Kiss every 5 seconds")
//            }
//        }, 0, 5000)
    }

    fun onIntent(intent: RoomIntent) {
        when (intent) {
            is RoomIntent.Refresh -> {
                refresh(intent.placeId)
            }

            is RoomIntent.Booking -> {
                viewModelScope.launch {
                    sendRoomBookingRequestUseCase.invoke(
                        intent.placeId,
                        intent.date
                    ).fold(
                        onSuccess = {
                            refresh(intent.placeId)
                        },
                        onFailure = { error ->
                            RoomState.Error(
                                error = error.message.orEmpty()
                            )
                        }
                    )
                }
            }

            is RoomIntent.UnBook -> {
                viewModelScope.launch {
                    deleteRoomBookingRequestUseCase.invoke(
                        intent.placeId,
                        intent.date
                    ).fold(
                        onSuccess = {
                            refresh(intent.placeId)
                        },
                        onFailure = { error ->
                            RoomState.Error(
                                error = error.message.orEmpty()
                            )
                        }
                    )
                }
            }
        }
    }

    private fun refresh(roomId: Int) {
        viewModelScope.launch {
            _uiState.update { RoomState.Loading }
            _uiState.update {
                getRoomBookingsDataUseCase.invoke(roomId = roomId).fold(
                    onSuccess = { data ->
                        RoomState.Data(
                            data = data.data,
                            name = data.name
                        )
                    },
                    onFailure = { error ->
                        RoomState.Error(
                            error = error.message.orEmpty()
                        )
                    }
                )
            }
        }
    }
}