package com.zyzf.coffeepreorder.database

import android.content.Context
import com.zyzf.coffeepreorder.api.MyServerService
import com.zyzf.coffeepreorder.api.coffee.RestCoffeeRepository
import com.zyzf.coffeepreorder.api.user.RestUserRepository
import com.zyzf.coffeepreorder.database.repository.CartRepository
import com.zyzf.coffeepreorder.database.repository.OfflineCartRepository
import com.zyzf.coffeepreorder.database.repository.OfflineCoffeeRepository
import com.zyzf.coffeepreorder.database.repository.OfflineRemoteKeyRepository
import com.zyzf.coffeepreorder.database.repository.OfflineUserRepository

interface AppContainer {
    val cartRepository: CartRepository
    val coffeeRestRepository: RestCoffeeRepository
    val userRestRepository: RestUserRepository

    companion object {
        const val LIMIT = 10
    }
}

class AppDataContainer(private val context: Context) : AppContainer {
    private val coffeeRepository: OfflineCoffeeRepository by lazy {
        OfflineCoffeeRepository(AppDatabase.getInstance(context).coffeeDao())
    }
    private val userRepository: OfflineUserRepository by lazy {
        OfflineUserRepository(AppDatabase.getInstance(context).userDao())
    }
    override val cartRepository: CartRepository by lazy {
        OfflineCartRepository(AppDatabase.getInstance(context).cartDao())
    }
    private val remoteKeyRepository: OfflineRemoteKeyRepository by lazy {
        OfflineRemoteKeyRepository(AppDatabase.getInstance(context).remoteKeysDao())
    }
    override val coffeeRestRepository: RestCoffeeRepository by lazy {
        RestCoffeeRepository(
            MyServerService.getInstance(),
            coffeeRepository,
            remoteKeyRepository,
            AppDatabase.getInstance(context)
        )
    }
    override val userRestRepository: RestUserRepository by lazy {
        RestUserRepository(
            MyServerService.getInstance(),
            userRepository,
            remoteKeyRepository,
            AppDatabase.getInstance(context)
        )
    }
}