package ru.myitschool.work.ui.main

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import ru.myitschool.work.api.list.ListInfo
import ru.myitschool.work.api.main.ApiServiceMain
import ru.myitschool.work.api.main.UserInfo
import ru.myitschool.work.ui.list.ListRepository
import javax.inject.Inject

@HiltViewModel
class MainViewModel @Inject constructor(
    private val apiService: ApiServiceMain,
    private val listRepository: ListRepository
) : ViewModel() {

    private val _state = MutableStateFlow<MainState>(MainState.Initial)
    val state = _state.asStateFlow()

    fun getUserData(login: String) {
        viewModelScope.launch {
            _state.value = MainState.Loading


            apiService.getDataUser(login).enqueue(object : Callback<UserInfo> {
                override fun onResponse(call: Call<UserInfo>, response: Response<UserInfo>) {

                    when {
                        response.isSuccessful -> {
                            val userInfo = response.body() ?: UserInfo.Initial
                            _state.value = MainState.Success(userInfo)
                        }
                        response.code() == 401 -> {
                            _state.value = MainState.Error("Unauthorized")
                        }
                        else -> {
                            _state.value = MainState.Error("Error")
                        }
                    }
                }

                override fun onFailure(call: Call<UserInfo>, t: Throwable) {
                    Log.e(TAG, "Network Error", t)
                    _state.value = MainState.Error("Network Error")
                }
            })
        }
    }

    private val _listState = MutableStateFlow<ListState>(ListState.Loading)
    val listState: StateFlow<ListState> = _listState

    sealed class ListState {
        data object Loading : ListState()
        data class Success(val data: List<ListInfo>) : ListState()
        data class Error(val message: String) : ListState()
    }

    fun loadList(login: String) {
        viewModelScope.launch {
            _listState.value = ListState.Loading
            try {
                val list = listRepository.getList(login)
                _listState.value = ListState.Success(list)
            } catch (e: Exception) {
                _listState.value = ListState.Error(e.message ?: "Unknown error")
            }
        }
    }

    sealed class MainState {
        data object Initial : MainState()
        data object Loading : MainState()
        data class Success(val userInfo: UserInfo) : MainState()
        data class Error(val message: String) : MainState()
    }

    companion object {
        private const val TAG = "MainViewModel"
    }
}