package com.pixelized.desktop.lwa

import com.pixelized.desktop.lwa.network.LwaClient
import com.pixelized.desktop.lwa.network.LwaClientImpl
import com.pixelized.desktop.lwa.repository.alteration.AlterationRepository
import com.pixelized.desktop.lwa.repository.alteration.AlterationStore
import com.pixelized.desktop.lwa.repository.campaign.CampaignRepository
import com.pixelized.desktop.lwa.repository.campaign.CampaignStore
import com.pixelized.desktop.lwa.repository.characterSheet.CharacterSheetRepository
import com.pixelized.desktop.lwa.repository.characterSheet.CharacterSheetStore
import com.pixelized.desktop.lwa.repository.inventory.InventoryRepository
import com.pixelized.desktop.lwa.repository.inventory.InventoryStore
import com.pixelized.desktop.lwa.repository.item.ItemRepository
import com.pixelized.desktop.lwa.repository.item.ItemStore
import com.pixelized.desktop.lwa.repository.map.MapRepository
import com.pixelized.desktop.lwa.repository.map.MapStore
import com.pixelized.desktop.lwa.repository.network.NetworkRepository
import com.pixelized.desktop.lwa.repository.resources.ImageResourcesRepository
import com.pixelized.desktop.lwa.repository.roll_history.RollHistoryRepository
import com.pixelized.desktop.lwa.repository.settings.SettingsFactory
import com.pixelized.desktop.lwa.repository.settings.SettingsRepository
import com.pixelized.desktop.lwa.repository.settings.SettingsStore
import com.pixelized.desktop.lwa.repository.tag.TagRepository
import com.pixelized.desktop.lwa.repository.tag.TagStore
import com.pixelized.desktop.lwa.ui.composable.character.alterteration.CharacterSheetAlterationDialogFactory
import com.pixelized.desktop.lwa.ui.composable.character.alterteration.CharacterSheetAlterationDialogViewModel
import com.pixelized.desktop.lwa.ui.composable.character.characteristic.CharacterSheetCharacteristicDialogFactory
import com.pixelized.desktop.lwa.ui.composable.character.characteristic.CharacterSheetCharacteristicDialogViewModel
import com.pixelized.desktop.lwa.ui.composable.character.diminished.CharacterSheetDiminishedDialogFactory
import com.pixelized.desktop.lwa.ui.composable.character.diminished.CharacterSheetDiminishedDialogViewModel
import com.pixelized.desktop.lwa.ui.composable.character.inventory.InventoryDialogFactory
import com.pixelized.desktop.lwa.ui.composable.character.inventory.InventoryDialogViewModel
import com.pixelized.desktop.lwa.ui.composable.character.item.ItemDetailDialogFactory
import com.pixelized.desktop.lwa.ui.composable.character.item.ItemDetailDialogViewModel
import com.pixelized.desktop.lwa.ui.composable.character.purse.PurseDialogFactory
import com.pixelized.desktop.lwa.ui.composable.character.purse.PurseDialogViewModel
import com.pixelized.desktop.lwa.ui.composable.image.ImagerModelConverter
import com.pixelized.desktop.lwa.ui.overlay.portrait.PortraitOverlayViewModel
import com.pixelized.desktop.lwa.ui.overlay.roll.RollViewModel
import com.pixelized.desktop.lwa.ui.screen.admin.AdminViewModel
import com.pixelized.desktop.lwa.ui.screen.admin.action.GMActionUseCase
import com.pixelized.desktop.lwa.ui.screen.admin.action.GMActionViewModel
import com.pixelized.desktop.lwa.ui.screen.admin.alteration.edit.GMAlterationEditFactory
import com.pixelized.desktop.lwa.ui.screen.admin.alteration.edit.GMAlterationEditViewModel
import com.pixelized.desktop.lwa.ui.screen.admin.alteration.list.GMAlterationFactory
import com.pixelized.desktop.lwa.ui.screen.admin.alteration.list.GMAlterationViewModel
import com.pixelized.desktop.lwa.ui.screen.admin.character.edit.GMCharacterEditFactory
import com.pixelized.desktop.lwa.ui.screen.admin.character.edit.GMCharacterEditViewModel
import com.pixelized.desktop.lwa.ui.screen.admin.character.edit.dialog.GMCharacterSheetCopyDialogFactory
import com.pixelized.desktop.lwa.ui.screen.admin.character.edit.dialog.GMCharacterSheetCopyDialogViewModel
import com.pixelized.desktop.lwa.ui.screen.admin.character.list.GMCharacterFactory
import com.pixelized.desktop.lwa.ui.screen.admin.character.list.GMCharacterViewModel
import com.pixelized.desktop.lwa.ui.screen.admin.common.tag.GMTagFactory
import com.pixelized.desktop.lwa.ui.screen.admin.item.edit.GMItemEditFactory
import com.pixelized.desktop.lwa.ui.screen.admin.item.edit.GMItemEditViewModel
import com.pixelized.desktop.lwa.ui.screen.admin.item.list.GMItemFactory
import com.pixelized.desktop.lwa.ui.screen.admin.item.list.GMItemViewModel
import com.pixelized.desktop.lwa.ui.screen.admin.map.edit.GMMapEditFactory
import com.pixelized.desktop.lwa.ui.screen.admin.map.edit.GMMapEditViewModel
import com.pixelized.desktop.lwa.ui.screen.admin.map.list.GMMapFactory
import com.pixelized.desktop.lwa.ui.screen.admin.map.list.GMMapViewModel
import com.pixelized.desktop.lwa.ui.screen.campaign.chatbox.CampaignChatViewModel
import com.pixelized.desktop.lwa.ui.screen.campaign.chatbox.TextMessageFactory
import com.pixelized.desktop.lwa.ui.screen.campaign.map.MapSceneFactory
import com.pixelized.desktop.lwa.ui.screen.campaign.map.MapSceneViewModel
import com.pixelized.desktop.lwa.ui.screen.campaign.player.detail.CharacterDetailPanelViewModel
import com.pixelized.desktop.lwa.ui.screen.campaign.player.detail.header.CharacterDetailHeaderFactory
import com.pixelized.desktop.lwa.ui.screen.campaign.player.detail.inventory.CharacterDetailInventoryFactory
import com.pixelized.desktop.lwa.ui.screen.campaign.player.detail.sheet.CharacterDetailSheetFactory
import com.pixelized.desktop.lwa.ui.screen.campaign.player.ribbon.CharacterRibbonFactory
import com.pixelized.desktop.lwa.ui.screen.campaign.player.ribbon.NpcRibbonViewModel
import com.pixelized.desktop.lwa.ui.screen.campaign.player.ribbon.PlayerRibbonViewModel
import com.pixelized.desktop.lwa.ui.screen.campaign.toolbar.CampaignToolbarViewModel
import com.pixelized.desktop.lwa.ui.screen.campaign.toolbar.links.ResourcesViewModel
import com.pixelized.desktop.lwa.ui.screen.campaign.toolbar.network.NetworkFactory
import com.pixelized.desktop.lwa.ui.screen.campaign.toolbar.network.NetworkViewModel
import com.pixelized.desktop.lwa.ui.screen.levelup.LevelUpFactory
import com.pixelized.desktop.lwa.ui.screen.levelup.LevelUpViewModel
import com.pixelized.desktop.lwa.ui.screen.settings.SettingsViewModel
import com.pixelized.desktop.lwa.usecase.SettingsUseCase
import com.pixelized.shared.lwa.model.campaign.factory.CampaignJsonFactory
import com.pixelized.shared.lwa.utils.PathProvider
import io.ktor.client.HttpClient
import io.ktor.client.engine.okhttp.OkHttp
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.plugins.websocket.WebSockets
import io.ktor.serialization.kotlinx.json.json
import org.koin.core.module.dsl.factoryOf
import org.koin.core.module.dsl.singleOf
import org.koin.core.module.dsl.viewModelOf
import org.koin.dsl.module

val appModuleDependencies
    get() = listOf(
        factoryDependencies,
        useCaseDependencies,
        storeDependencies,
        repositoryDependencies,
        viewModelDependencies,
        toolsDependencies,
    )

val toolsDependencies
    get() = module {
        single {
            HttpClient(
                engine = OkHttp.create(),
            ) {
                install(WebSockets) {
                    pingIntervalMillis = 20_000
                }
                install(ContentNegotiation) {
                    json(get())
                }
            }
        }
        single<LwaClient> {
            val client: HttpClient = get()
            val setting: SettingsRepository = get()
            LwaClientImpl(client, setting)
        }
        single {
            PathProvider(appName = "LwaClient")
        }
        singleOf(::ImagerModelConverter)
    }

val storeDependencies
    get() = module {
        singleOf(::CharacterSheetStore)
        singleOf(::SettingsStore)
        singleOf(::AlterationStore)
        singleOf(::CampaignStore)
        singleOf(::TagStore)
        singleOf(::ItemStore)
        singleOf(::InventoryStore)
        singleOf(::MapStore)
    }

val repositoryDependencies
    get() = module {
        singleOf(::NetworkRepository)
        singleOf(::CharacterSheetRepository)
        singleOf(::RollHistoryRepository)
        singleOf(::SettingsRepository)
        singleOf(::AlterationRepository)
        singleOf(::CampaignRepository)
        singleOf(::TagRepository)
        singleOf(::ItemRepository)
        singleOf(::InventoryRepository)
        singleOf(::ImageResourcesRepository)
        singleOf(::MapRepository)
    }

val factoryDependencies
    get() = module {
        factoryOf(::NetworkFactory)
        factoryOf(::SettingsFactory)
        factoryOf(::CampaignJsonFactory)
        factoryOf(::MapSceneFactory)
        factoryOf(::CharacterRibbonFactory)
        factoryOf(::CharacterDetailHeaderFactory)
        factoryOf(::CharacterDetailSheetFactory)
        factoryOf(::CharacterDetailInventoryFactory)
        factoryOf(::CharacterSheetCharacteristicDialogFactory)
        factoryOf(::CharacterSheetDiminishedDialogFactory)
        factoryOf(::CharacterSheetAlterationDialogFactory)
        factoryOf(::InventoryDialogFactory)
        factoryOf(::ItemDetailDialogFactory)
        factoryOf(::PurseDialogFactory)
        factoryOf(::GMCharacterSheetCopyDialogFactory)
        factoryOf(::TextMessageFactory)
        factoryOf(::LevelUpFactory)
        factoryOf(::GMTagFactory)
        factoryOf(::GMCharacterFactory)
        factoryOf(::GMCharacterEditFactory)
        factoryOf(::GMAlterationFactory)
        factoryOf(::GMAlterationEditFactory)
        factoryOf(::GMItemFactory)
        factoryOf(::GMItemEditFactory)
        factoryOf(::GMMapFactory)
        factoryOf(::GMMapEditFactory)
    }

val viewModelDependencies
    get() = module {
        viewModelOf(::DataSyncViewModel)
        viewModelOf(::CampaignToolbarViewModel)
        viewModelOf(::ResourcesViewModel)
        viewModelOf(::RollViewModel)
        viewModelOf(::NetworkViewModel)
        viewModelOf(::PlayerRibbonViewModel)
        viewModelOf(::MapSceneViewModel)
        viewModelOf(::NpcRibbonViewModel)
        viewModelOf(::CharacterDetailPanelViewModel)
        viewModelOf(::CharacterSheetDiminishedDialogViewModel)
        viewModelOf(::CharacterSheetCharacteristicDialogViewModel)
        viewModelOf(::CharacterSheetAlterationDialogViewModel)
        viewModelOf(::InventoryDialogViewModel)
        viewModelOf(::ItemDetailDialogViewModel)
        viewModelOf(::PurseDialogViewModel)
        viewModelOf(::GMCharacterSheetCopyDialogViewModel)
        viewModelOf(::CampaignChatViewModel)
        viewModelOf(::SettingsViewModel)
        viewModelOf(::LevelUpViewModel)
        viewModelOf(::PortraitOverlayViewModel)
        viewModelOf(::AdminViewModel)
        viewModelOf(::GMCharacterViewModel)
        viewModelOf(::GMCharacterEditViewModel)
        viewModelOf(::GMActionViewModel)
        viewModelOf(::GMAlterationViewModel)
        viewModelOf(::GMAlterationEditViewModel)
        viewModelOf(::GMItemViewModel)
        viewModelOf(::GMItemEditViewModel)
        viewModelOf(::GMMapViewModel)
        viewModelOf(::GMMapEditViewModel)
    }

val useCaseDependencies
    get() = module {
        factoryOf(::SettingsUseCase)
        factoryOf(::GMActionUseCase)
    }