package com.iscs.bozzys.ui.pages.vm

import androidx.compose.ui.graphics.Color
import androidx.lifecycle.viewModelScope
import com.iscs.bozzys.R
import com.iscs.bozzys.ui.base.VMBase
import com.iscs.bozzys.ui.theme.Text
import com.iscs.bozzys.utils.Storage
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch

class VMHome : VMBase() {

    private val _state = MutableStateFlow(StateHome())
    val state = _state.asStateFlow()

    fun init() {
        viewModelScope.launch {
            val navs = listOf(
                NavBarItem(0, "首页", R.drawable.home),
                NavBarItem(1, "作业", R.drawable.jobs),
                NavBarItem(2, "消息", R.drawable.message),
                NavBarItem(3, "设置", R.drawable.settings)
            )
            _state.value = _state.value.copy(navs = navs, username = Storage.readUserName())
        }
    }

    fun updateNavIndex(navIndex: Int) {
        _state.value = _state.value.copy(navIndex = navIndex)
    }

    fun updateUnReadCount() {
        // 更新未读消息数演示
        val navs = _state.value.navs.map { it.copy(count = 12) }
        _state.value = _state.value.copy(navs = ArrayList(navs))
    }

}

/**
 * @param navs      当前底部的导航列表
 * @param navIndex  当前选中的底部导航
 * @param username  用户名
 */
data class StateHome(val navs: List<NavBarItem> = arrayListOf(), var navIndex: Int = 0, var username: String = "")

/**
 * 底部导航栏对象
 *
 * @param id                用于标记当前
 * @param title             导航栏名称
 * @param icon              图标
 * @param count             未读消息数
 * @param selectedColor     选中的颜色
 * @param unselectedColor   未选中的颜色
 */
data class NavBarItem(
    val id: Int = 0,
    val title: String = "",
    val icon: Int = 0,
    val count: Int = 0,
    val selectedColor: Color = Color(0xFF1E90FF),
    val unselectedColor: Color = Text
)