/*
 * 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.UserData
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

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

    // Backing properties for StateFlows (Private to prevent external modification)
    private val _bannerCardList = MutableStateFlow<List<CardData>>(emptyList())

    // Publicly exposed immutable StateFlows for UI observation
    val bannerCardList: StateFlow<List<CardData>> = _bannerCardList

    private val _fixedGridCardList = MutableStateFlow<List<CardData>>(emptyList())
    val fixedGridCardList: StateFlow<List<CardData>> = _fixedGridCardList

    private val _userList = MutableStateFlow<List<UserData>>(emptyList())
    val userList: StateFlow<List<UserData>> = _userList

    private val _cardList = MutableStateFlow<List<CardData>>(emptyList())
    val cardList: StateFlow<List<CardData>> = _cardList

    init {
        // Initialize data when the ViewModel is created
        loadLocalData()
    }

    /**
     * Simulates fetching data from a local data source or repository. Generates mock data with
     * unique String IDs for better list performance and animations.
     */
    private fun loadLocalData() {
        // 1. Initialize Banner data with index-based String IDs
        val bannerResIds =
            listOf(
                R.drawable.banner_1,
                R.drawable.banner_2,
                R.drawable.banner_3,
                R.drawable.banner_1,
                R.drawable.banner_2,
                R.drawable.banner_3,
                R.drawable.banner_1,
                R.drawable.banner_2,
                R.drawable.banner_3,
            )
        _bannerCardList.value =
            bannerResIds.mapIndexed { index, resId ->
                CardData(id = "banner-$index", imageId = resId)
            }

        // 2. Initialize 2x2 Fixed Grid data
        val fixedGridResIds =
            listOf(
                R.drawable.img_banner_6,
                R.drawable.img_banner_2,
                R.drawable.img_banner_4,
                R.drawable.img_banner_3
            )
        _fixedGridCardList.value =
            fixedGridResIds.mapIndexed { index, resId ->
                CardData(id = "fixed-$index", imageId = resId)
            }

        // 3. Initialize Recommended User list with predefined unique IDs
        _userList.value =
            listOf(
                UserData(
                    id = "user_1",
                    nameRes = R.string.sui_sample_user_name_1,
                    descriptionRes = R.string.sui_sample_user_desc_1,
                    imageId = R.drawable.recommend_user_1,
                    bgId = R.color.user_bg_1,
                    hot = true
                ),
                UserData(
                    id = "user_2",
                    nameRes = R.string.sui_sample_user_name_2,
                    descriptionRes = R.string.sui_sample_user_desc_2,
                    imageId = R.drawable.recommend_user_2,
                    bgId = R.color.user_bg_2,
                    hot = false
                ),
                UserData(
                    id = "user_3",
                    nameRes = R.string.sui_sample_user_name_3,
                    descriptionRes = R.string.sui_sample_user_desc_3,
                    imageId = R.drawable.recommend_user_3,
                    bgId = R.color.user_bg_3,
                    hot = false
                )
            )

        // 4. Initialize the main infinite card list for the LazyVerticalGrid
        val flowCardResIds =
            listOf(
                R.drawable.img_feed_1,
                R.drawable.img_feed_2,
                R.drawable.img_feed_3,
                R.drawable.img_feed_4,
                R.drawable.img_feed_5,
                R.drawable.img_feed_6,
                R.drawable.img_feed_7,
                R.drawable.img_feed_8,
                R.drawable.img_feed_9,
                R.drawable.img_feed_10_new,
                R.drawable.img_feed_11_new,
                R.drawable.img_feed_1
            )
        _cardList.value =
            flowCardResIds.mapIndexed { index, resId ->
                CardData(id = "id_$index", imageId = resId)
            }
    }
}
