package com.dowstats.service.w40k

import com.dowstats.configuration.StorageConfig
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.io.File
import java.nio.file.Path
import kotlin.io.path.exists

@Service
class ModAttribPathService @Autowired constructor(
    private val storageConfig: StorageConfig,
) {

    val pathToWanilaData = storageConfig.wanilaStorage.replace("/", File.separator)

    fun getModFolderData(modTechName: String, modVersion: String) =
        "${storageConfig.modStorage.replace("/", File.separator)}${File.separator}$modTechName$modVersion${File.separator}Data"

    fun getUcsFolder(modFolderData: String): String =
        "${modFolderData.replace("Data", "")}Locale${File.separator}English${File.separator}"

    fun getUcsFolderRus(modFolderData: String): String =
        "${modFolderData.replace("Data", "")}Locale${File.separator}Russian${File.separator}"

    fun getWeaponAttribsPath(modFolderData: String): String =
        "$modFolderData${File.separator}attrib${File.separator}weapon"

    fun getAbilitiesAttribsPath(modFolderData: String): String =
        "$modFolderData${File.separator}attrib${File.separator}abilities"

    fun getAbilityEnvironmentsAttribsPath(modFolderData: String): String =
        "$modFolderData${File.separator}attrib${File.separator}ebps${File.separator}environment${File.separator}gameplay"

    fun getResearchAttribsPath(modFolderData: String): String =
        "$modFolderData${File.separator}attrib${File.separator}research"

    fun getAddonAttribsPath(modFolderData: String): String =
        "$modFolderData${File.separator}attrib${File.separator}addons"

    fun getSbpsAttribsFolderPath(modFolderData: String): String =
        "$modFolderData${File.separator}attrib${File.separator}sbps${File.separator}races${File.separator}"

    fun getSbpsAttribsPath(modFolderData: String, race: String): String =
        "${getSbpsAttribsFolderPath(modFolderData)}$race"

    fun getEbpsTroopsAttribsPath(modFolderData: String, race: String): String =
        "$modFolderData${File.separator}attrib${File.separator}ebps${File.separator}races${File.separator}$race${File.separator}troops"

    fun geBuildingAttribsPath(modFolderData: String, race: String): String =
        "$modFolderData${File.separator}attrib${File.separator}ebps${File.separator}races${File.separator}$race${File.separator}structures"

    fun getIconPath(modFolderData: String, iconPath: String): String  {

        val iconValidEndPath = iconPath.replace("\\", File.separator).replace("/", File.separator).replace(".tga", "")

        val pathToIcon = "${File.separator}art${File.separator}ui${File.separator}ingame${File.separator}$iconValidEndPath.tga"
        val modIcon = "$modFolderData$pathToIcon"
        val lowercaseModIcon = "$modFolderData${pathToIcon.lowercase()}"

        val wanilaModIcon =  "$pathToWanilaData$pathToIcon"

        return if (Path.of(modIcon).exists()) modIcon else
                if (Path.of(lowercaseModIcon).exists()) lowercaseModIcon else
                    wanilaModIcon
    }

}
