package com.example.myapplication.database

import android.content.Context
import com.example.myapplication.api.MyServerService
import com.example.myapplication.api.bike.RestBikeRepository
import com.example.myapplication.api.rent.RestRentRepository
import com.example.myapplication.api.rentitem.RestRentItemRepository
import com.example.myapplication.api.item.RestItemRepository
import com.example.myapplication.api.user.RestUserRepository
import com.example.myapplication.api.useritem.RestUserItemRepository
import com.example.myapplication.database.entities.repository.OfflineBikeRepository
import com.example.myapplication.database.entities.repository.OfflineRentRepository
import com.example.myapplication.database.entities.repository.OfflineRentItemRepository
import com.example.myapplication.database.entities.repository.OfflineItemRepository
import com.example.myapplication.database.entities.repository.OfflineUserRepository
import com.example.myapplication.database.entities.repository.OfflineUserItemRepository
import com.example.myapplication.database.remotekeys.repository.OfflineRemoteKeyRepository

interface AppContainer {
    val bikeRestRepository: RestBikeRepository
    val itemRestRepository: RestItemRepository
    val userRestRepository: RestUserRepository
    val rentRestRepository: RestRentRepository
    val rentItemRestRepository: RestRentItemRepository
    val userItemRestRepository: RestUserItemRepository

    companion object {
        const val TIMEOUT = 5000L
        const val LIMIT = 10
    }
}

class AppDataContainer(private val context: Context) : AppContainer {
    private val bikeRepository: OfflineBikeRepository by lazy {
        OfflineBikeRepository(AppDatabase.getInstance(context).bikeDao())
    }
    private val rentRepository: OfflineRentRepository by lazy {
        OfflineRentRepository(AppDatabase.getInstance(context).rentDao())
    }
    private val rentItemRepository: OfflineRentItemRepository by lazy {
        OfflineRentItemRepository(AppDatabase.getInstance(context).rentItemCrossRefDao())
    }
    private val itemRepository: OfflineItemRepository by lazy {
        OfflineItemRepository(AppDatabase.getInstance(context).itemDao())
    }
    private val userRepository: OfflineUserRepository by lazy {
        OfflineUserRepository(AppDatabase.getInstance(context).userDao())
    }
    private val userItemRepository: OfflineUserItemRepository by lazy {
        OfflineUserItemRepository(AppDatabase.getInstance(context).userItemCrossRefDao())
    }
    private val remoteKeyRepository: OfflineRemoteKeyRepository by lazy {
        OfflineRemoteKeyRepository(AppDatabase.getInstance(context).remoteKeysDao())
    }
    override val bikeRestRepository: RestBikeRepository by lazy {
        RestBikeRepository(
            MyServerService.getInstance(),
            bikeRepository,
            itemRepository,
            remoteKeyRepository,
            AppDatabase.getInstance(context)
        )
    }
    override val itemRestRepository: RestItemRepository by lazy {
        RestItemRepository(
            MyServerService.getInstance(),
            itemRepository,
            userItemRepository,
            rentItemRepository,
        )
    }
    override val userRestRepository: RestUserRepository by lazy {
        RestUserRepository(
            MyServerService.getInstance(),
            userRepository,
            userItemRepository,
        )
    }
    override val rentRestRepository: RestRentRepository by lazy {
        RestRentRepository(
            MyServerService.getInstance(),
            rentRepository,
            bikeRepository,
            rentItemRepository,
            remoteKeyRepository,
            AppDatabase.getInstance(context)
        )
    }
    override val userItemRestRepository: RestUserItemRepository by lazy {
        RestUserItemRepository(
            MyServerService.getInstance(),
            userItemRepository,
        )
    }
    override val rentItemRestRepository: RestRentItemRepository by lazy {
        RestRentItemRepository(
            MyServerService.getInstance(),
            rentItemRepository,
        )
    }
}