package com.develop.step.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.scopeLife
import com.develop.base.mvvm.BaseViewModel
import com.develop.common.data_repo.FoodDataProvider
import com.develop.common.data_repo.db.DataFactory
import com.develop.common.data_repo.db.FoodContentModel
import com.develop.common.data_repo.db.entity.DevRecipe
import com.develop.common.data_repo.db.getTime
import com.develop.common.tag.CURRENT_USER_ID
import com.develop.common.tag.Downloaded
import com.develop.common.tag.Favourite
import com.develop.common.tag.History
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class FoodListViewModel : BaseViewModel() {
    var foodListLiveData = MutableLiveData<MutableList<FoodContentModel>>()
    var foodListType = Favourite


    fun refreshData() {
        if (foodListType == Downloaded) {
            getOnLineDownloadedFoodList()
        } else {
            getFavoriteOrHistoryFoodList(foodListType)
        }
    }

    private fun getFavoriteOrHistoryFoodList(foodListType: Int) = scopeLife(Dispatchers.IO) {
        val userDao = FoodDataProvider.getUserDatabase().userInfoDao()
        val recipesDao = FoodDataProvider.getDatabase().recipeDao()
        val recipesIdList = mutableListOf<String>()
        val dataList = mutableListOf<FoodContentModel>()
        val favoriteRecipesIds = mutableListOf<String>()
        userDao.queryFavoriteRecipes(CURRENT_USER_ID).forEach {
            if (foodListType == Favourite) {
                recipesIdList.add(it.favoriteRecipesId)
            }
            favoriteRecipesIds.add(it.favoriteRecipesId)
        }
        if (foodListType == History) {
            userDao.queryHistoryRecipes(CURRENT_USER_ID).forEach {
                recipesIdList.add(it.historyRecipesId)
            }
        }
        val recipesList = mutableListOf<DevRecipe>()
        recipesIdList.forEach {
            recipesDao.queryRecipe(it)?.apply {
                recipesList.add(this)
            }
        }
        recipesList.forEach {
            dataList.add(
                FoodContentModel(
                    it.photoPath ?: "",
                    it.name ?: "",
                    3,
                    getTime(it.makeHours ?: 0, it.makeMinutes ?: 0),
                    it.difficultyLevel ?: "",
                    isLike = favoriteRecipesIds.contains(it.number),
                    foodId = it.number ?: "",
                    recipesEdition = it.edition
                )
            )
        }
        foodListLiveData.postValue(dataList)
    }

    private fun getOnLineDownloadedFoodList() = scopeLife(Dispatchers.IO) {
        val dataList = mutableListOf<FoodContentModel>()
        val userDao = FoodDataProvider.getUserDatabase().userInfoDao()
        val recipesDao = FoodDataProvider.getDatabase().recipeDao()
        val recipesIdList = mutableListOf<String>()
        userDao.queryUserOnLineRecipesIds().forEach {
            recipesIdList.add(it.onLineRecipesId)
        }
        val recipesList = mutableListOf<DevRecipe>()
        recipesIdList.forEach {
            recipesDao.queryRecipe(it)?.apply {
                recipesList.add(this)
            }
        }
        dataList.clear()
        recipesList.forEach {
            dataList.add(
                FoodContentModel(
                    it.photoPath ?: "",
                    it.name ?: "",
                    3,
                    getTime(it.makeHours ?: 0, it.makeMinutes ?: 0),
                    it.difficultyLevel ?: "",
                    isLike = false,
                    foodId = it.number ?: "",
                    isNetRecipes = true,
                    hasDownloaded = true,
                    recipesEdition = it.edition
                )
            )
        }
        foodListLiveData.postValue(dataList)
    }
}