package com.develop.food.base.repo.db.dao

import androidx.lifecycle.LiveData
import androidx.room.*
import com.develop.food.base.common.CURRENT_LANGUAGE
import com.develop.food.base.repo.entity.*
import com.develop.food.base.utils.MmkvUtils

@Dao
interface FoodRecipeDao {

    @Query("select * from cc_dev_recipe where lang = :language")
    fun queryAllRecipe(
        language: String = MmkvUtils.getString(CURRENT_LANGUAGE) ?: "EN"
    ): List<DevRecipe>

    @Query("select * from cc_dev_recipe where number = :recipeNumber and lang = :language")
    fun queryRecipe(
        recipeNumber: String,
        language: String = MmkvUtils.getString(CURRENT_LANGUAGE) ?: "EN"
    ): DevRecipe?

    @Query("select * from cc_dev_recipe where recipe_category_number = :categoryNumber and lang = :language ")
    fun queryRecipesByCategory(
        categoryNumber: String,
        language: String = MmkvUtils.getString(CURRENT_LANGUAGE) ?: "EN"
    ): List<DevRecipe>

    @Query("select * from cc_dev_recipe where name LIKE '%' || :name || '%' and lang =:language")
    fun queryRecipeByName(
        name: String,
        language: String = MmkvUtils.getString(CURRENT_LANGUAGE) ?: "EN"
    ): List<DevRecipe>

    @Query("select * from cc_dev_recipe_accessory where recipe_number = :recipeNumber")
    fun queryAccessoryIds(recipeNumber: String): List<DevRecipeAccessory>

    @Query("select * from cc_dev_accessory where number = :accessoryNumber and lang = :language")
    fun queryAccessory(
        accessoryNumber: String,
        language: String = MmkvUtils.getString(CURRENT_LANGUAGE) ?: "EN"
    ): DevAccessory?

    @Query("select * from cc_dev_recipe_category where lang = :language")
    fun queryCategory(
        language: String = MmkvUtils.getString(CURRENT_LANGUAGE) ?: "EN"
    ): List<DevRecipeCategory>

    @Query("select * from cc_dev_recipe_cooking_step where recipe_number = :recipeNumber and lang = :language")
    fun queryCookingStep(
        recipeNumber: String,
        language: String = MmkvUtils.getString(CURRENT_LANGUAGE) ?: "EN"
    ): List<DevRecipeCookingStep>

    @Query("select * from cc_dev_recipe_food where recipe_number = :recipeNumber and lang = :language")
    fun queryFood(
        recipeNumber: String,
        language: String = MmkvUtils.getString(CURRENT_LANGUAGE) ?: "EN"
    ): List<DevRecipeFood>

    @Query("select * from cc_dev_recipe_food where food_name = :foodName and lang = :language")
    fun queryFoodByFoodName(
        foodName: String,
        language: String = MmkvUtils.getString(CURRENT_LANGUAGE) ?: "EN"
    ): List<DevRecipeFood>

    @Query("select * from cc_dev_recipe_nutrition where recipe_number = :recipeNumber and lang = :language")
    fun queryNutrition(
        recipeNumber: String,
        language: String = MmkvUtils.getString(CURRENT_LANGUAGE) ?: "EN"
    ): List<DevRecipeNutrition>

    @Query("select * from cc_dev_recipe_portion_size where recipe_number = :recipeNumber and lang = :language")
    fun queryPortionSize(
        recipeNumber: String,
        language: String = MmkvUtils.getString(CURRENT_LANGUAGE) ?: "EN"
    ): List<DevRecipePortionSize>

    @Query("select * from cc_dev_recipe_rel_tag")
    fun queryRelTag(): LiveData<List<DevRecipeRelTag>>

    @Query("select * from cc_dev_recipe_tag")
    fun queryFoodTag(): LiveData<List<DevRecipeTag>>

    @Delete
    fun deleteRecipe(recipe: DevRecipe)

    @Query("delete from cc_dev_recipe  where number=:recipeNumber")
    fun deleteRecipe(recipeNumber: String)

    /**
     * =======================INSERT METHODS=============================
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertDevRecipes(data: List<DevRecipe>)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertDevAccessorys(data: List<DevAccessory>)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertHotTags(data: List<DevHotTag>)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertDevPortraits(data: List<DevPortrait>)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertDevRecipeAccessorys(data: List<DevRecipeAccessory>)

    @Query("delete from cc_dev_recipe_accessory  where recipe_number=:recipeNumber")
    fun deleteDevRecipeAccessorys(recipeNumber: String);

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertDevRecipeCategorys(data: List<DevRecipeCategory>)


    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertDevRecipeCookingSteps(data: List<DevRecipeCookingStep>)

    @Query("delete from cc_dev_recipe_cooking_step  where recipe_number=:recipeNumber")
    fun deleteDevRecipeCookingSteps(recipeNumber: String)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertDevRecipeFoods(data: List<DevRecipeFood>)

    @Query("delete from cc_dev_recipe_food  where recipe_number=:recipeNumber")
    fun deleteDevRecipeFoods(recipeNumber: String)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertDevRecipeNutritions(data: List<DevRecipeNutrition>)

    @Query("delete from cc_dev_recipe_nutrition  where recipe_number=:recipeNumber")
    fun deleteDevRecipeNutritions(recipeNumber: String)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertDevRecipePortionSizes(data: List<DevRecipePortionSize>)

    @Query("delete from cc_dev_recipe_portion_size  where recipe_number=:recipeNumber")
    fun deleteDevRecipePortionSizes(recipeNumber: String)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertDevRecipeRelTags(data: List<DevRecipeRelTag>)

    @Query("delete from cc_dev_recipe_rel_tag  where recipe_number=:recipeNumber")
    fun deleteDevRecipeRelTags(recipeNumber: String)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertDevRecipeTags(data: List<DevRecipeTag>)


}