package de.trainingstagebuch.app.services

import android.content.Context
import de.trainingstagebuch.app.core.SessionManager
import de.trainingstagebuch.app.network.NetworkModule
import de.trainingstagebuch.app.repository.TrainingstagebuchRepository
import java.util.concurrent.atomic.AtomicBoolean

object AppServices {
    private val initialized = AtomicBoolean(false)

    lateinit var authService: AuthService
        private set

    lateinit var homeService: HomeService
        private set

    lateinit var membersService: MembersService
        private set

    lateinit var memberTransferService: MemberTransferService
        private set

    lateinit var clubSettingsService: ClubSettingsService
        private set

    lateinit var diaryService: DiaryService
        private set

    lateinit var trainingStatsService: TrainingStatsService
        private set

    lateinit var predefinedActivitiesService: PredefinedActivitiesService
        private set

    lateinit var scheduleService: ScheduleService
        private set

    lateinit var tournamentService: TournamentService
        private set

    lateinit var permissionsService: PermissionsService
        private set

    lateinit var teamManagementService: TeamManagementService
        private set

    lateinit var logsService: LogsService
        private set

    lateinit var myTischtennisAccountService: MyTischtennisAccountService
        private set

    lateinit var personalSettingsService: PersonalSettingsService
        private set

    fun initialize(context: Context) {
        if (!initialized.compareAndSet(false, true)) return

        val appContext = context.applicationContext
        val repository = TrainingstagebuchRepository(NetworkModule.createApiService())
        val sessionManager = SessionManager(appContext)

        authService = AuthService(
            sessionManager = sessionManager,
            repository = repository
        )
        homeService = HomeService(repository = repository)
        membersService = MembersService(repository = repository)
        memberTransferService = MemberTransferService(repository = repository)
        clubSettingsService = ClubSettingsService(repository = repository)
        diaryService = DiaryService(repository = repository)
        trainingStatsService = TrainingStatsService(repository = repository)
        predefinedActivitiesService = PredefinedActivitiesService(repository = repository)
        scheduleService = ScheduleService(repository = repository)
        tournamentService = TournamentService(repository = repository)
        permissionsService = PermissionsService(repository = repository)
        teamManagementService = TeamManagementService(repository = repository)
        logsService = LogsService(repository = repository)
        myTischtennisAccountService = MyTischtennisAccountService(repository = repository)
        personalSettingsService = PersonalSettingsService(appContext)
    }
}
