package com.aiosman.riderpro

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel

class LikeDataSource : MockDataSource<LikeItemData>() {
    init {
        var newList = mutableListOf<LikeItemData>()
        for (i in 0..999) {
            newList.add(LikeItemData(i, "Like $i"))
        }
        list = newList
    }
}
abstract class DataLoader<T> {
    var list = mutableListOf<T>()
    var page by mutableStateOf(1)
    var pageSize by mutableStateOf(20)
    var total by mutableStateOf(0)
    var noMoreData by mutableStateOf(false)

    suspend fun loadData() {
        val resp = fetchData(page, pageSize)
        if (resp.success) {
            resp.data?.let {
                total = it.total
                list = it.list.toMutableList()
                if (it.list.size < pageSize) {
                    noMoreData = true
                }
            }
        }
    }
    suspend fun loadMore(){
        if (list.size >= total) return
        page++
        val resp = fetchData(page, pageSize)
        if (resp.success) {
            resp.data?.let {
                total = it.total
                list.addAll(it.list)
                if (it.list.size < pageSize) {
                    noMoreData = true
                }
            }
        }
    }
    abstract suspend fun fetchData(page: Int, pageSize: Int): MockDataContainer<MockListContainer<T>>
}
class LikeListLoader : DataLoader<LikeItemData>() {
    var mockDataSource = LikeDataSource()
    override suspend fun fetchData(page: Int, pageSize: Int): MockDataContainer<MockListContainer<LikeItemData>> {
        return mockDataSource.fetchData(page, pageSize)
    }
}
object LikePageViewModel : ViewModel() {
    val loader = LikeListLoader()
}

data class LikeItemData(
    val id: Int,
    val name: String,
)