import Combine
import GRDB
import GRDBQuery

struct ScrollStateRequest: Queryable {
    // enum Ordering {
    //     case byScore
    //     case byName
    // }

    /// The ordering used by the player request.
    // var ordering: Ordering
    static var defaultValue: [Ribbon] { [] }
    
    func publisher(in appDatabase: AppDatabase) -> AnyPublisher<[Ribbon], Error> {
        // Build the publisher from the general-purpose read-only access
        // granted by `appDatabase.reader`.
        // Some apps will prefer to call a dedicated method of `appDatabase`.
        ValueObservation
            .tracking(fetchValue(_:))
            .publisher(
                in: appDatabase.reader,
                // The `.immediate` scheduling feeds the view right on
                // subscription, and avoids an undesired animation when the
                // application starts.
                scheduling: .immediate)
            .eraseToAnyPublisher()
    }
    
    // This method is not required by Queryable, but it makes it easier
    func fetchValue(_ db: Database) throws -> [Ribbon] {
        var ret =  try Ribbon.fetchAll(db, sql: "SELECT * FROM ScrollState LIMIT 1") // [Player]
        // print(ret)
        return ret
    }
}
