package com.develop.food.base.data


import android.content.res.Resources
import com.develop.base.R
import com.develop.food.base.data.model.CommonSearchItem
import com.develop.food.base.data.model.CommonSearchModel
import com.develop.food.base.data.model.FilterSortModel
import com.develop.food.base.data.model.FoodContentModel
import com.develop.food.base.data.model.HomeBannerListModel
import com.develop.food.base.data.model.HomeBannerModel
import com.develop.food.base.data.model.LanguageModel
import com.develop.food.base.data.model.ModelsModel
import com.develop.food.base.data.model.NewsModel
import com.develop.food.base.data.model.SettingModel
import com.develop.food.base.repo.entity.DevRecipe
import com.develop.food.base.repo.entity.UserFavoriteRecipes
import com.develop.food.base.utils.ConfigUtils
import com.develop.food.base.utils.GlobalApp
import com.develop.food.base.utils.MmkvUtils
import com.develop.food.base.utils.isNightTheme
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import java.lang.reflect.Type
import java.util.Collections

object DataFactory {

    var dataInvalid = false

    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
    }


    fun createLanguageDataV2(resource: Resources): List<ConfigUtils.Lang> {
        return ConfigUtils.loadLangConfig()?.langs?.filter { it.show == true }
            ?: Collections.emptyList();
    }

    fun genNetRecipesData(
        categoryName: String,
        recipesList: MutableList<DevRecipe>,
        favoriteRecipes: MutableList<UserFavoriteRecipes>,
        starList: MutableList<Int>,
        sortedType: SortedType
    ): MutableList<Any> {
        val result = mutableListOf<Any>()
        result.add(FilterSortModel(""))
        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
                    }
                }
            }
        })

        for (i in recipesList.indices) {
            val recipes = recipesList[i]
            result.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
                )
            )
        }
        return result
    }

    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_night_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.ferment),
            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)
        )
        val modesTypeList = mutableListOf(
            ModesType.ADAPTED_COOKING,
            ModesType.WIGHT,
            ModesType.BOIL_WATER,
            ModesType.FERMENT,
            ModesType.SLOW_COOK,
            ModesType.KNEAD_TOUGH,
            ModesType.STEAM,
            ModesType.FOOD_PROCESSOR,
            ModesType.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.ferment),
            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)
        )
        val modesTypeList = mutableListOf(
            ModesType.ADAPTED_COOKING,
            ModesType.WIGHT,
            ModesType.BOIL_WATER,
            ModesType.FERMENT,
            ModesType.SLOW_COOK,
            ModesType.KNEAD_TOUGH,
            ModesType.STEAM,
            ModesType.FOOD_PROCESSOR,
            ModesType.TURBO
        )
        for (i in resIdList.indices) {
            result.add(ModelsModel(resIdList[i], modeNameList[i], modesTypeList[i]))
        }
        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
        )

        var tuyaConfig =  ConfigUtils.loadTuyaConfig()
        if(tuyaConfig != null){
            settingTypeList.add(SettingType.TUYA)
            nameList.add(resource.getString(R.string.tuya))
            resIdList.add(R.drawable.ic_setting_tuya)
        }

        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>,
        starList: MutableList<Int>,
        sortedType: SortedType
    ): MutableList<Any> {
        val result = mutableListOf<Any>()
        val bannerList = mutableListOf<HomeBannerModel>()
        if (isNightTheme()) {
            for (i in 0 until 3) {
                bannerList.add(HomeBannerModel(R.drawable.ic_home_banner, ""))
            }
        } else {
            for (i in 0 until 3) {
                bannerList.add(HomeBannerModel(R.drawable.ic_auth_banner, ""))
            }
        }

        result.add(HomeBannerListModel(bannerList))
        result.add(FilterSortModel(""))
        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 if (score1 == score2) {
                        return@Comparator 0
                    } 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
                    }
                }
            }
        })
        for (i in recipesList.indices) {
            val recipes = recipesList[i]
            result.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
                )
            )
        }
        return result
    }

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

    fun genTestSearchDataList(): MutableList<Any> {
        val result = mutableListOf<Any>()
        result.add(FilterSortModel(""))
        var historyDataList: MutableList<CommonSearchItem> = mutableListOf()
        val hotWordJson = MmkvUtils.getString("CommonWords") ?: ""
        if (hotWordJson.isNotEmpty()) {
            val type: Type = object : TypeToken<List<CommonSearchItem>>() {}.type
            historyDataList = Gson().fromJson(hotWordJson, type)
        }
        historyDataList.sortWith(Comparator { t, t2 ->
            if (t.count > (t2.count ?: 0)) {
                return@Comparator -1
            } else if ((t.count ?: 0) == (t2.count ?: 0)) {
                return@Comparator 0
            } else {
                return@Comparator 1
            }
        })
        val searchModel = if (historyDataList.size > 12) {
            CommonSearchModel(historyDataList.subList(0, 12))
        } else {
            CommonSearchModel(historyDataList)
        }
        result.add(searchModel)
        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, 1))
        }
        result.add(CommonSearchModel(searchModelList))
        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 getTime(hour: Int, minute: Int): String {
        var hourStr = ""
        var minuteStr = ""
        if (hour != 0) {
            hourStr = "${hour}${GlobalApp().getString(R.string.hour)}"
        }
        if (minute != 0) {
            minuteStr = "${minute}${GlobalApp().getString(R.string.minute_lower)}"
        }
        return "$hourStr$minuteStr"
    }

    fun  getModeType(type :String?) : ModesType{
        return when (type) {
           "FERMENT"-> {
               ModesType.FERMENT
            }
            "CHOP" -> {
                ModesType.CHOP
            }
            "STEAM" -> {
                ModesType.STEAM
            }
            "FOOD_PROCESSOR" -> {
                ModesType.FOOD_PROCESSOR
            }
            "BOIL_WATER" -> {
                ModesType.BOIL_WATER
            }
            "ADAPTED_COOKING" -> {
                ModesType.ADAPTED_COOKING
            }
            "WIGHT" -> {
                ModesType.WIGHT
            }
            "SLOW_COOK" -> {
                ModesType.SLOW_COOK
            }
           "KNEAD_TOUGH" -> {
                ModesType.KNEAD_TOUGH
            }
            "TURBO" -> {
                ModesType.TURBO
            }
            else -> {
                ModesType.ADAPTED_COOKING
            }
        }

    }

    fun getConfigListIcon(icon: String?): Int {
        return when (icon) {
            "ic_grid_adapted_cooking" -> {
                R.drawable.ic_grid_adapted_cooking
            }

            "ic_grid_knead_dough" -> {
                R.drawable.ic_grid_knead_dough
            }
            "ic_grid_steam" -> {
                R.drawable.ic_grid_steam
            }
            "ic_grid_slow_cook" -> {
                R.drawable.ic_grid_slow_cook
            }
            "ic_grid_boil_water" -> {
                R.drawable.ic_grid_boil_water
            }
            "ic_grid_food_processor" -> {
                R.drawable.ic_grid_food_processor
            }
            "ic_grid_chop" -> {
                R.drawable.ic_grid_chop
            }
            "ic_grid_turbo" -> {
                R.drawable.ic_grid_turbo
            }
           "ic_grid_weight" -> {
                R.drawable.ic_grid_weight
            }

            else -> {
                R.drawable.ic_grid_adapted_cooking
            }
        }
    }
    fun getConfigGridListIcon(icon: String?): Int {
        return when (icon) {
            "ic_adapted_cooking" -> {
                R.drawable.ic_adapted_cooking
            }

            "ic_knead_dough" -> {
                R.drawable.ic_knead_dough
            }
            "ic_steam" -> {
                R.drawable.ic_steam
            }
            "ic_slow_cook" -> {
                R.drawable.ic_slow_cook
            }
            "ic_boil_water" -> {
                R.drawable.ic_boil_water
            }
            "ic_food_processor" -> {
                R.drawable.ic_food_processor
            }
            "ic_chop" -> {
                R.drawable.ic_chop
            }
            "ic_night_turbo" -> {
                R.drawable.ic_night_turbo
            }
            "ic_scales" -> {
                R.drawable.ic_scales
            }

            else -> {
                R.drawable.ic_grid_adapted_cooking
            }
        }
    }

}


enum class LanguageType {
    English,
    Chinese,
    France,
    Japan,
    PL,
}

enum class FunType {
    Modes,
    LocalRecipes,
    NetRecipes
}

enum class ModesType {
    ADAPTED_COOKING,
    WIGHT,
    BOIL_WATER,
    CHOP,
    FERMENT,
    SLOW_COOK,
    KNEAD_TOUGH,
    STEAM,
    FOOD_PROCESSOR,
    TURBO
}

enum class SettingType {
    LANGUAGE,
    WIFI,
    SOUND,
    BRIGHTNESS,
    USER_ACCOUNT,
    STORAGE,
    ABOUT,
    RESTORE_FACTORY_SETTINGS,
    TUYA
}

enum class CategoryType {
    All,
    Recipes,
    Ingredients
}

enum class SortedType {
    Popular,
    Newest,
    Score,
    A2Z
}


fun getHistoryWords(): MutableList<String> {
    var historyDataList: MutableList<String> = mutableListOf()
    val hotWordJson = MmkvUtils.getString("HistoryWords") ?: ""
    if (hotWordJson.isNotEmpty()) {
        val type: Type = object : TypeToken<List<String>>() {}.type
        historyDataList = Gson().fromJson(hotWordJson, type)
    }
    return historyDataList
}
