/*
 * Copyright 2025 - 2026 PICO. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.pico.spatialui.sample.viewmodel

import androidx.lifecycle.ViewModel
import com.pico.spatialui.sample.R
import com.pico.spatialui.sample.model.CardData
import com.pico.spatialui.sample.model.Category
import com.pico.spatialui.sample.model.Suggestion
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

/**
 * ViewModel responsible for managing and providing data for the SearchPage. It uses StateFlow to
 * expose data in a lifecycle-aware and reactive manner.
 */
class SearchViewModel : ViewModel() {

    private val _searchKeyRecommendedList = MutableStateFlow<List<Int>>(emptyList())
    val searchKeyRecommendedList: StateFlow<List<Int>> = _searchKeyRecommendedList

    private val _suggestionList = MutableStateFlow<List<Suggestion>>(emptyList())
    val suggestionList: StateFlow<List<Suggestion>> = _suggestionList

    private val _categoryList = MutableStateFlow<List<Category>>(emptyList())
    val categoryList: StateFlow<List<Category>> = _categoryList

    private val _searchResultList = MutableStateFlow<List<CardData>>(emptyList())
    val searchResultList: StateFlow<List<CardData>> = _searchResultList

    private val _searchText = MutableStateFlow("")
    val searchText: StateFlow<String> = _searchText

    init {
        loadLocalData()
    }

    fun onSearchTextChange(text: String) {
        _searchText.value = text
    }

    fun removeKeyFromRecommendedList(key: Int) {
        _searchKeyRecommendedList.value = _searchKeyRecommendedList.value.filter { it != key }
    }

    fun toggleCategorySelection(targetNameRes: Int) {
        _categoryList.value =
            _categoryList.value.map { category ->
                if (category.nameRes == targetNameRes) {
                    category.copy(isSelected = !category.isSelected)
                } else {
                    category
                }
            }
    }

    private fun loadLocalData() {
        _searchKeyRecommendedList.value =
            listOf(
                    R.string.sui_sample_search_keyword_pico_new_gameplay,
                    R.string.sui_sample_search_keyword_national_day_hot_activity,
                    R.string.sui_sample_search_keyword_national_day_holiday,
                    R.string.sui_sample_search_keyword_pico_design_system,
                    R.string.sui_sample_search_keyword_developer_platform,
                    R.string.sui_sample_search_keyword_removable_chip
                )
                .distinct()

        _suggestionList.value =
            listOf(
                    Suggestion(R.string.sui_sample_search_keyword_pico_design_system, true),
                    Suggestion(R.string.sui_sample_search_suggestion_design_guide, true),
                    Suggestion(
                        R.string.sui_sample_search_keyword_national_day_niche_tourist_attractions,
                        true
                    ),
                    Suggestion(R.string.sui_sample_search_keyword_national_day_holiday, false),
                    Suggestion(R.string.sui_sample_search_suggestion_spatial_ui, false),
                    Suggestion(R.string.sui_sample_search_suggestion_spatial_ui_tutorial, false),
                    Suggestion(
                        R.string.sui_sample_search_suggestion_mom_retired_study_abroad,
                        false
                    )
                )
                .distinctBy(Suggestion::valueRes)

        _categoryList.value =
            listOf(
                    Category(R.string.sui_sample_search_category_pico, false),
                    Category(R.string.sui_sample_search_suggestion_spatial_ui, true),
                    Category(R.string.sui_sample_search_keyword_pico_design_system, false),
                    Category(R.string.sui_sample_search_category_ui_kit, false),
                    Category(R.string.sui_sample_search_keyword_developer_platform, true)
                )
                .distinctBy(Category::nameRes)

        val searchResultResIds =
            listOf(
                R.drawable.img_banner_1,
                R.drawable.img_banner_2,
                R.drawable.img_banner_3,
                R.drawable.img_banner_4,
                R.drawable.img_banner_5,
                R.drawable.img_banner_6
            )

        _searchResultList.value =
            searchResultResIds.mapIndexed { index, resId ->
                CardData(id = "id_$index", imageId = resId)
            }
    }
}
