/*
 * F7cloud Talk - Android Client
 *
 * SPDX-FileCopyrightText: 2025 Julius Linus <juliuslinus1@gmail.com>
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

package ru.f7cloud.talk.utils.preview

import android.content.Context
import com.github.aurae.retrofit2.LoganSquareConverterFactory
import com.nextcloud.android.common.ui.color.ColorUtil
import com.nextcloud.android.common.ui.theme.MaterialSchemes
import com.nextcloud.android.common.ui.theme.utils.AndroidViewThemeUtils
import com.nextcloud.android.common.ui.theme.utils.AndroidXViewThemeUtils
import com.nextcloud.android.common.ui.theme.utils.DialogViewThemeUtils
import com.nextcloud.android.common.ui.theme.utils.MaterialViewThemeUtils
import ru.f7cloud.talk.api.NcApi
import ru.f7cloud.talk.api.NcApiCoroutines
import ru.f7cloud.talk.chat.data.ChatMessageRepository
import ru.f7cloud.talk.chat.data.io.AudioFocusRequestManager
import ru.f7cloud.talk.chat.data.io.MediaRecorderManager
import ru.f7cloud.talk.chat.data.network.ChatNetworkDataSource
import ru.f7cloud.talk.chat.data.network.OfflineFirstChatRepository
import ru.f7cloud.talk.chat.data.network.RetrofitChatNetwork
import ru.f7cloud.talk.chat.viewmodels.ChatViewModel
import ru.f7cloud.talk.contacts.ContactsRepository
import ru.f7cloud.talk.contacts.ContactsRepositoryImpl
import ru.f7cloud.talk.contacts.ContactsViewModel
import ru.f7cloud.talk.conversationlist.data.OfflineConversationsRepository
import ru.f7cloud.talk.conversationlist.data.network.ConversationsNetworkDataSource
import ru.f7cloud.talk.conversationlist.data.network.OfflineFirstConversationsRepository
import ru.f7cloud.talk.conversationlist.data.network.RetrofitConversationsNetwork
import ru.f7cloud.talk.data.database.dao.ChatBlocksDao
import ru.f7cloud.talk.data.database.dao.ChatMessagesDao
import ru.f7cloud.talk.data.database.dao.ConversationsDao
import ru.f7cloud.talk.data.network.NetworkMonitor
import ru.f7cloud.talk.data.network.NetworkMonitorImpl
import ru.f7cloud.talk.data.user.UsersDao
import ru.f7cloud.talk.data.user.UsersRepository
import ru.f7cloud.talk.data.user.UsersRepositoryImpl
import ru.f7cloud.talk.repositories.reactions.ReactionsRepository
import ru.f7cloud.talk.repositories.reactions.ReactionsRepositoryImpl
import ru.f7cloud.talk.threadsoverview.data.ThreadsRepository
import ru.f7cloud.talk.threadsoverview.data.ThreadsRepositoryImpl
import ru.f7cloud.talk.ui.theme.MaterialSchemesProviderImpl
import ru.f7cloud.talk.ui.theme.TalkSpecificViewThemeUtils
import ru.f7cloud.talk.ui.theme.ViewThemeUtils
import ru.f7cloud.talk.users.UserManager
import ru.f7cloud.talk.utils.database.user.CurrentUserProviderOldImpl
import ru.f7cloud.talk.utils.database.user.CurrentUserProviderOld
import ru.f7cloud.talk.utils.message.MessageUtils
import ru.f7cloud.talk.utils.preferences.AppPreferences
import ru.f7cloud.talk.utils.preferences.AppPreferencesImpl
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory

/**
 * TODO - basically a reimplementation of common dependencies for use in Previewing Advanced Compose Views
 * It's a hard coded Dependency Injector
 *
 */
class ComposePreviewUtils private constructor(context: Context) {
    private val mContext = context

    companion object {
        fun getInstance(context: Context) = ComposePreviewUtils(context)
        val TAG: String = ComposePreviewUtils::class.java.simpleName
    }

    @OptIn(ExperimentalCoroutinesApi::class)
    val appPreferences: AppPreferences
        get() = AppPreferencesImpl(mContext)

    val context: Context = mContext

    val userRepository: UsersRepository
        get() = UsersRepositoryImpl(usersDao)

    val userManager: UserManager
        get() = UserManager(userRepository)

    val userProvider: CurrentUserProviderOld
        get() = CurrentUserProviderOldImpl(userManager)

    val colorUtil: ColorUtil
        get() = ColorUtil(mContext)

    val materialScheme: MaterialSchemes
        get() = MaterialSchemesProviderImpl(userProvider, colorUtil).getMaterialSchemesForCurrentUser()

    val viewThemeUtils: ViewThemeUtils
        get() {
            val android = AndroidViewThemeUtils(materialScheme, colorUtil)
            val material = MaterialViewThemeUtils(materialScheme, colorUtil)
            val androidx = AndroidXViewThemeUtils(materialScheme, android)
            val talk = TalkSpecificViewThemeUtils(materialScheme, androidx)
            val dialog = DialogViewThemeUtils(materialScheme)
            return ViewThemeUtils(materialScheme, android, material, androidx, talk, dialog)
        }

    val messageUtils: MessageUtils
        get() = MessageUtils(mContext)

    val retrofit: Retrofit
        get() {
            val retrofitBuilder = Retrofit.Builder()
                .client(OkHttpClient.Builder().build())
                .baseUrl("https://f7cloud.com")
                .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
                .addConverterFactory(LoganSquareConverterFactory.create())

            return retrofitBuilder.build()
        }

    val ncApi: NcApi
        get() = retrofit.create(NcApi::class.java)

    val ncApiCoroutines: NcApiCoroutines
        get() = retrofit.create(NcApiCoroutines::class.java)

    val chatNetworkDataSource: ChatNetworkDataSource
        get() = RetrofitChatNetwork(ncApi, ncApiCoroutines)

    val usersDao: UsersDao
        get() = DummyUserDaoImpl()

    val chatMessagesDao: ChatMessagesDao
        get() = DummyChatMessagesDaoImpl()

    val chatBlocksDao: ChatBlocksDao
        get() = DummyChatBlocksDaoImpl()

    val conversationsDao: ConversationsDao
        get() = DummyConversationDaoImpl()

    val networkMonitor: NetworkMonitor
        get() = NetworkMonitorImpl(mContext)

    val chatRepository: ChatMessageRepository
        get() = OfflineFirstChatRepository(
            chatMessagesDao,
            chatBlocksDao,
            chatNetworkDataSource,
            networkMonitor
        )

    val threadsRepository: ThreadsRepository
        get() = ThreadsRepositoryImpl(ncApiCoroutines)

    val conversationNetworkDataSource: ConversationsNetworkDataSource
        get() = RetrofitConversationsNetwork(ncApi)

    val conversationRepository: OfflineConversationsRepository
        get() = OfflineFirstConversationsRepository(
            conversationsDao,
            conversationNetworkDataSource,
            chatNetworkDataSource,
            networkMonitor
        )

    val reactionsRepository: ReactionsRepository
        get() = ReactionsRepositoryImpl(ncApi, chatMessagesDao)

    val mediaRecorderManager: MediaRecorderManager
        get() = MediaRecorderManager()

    val audioFocusRequestManager: AudioFocusRequestManager
        get() = AudioFocusRequestManager(mContext)

    val chatViewModel: ChatViewModel
        get() = ChatViewModel(
            appPreferences,
            chatNetworkDataSource,
            chatRepository,
            threadsRepository,
            conversationRepository,
            reactionsRepository,
            mediaRecorderManager,
            audioFocusRequestManager
        )

    val contactsRepository: ContactsRepository
        get() = ContactsRepositoryImpl(ncApiCoroutines)

    val contactsViewModel: ContactsViewModel
        get() = ContactsViewModel(contactsRepository, userProvider)
}
