package com.develop.common.data_repo.db

import android.content.res.Resources
import com.develop.common.R
import com.develop.common.data_repo.db.entity.DevRecipe
import com.develop.common.data_repo.db.entity.UserFavoriteRecipes
import com.develop.common.widget.EntranceData
import com.develop.common.widget.EntranceType


object DataFactory {

    fun createLanguageData(resource: Resources): MutableList<LanguageModel> {
        val languageList = mutableListOf<LanguageModel>()
        val strList = mutableListOf<String>(
            resource.getString(R.string.english),
//            resource.getString(R.string.chinese),
            resource.getString(R.string.france),
//            resource.getString(R.string.japan)
        )
        val iconList = mutableListOf<Int>(
            R.drawable.ic_english,
//            R.drawable.ic_chinese,
            R.drawable.ic_france,
//            R.drawable.ic_japan
        )
        val typeList = mutableListOf<LanguageType>(
            LanguageType.English,
//            LanguageType.Chinese,
            LanguageType.France,
//            LanguageType.Japan
        )
        for (i in strList.indices) {
            languageList.add(LanguageModel(iconList[i], strList[i], typeList[i]))
        }

        return languageList
    }

    val modesTypeList = mutableListOf(
        ModesType.ADAPTED_COOKING,
        ModesType.WIGHT,
        ModesType.BOIL_WATER,
        ModesType.CHOP,
        ModesType.SLOW_COOK,
        ModesType.KNEAD_TOUGH,
        ModesType.STEAM,
        ModesType.FOOD_PROCESSOR,
        ModesType.TURBO
    )

    fun genGridModesList(resource: Resources): MutableList<ModelsModel> {
        val result = mutableListOf<ModelsModel>()
        val resIdList = mutableListOf(
            R.drawable.ic_adapted_cooking,
            R.drawable.ic_scales,
            R.drawable.ic_boil_water,
            R.drawable.ic_chop,
            R.drawable.ic_slow_cook,
            R.drawable.ic_knead_dough,
            R.drawable.ic_steam,
            R.drawable.ic_food_processor,
            R.drawable.ic_turbo
        )
        val modeNameList = mutableListOf(
            resource.getString(R.string.adapted_cooking),
            resource.getString(R.string.wight),
            resource.getString(R.string.boil_water),
            resource.getString(R.string.chop),
            resource.getString(R.string.slow_cook),
            resource.getString(R.string.knead_dough),
            resource.getString(R.string.steam),
            resource.getString(R.string.food_processor),
            resource.getString(R.string.turbo)
        )

        for (i in resIdList.indices) {
            result.add(ModelsModel(resIdList[i], modeNameList[i], modesTypeList[i]))
        }
        return result
    }

    fun genGalleryModesList(resource: Resources): MutableList<ModelsModel> {
        val result = mutableListOf<ModelsModel>()
        val resIdList = mutableListOf(
            R.drawable.ic_grid_adapted_cooking,
            R.drawable.ic_grid_weight,
            R.drawable.ic_grid_boil_water,
            R.drawable.ic_grid_chop,
            R.drawable.ic_grid_slow_cook,
            R.drawable.ic_grid_knead_dough,
            R.drawable.ic_grid_steam,
            R.drawable.ic_grid_food_processor,
            R.drawable.ic_grid_turbo
        )
        val modeNameList = mutableListOf(
            resource.getString(R.string.adapted_cooking),
            resource.getString(R.string.wight),
            resource.getString(R.string.boil_water),
            resource.getString(R.string.chop),
            resource.getString(R.string.slow_cook),
            resource.getString(R.string.knead_dough),
            resource.getString(R.string.steam),
            resource.getString(R.string.food_processor),
            resource.getString(R.string.turbo)
        )
        for (i in resIdList.indices) {
            result.add(ModelsModel(resIdList[i], modeNameList[i], modesTypeList[i]))
        }
        return result
    }

    fun genTestNewsList(): MutableList<NewsModel> {
        val result = mutableListOf<NewsModel>()
        for (i in 0..4) {
            result.add(
                NewsModel(
                    "Good News!",
                    "The scattergun approach to marketing means that the campaign is not targeted at particular individuals.",
                    false
                )
            )
        }
        return result
    }

    fun genSettingModesList(resource: Resources): MutableList<SettingModel> {
        val result = mutableListOf<SettingModel>()
        val resIdList = mutableListOf(
            R.drawable.ic_setting_language,
            R.drawable.ic_setting_wifi,
//            R.drawable.ic_setting_voice,
            R.drawable.ic_setting_brightness,
            R.drawable.ic_setting_user,
//            R.drawable.ic_setting_folder,
            R.drawable.ic_setting_reset,
            R.drawable.ic_setting_about,

            )
        val nameList = mutableListOf(
            resource.getString(R.string.language),
            resource.getString(R.string.wifi),
//            resource.getString(R.string.sound),
            resource.getString(R.string.brightness),
            resource.getString(R.string.user_account),
//            resource.getString(R.string.storage),
            resource.getString(R.string.reset),
            resource.getString(R.string.about)

        )
        val settingTypeList = mutableListOf(
            SettingType.LANGUAGE,
            SettingType.WIFI,
//            SettingType.SOUND,
            SettingType.BRIGHTNESS,
            SettingType.USER_ACCOUNT,
//            SettingType.STORAGE,
            SettingType.RESTORE_FACTORY_SETTINGS,
            SettingType.ABOUT
        )
        for (i in resIdList.indices) {
            result.add(SettingModel(resIdList[i], nameList[i], settingTypeList[i]))
        }
        return result
    }

    fun genLocalRecipes(
        categoryName: String,
        recipesList: MutableList<DevRecipe>,
        favoriteRecipes: MutableList<UserFavoriteRecipes>,
        sortedType: SortedType
    ): MutableList<Any> {
        val result = mutableListOf<Any>()
        result.add(FilterSortModel(""))
        result.add(categoryName)
        val favoriteRecipesIds = mutableListOf<String>()
        favoriteRecipes.forEach {
            favoriteRecipesIds.add(it.favoriteRecipesId)
        }
        val foodList = mutableListOf<FoodContentModel>()
        for (i in recipesList.indices) {
            val recipes = recipesList[i]
            foodList.add(
                FoodContentModel(
                    recipes.photoPath ?: "",
                    recipes.name ?: "",
                    recipes.score?.toInt() ?: 0,
                    getTime(recipes.makeHours ?: 0, recipes.makeMinutes ?: 0),
                    recipes.difficultyLevel ?: "",
                    isLike = favoriteRecipesIds.contains(recipes.number),
                    foodId = recipes.number ?: "",
                    isNetRecipes = false,
                    hasDownloaded = false,
                    recipesEdition = recipes.edition,
                    useNum = recipes.useNum?.toInt() ?: 0
                )
            )
        }
        result.add(FoodListModel(foodList, categoryName))
        return result
    }

    fun genSortNameList(resource: Resources): MutableList<FilterSortModel> {
        val result = mutableListOf<FilterSortModel>()
        val sortNameList = mutableListOf(
            "A-Z",
            resource.getString(R.string.most_popular),
            resource.getString(R.string.newest),
            "rating"
        )
        for (name in sortNameList) {
            result.add(FilterSortModel(name))
        }
        return result
    }

    fun genTestSearchDataList(): MutableList<Any> {
        val result = mutableListOf<Any>()
        result.add(FilterSortModel(""))
        val searchNameList =
            mutableListOf("Guacamole", "Aioli", "Popular", "Chinese food", "Healthy")
        val searchModelList = mutableListOf<CommonSearchItem>()
        searchNameList.forEach {
            searchModelList.add(CommonSearchItem(it))
        }
        result.add(CommonSearchModel(searchModelList))
        return result
    }

    fun genOnLineHotTags(hotTags: MutableList<String>): MutableList<Any> {
        val result = mutableListOf<Any>()
        result.add(FilterSortModel(""))
        val searchModelList = mutableListOf<CommonSearchItem>()
        hotTags.forEach {
            searchModelList.add(CommonSearchItem(it))
        }
        result.add(CommonSearchModel(searchModelList))
        return result
    }


    fun genNetRecipesData(
        categoryName: String,
        recipesList: MutableList<DevRecipe>,
        favoriteRecipes: MutableList<UserFavoriteRecipes>,
        sortedType: SortedType
    ): MutableList<Any> {
        val result = mutableListOf<Any>()
        result.add(FilterSortModel("", isOnLineState = true))
        result.add(categoryName)
        val favoriteRecipesIds = mutableListOf<String>()
        favoriteRecipes.forEach {
            favoriteRecipesIds.add(it.favoriteRecipesId)
        }
        recipesList.sortWith(Comparator { t, t2 ->
            when (sortedType) {
                SortedType.Popular -> {
                    if ((t.useNum ?: 0) > (t2.useNum ?: 0)) {
                        return@Comparator -1
                    } else {
                        return@Comparator 1
                    }
                }

                SortedType.Newest -> {
                    if ((t.updateTime ?: 0L) > (t2.updateTime ?: 0L)) {
                        return@Comparator 1
                    } else {
                        return@Comparator -1
                    }

                }

                SortedType.Score -> {
                    val score1 = t.score?.toInt() ?: 0
                    val score2 = t2.score?.toInt() ?: 0
                    if (score1 > score2) {
                        return@Comparator 1
                    } else {
                        return@Comparator -1
                    }
                }

                SortedType.A2Z -> {
                    val score1 = t.name?.compareTo(t2.name ?: "") ?: 0
                    if (score1 > 0) {
                        return@Comparator 1
                    } else if (score1 == 0) {
                        return@Comparator 0
                    } else {
                        return@Comparator -1
                    }
                }
            }
        })
        val foodList = mutableListOf<FoodContentModel>()
        for (i in recipesList.indices) {
            val recipes = recipesList[i]
            foodList.add(
                FoodContentModel(
                    recipes.photoPath ?: "",
                    recipes.name ?: "",
                    recipes.score?.toInt() ?: 0,
                    getTime(recipes.makeHours ?: 0, recipes.makeMinutes ?: 0),
                    recipes.difficultyLevel ?: "",
                    isLike = favoriteRecipesIds.contains(recipes.number),
                    foodId = recipes.number ?: "",
                    isNetRecipes = true,
                    hasDownloaded = false,
                    recipesEdition = recipes.edition,
                    useNum = recipes.useNum?.toInt() ?: 0
                )
            )
        }
        result.add(FoodListModel(foodList, categoryName))
        return result
    }

    private var bgResIds = arrayListOf<Int>(
        R.drawable.entrance_bg_1,
        R.drawable.entrance_bg_2,
        R.drawable.entrance_bg_3,
        R.drawable.entrance_bg_4
    )
    private var iconResIds = arrayListOf<Int>(
        R.drawable.entrance_icon_1,
        R.drawable.entrance_icon_2,
        R.drawable.entrance_icon_3,
        R.drawable.entrance_icon_4
    )
    private var titleStrIds = mutableListOf<Int>(
        R.string.automatic_recipes,
        R.string.automatic_programmes,
        R.string.manual_cooking,
        R.string.download_new_recipes
    )
    private var title2StrIds = mutableListOf<Int>(
        R.string.Automatic_recipes,
        R.string.Automatic_programmes,
        R.string.Manual_cooking,
        R.string.Download_new_recipes
    )


    private var typeList = mutableListOf<EntranceType>(
        EntranceType.automatic_recipes,
        EntranceType.automatic_programmes,
        EntranceType.manual_cooking,
        EntranceType.download_new_recipes
    )

    fun getEntranceData(): MutableList<EntranceData> {
        val dataList = mutableListOf<EntranceData>()
        for (i in iconResIds.indices) {
            dataList.add(
                EntranceData(
                    bgResIds[i],
                    iconResIds[i],
                    titleStrIds[i],
                    title2StrIds[i],
                    typeList[i]
                )
            )
        }
        return dataList
    }
}

fun getTime(hour: Int, minute: Int): String {
    var hourStr = ""
    var minuteStr = ""
    if (hour != 0) {
        hourStr = "${hour}hour"
    }
    if (minute != 0) {
        minuteStr = "${minute}minute"
    }
    return "$hourStr$minuteStr"
}