package com.aiosman.ravenow.entity

import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import com.aiosman.ravenow.data.ListContainer

abstract class DataLoader<T,ET> {
    var list: MutableList<T> = mutableListOf()
    var page by mutableStateOf(1)
    var total by mutableStateOf(0)
    var pageSize by mutableStateOf(10)
    var hasNext by mutableStateOf(true)
    var isLoading by mutableStateOf(false)
    var error by mutableStateOf<String?>(null)
    var onListChanged: ((List<T>) -> Unit)? = null
    var onError: ((String) -> Unit)? = null
    private var firstLoad = true


    abstract suspend fun fetchData(
        page: Int, pageSize: Int, extra: ET
    ): ListContainer<T>

    suspend fun loadData(
        extra: ET
    ) {
        Log.d("DataLoader", "loadData开始 - firstLoad: $firstLoad")
        if (!firstLoad) {
            Log.d("DataLoader", "loadData跳过 - 非首次加载")
            return
        }
        
        if (isLoading) {
            Log.d("DataLoader", "loadData跳过 - 正在加载中")
            return
        }
        
        firstLoad = false
        isLoading = true
        error = null
        
        try {
            Log.d("DataLoader", "调用fetchData - page: $page, pageSize: $pageSize")
            val result = fetchData(page, pageSize, extra)
            list = result.list.toMutableList()
            this.page = page
            this.total = result.total
            this.pageSize = pageSize
            this.hasNext = result.list.size == pageSize
            Log.d("DataLoader", "loadData完成 - 数据量: ${list.size}, total: $total, hasNext: $hasNext")
            onListChanged?.invoke(list)
        } catch (e: Exception) {
            Log.e("DataLoader", "loadData失败", e)
            error = e.message ?: "加载数据时发生未知错误"
            firstLoad = true // 重置firstLoad状态，允许重试
            onError?.invoke(error!!)
        } finally {
            isLoading = false
        }
    }

    suspend fun loadMore(extra: ET) {
        if (firstLoad) {
            Log.d("DataLoader", "loadMore跳过 - firstLoad为true")
            return
        }
        if (!hasNext) {
            Log.d("DataLoader", "loadMore跳过 - hasNext为false")
            return
        }
        
        if (isLoading) {
            Log.d("DataLoader", "loadMore跳过 - 正在加载中")
            return
        }
        
        isLoading = true
        error = null
        
        try {
            Log.d("DataLoader", "开始loadMore - 当前页: $page, 当前数据量: ${list.size}")
            val result = fetchData(page + 1, pageSize, extra)
            list.addAll(result.list)
            page += 1
            hasNext = result.list.size == pageSize
            Log.d("DataLoader", "loadMore完成 - 新页: $page, 新数据量: ${list.size}, 本次获取: ${result.list.size}, hasNext: $hasNext")
            onListChanged?.invoke(list)
        } catch (e: Exception) {
            Log.e("DataLoader", "loadMore失败", e)
            error = e.message ?: "加载更多数据时发生未知错误"
            onError?.invoke(error!!)
        } finally {
            isLoading = false
        }
    }

    fun clear() {
        list.clear()
        page = 1
        total = 0
        pageSize = 10
        hasNext = true
        firstLoad = true
        isLoading = false
        error = null
    }
}