<commit_before>/*
 * Minecraft Dev for IntelliJ
 *
 * https://minecraftdev.org
 *
 * Copyright (c) 2017 minecraft-dev
 *
 * MIT License
 */

package com.demonwav.mcdev.creator

import com.demonwav.mcdev.asset.PlatformAssets
import com.demonwav.mcdev.platform.PlatformType
import com.demonwav.mcdev.platform.forge.ForgeProjectConfiguration
import com.demonwav.mcdev.platform.forge.version.ForgeVersion
import com.demonwav.mcdev.platform.hybrid.SpongeForgeProjectConfiguration
import com.demonwav.mcdev.platform.mcp.version.McpVersion
import com.demonwav.mcdev.platform.mcp.version.McpVersionEntry
import com.demonwav.mcdev.platform.sponge.SpongeVersion
import com.demonwav.mcdev.util.invokeLaterAny
import com.intellij.util.ui.UIUtil
import org.apache.commons.lang.WordUtils
import org.jetbrains.concurrency.runAsync
import java.awt.event.ActionListener
import javax.swing.DefaultComboBoxModel
import javax.swing.JCheckBox
import javax.swing.JComboBox
import javax.swing.JComponent
import javax.swing.JLabel
import javax.swing.JPanel
import javax.swing.JProgressBar
import javax.swing.JTextField
import javax.swing.SwingWorker

class ForgeProjectSettingsWizard(private val creator: MinecraftProjectCreator) : MinecraftModuleWizardStep() {

    private lateinit var pluginNameField: JTextField
    private lateinit var pluginVersionField: JTextField
    private lateinit var mainClassField: JTextField
    private lateinit var panel: JPanel
    private lateinit var title: JLabel
    private lateinit var descriptionField: JTextField
    private lateinit var authorsField: JTextField
    private lateinit var websiteField: JTextField
    private lateinit var dependField: JTextField
    private lateinit var updateUrlField: JTextField
    private lateinit var minecraftVersionBox: JComboBox<String>
    private lateinit var forgeVersionBox: JComboBox<String>
    private lateinit var mcpVersionBox: JComboBox<McpVersionEntry>
    private lateinit var loadingBar: JProgressBar
    private lateinit var generateDocsCheckbox: JCheckBox
    private lateinit var minecraftVersionLabel: JLabel
    private lateinit var mcpWarning: JLabel
    private lateinit var errorLabel: JLabel

    private var settings: ForgeProjectConfiguration? = null
    private var isSpongeForge: Boolean? = null

    private var spongeVersion: SpongeVersion? = null
    private var mcpVersion: McpVersion? = null
    private var forgeVersion: ForgeVersion? = null

    private val mcpBoxActionListener = ActionListener {
        mcpWarning.isVisible = (mcpVersionBox.selectedItem as? McpVersionEntry)?.isRed == true
    }

    private val minecraftBoxActionListener = ActionListener {
        val tempVersion = version
        runAsync {
            getData(tempVersion)
        }.done { data ->
            invokeLaterAny {
                mcVersionUpdate(data)
            }
        }
    }

    private var apiWorker = ForgeWorker(null)

    init {
        generateDocsCheckbox.isVisible = false
        mcpWarning.isVisible = false
    }

    override fun getComponent(): JComponent? {
        settings = creator.settings[PlatformType.FORGE] as? ForgeProjectConfiguration
        if (settings == null) {
            return null
        }

        pluginNameField.text = WordUtils.capitalize(creator.artifactId)
        pluginVersionField.text = creator.version

        if (settings != null && !settings!!.isFirst) {
            pluginNameField.isEditable = false
            pluginVersionField.isEditable = false
        }

        mainClassField.text = "${this.creator.groupId.toLowerCase()}.${this.creator.artifactId.toLowerCase()}." +
            WordUtils.capitalize(this.creator.artifactId)

        if (creator.settings.size > 1) {
            mainClassField.text = mainClassField.text + PlatformType.FORGE.normalName
        }

        loadingBar.isIndeterminate = true

        apiWorker.execute()

        return panel
    }

    override fun updateStep() {
        if (settings is SpongeForgeProjectConfiguration) {
            if (UIUtil.isUnderDarcula()) {
                title.icon = PlatformAssets.SPONGE_FORGE_ICON_2X_DARK
            } else {
                title.icon = PlatformAssets.SPONGE_ICON_2X_DARK
            }
            title.text = "<html><font size=\"5\">SpongeForge Settings</font></html>"
            generateDocsCheckbox.isVisible = true

            minecraftVersionLabel.text = "Sponge API Version"
        } else {
            title.icon = PlatformAssets.FORGE_ICON_2X
            title.text = "<html><font size=\"5\">Forge Settings</font></html>"
            generateDocsCheckbox.isVisible = false

            minecraftVersionLabel.text = "Minecraft Version"
        }

        if ((forgeVersion == null || mcpVersion == null || spongeVersion == null) && (apiWorker.isCancelled || apiWorker.isDone)) {
            // A SwingWorker will only run once, so we need to create a new instance
            apiWorker = ForgeWorker(version)
            apiWorker.execute()
        } else if (forgeVersion != null && mcpVersion != null && spongeVersion != null) {
            // always make sure versions are reset in case of a state change
            val tempVersion = version
            runAsync {
                getData(tempVersion)
            }.done { data ->
                invokeLaterAny {
                    update(data)
                }
            }
        }
    }

    private fun setForgeVersion(data: Data) {
        forgeVersionBox.model = DefaultComboBoxModel<String>(data.forgeVersions.toTypedArray())
        try {
            forgeVersionBox.selectedIndex = data.forgeSelectedIndex
        } catch (e: IllegalArgumentException) {
            println()
        }
    }

    private val version: String?
        get() {
            if (isSpongeForge == true || (isSpongeForge == null && settings is SpongeForgeProjectConfiguration)) {
                return spongeVersion?.let { it.versions[minecraftVersionBox.selectedItem as? String] }
            } else {
                return minecraftVersionBox.selectedItem as? String
            }
        }

    override fun validate(): Boolean {
        return validate(pluginNameField, pluginVersionField, mainClassField, authorsField, dependField, MinecraftModuleWizardStep.pattern) && !loadingBar.isVisible
    }

    override fun isStepVisible(): Boolean {
        settings = creator.settings[PlatformType.FORGE] as? ForgeProjectConfiguration
        return settings != null
    }

    override fun onStepLeaving() {
        if (loadingBar.isVisible || errorLabel.isVisible) {
            // we're in a cancel state
            apiWorker.cancel(true)<commit_msg>Remove debug try-catch I forgot about<commit_after>/*
 * Minecraft Dev for IntelliJ
 *
 * https://minecraftdev.org
 *
 * Copyright (c) 2017 minecraft-dev
 *
 * MIT License
 */

package com.demonwav.mcdev.creator

import com.demonwav.mcdev.asset.PlatformAssets
import com.demonwav.mcdev.platform.PlatformType
import com.demonwav.mcdev.platform.forge.ForgeProjectConfiguration
import com.demonwav.mcdev.platform.forge.version.ForgeVersion
import com.demonwav.mcdev.platform.hybrid.SpongeForgeProjectConfiguration
import com.demonwav.mcdev.platform.mcp.version.McpVersion
import com.demonwav.mcdev.platform.mcp.version.McpVersionEntry
import com.demonwav.mcdev.platform.sponge.SpongeVersion
import com.demonwav.mcdev.util.invokeLaterAny
import com.intellij.util.ui.UIUtil
import org.apache.commons.lang.WordUtils
import org.jetbrains.concurrency.runAsync
import java.awt.event.ActionListener
import javax.swing.DefaultComboBoxModel
import javax.swing.JCheckBox
import javax.swing.JComboBox
import javax.swing.JComponent
import javax.swing.JLabel
import javax.swing.JPanel
import javax.swing.JProgressBar
import javax.swing.JTextField
import javax.swing.SwingWorker

class ForgeProjectSettingsWizard(private val creator: MinecraftProjectCreator) : MinecraftModuleWizardStep() {

    private lateinit var pluginNameField: JTextField
    private lateinit var pluginVersionField: JTextField
    private lateinit var mainClassField: JTextField
    private lateinit var panel: JPanel
    private lateinit var title: JLabel
    private lateinit var descriptionField: JTextField
    private lateinit var authorsField: JTextField
    private lateinit var websiteField: JTextField
    private lateinit var dependField: JTextField
    private lateinit var updateUrlField: JTextField
    private lateinit var minecraftVersionBox: JComboBox<String>
    private lateinit var forgeVersionBox: JComboBox<String>
    private lateinit var mcpVersionBox: JComboBox<McpVersionEntry>
    private lateinit var loadingBar: JProgressBar
    private lateinit var generateDocsCheckbox: JCheckBox
    private lateinit var minecraftVersionLabel: JLabel
    private lateinit var mcpWarning: JLabel
    private lateinit var errorLabel: JLabel

    private var settings: ForgeProjectConfiguration? = null
    private var isSpongeForge: Boolean? = null

    private var spongeVersion: SpongeVersion? = null
    private var mcpVersion: McpVersion? = null
    private var forgeVersion: ForgeVersion? = null

    private val mcpBoxActionListener = ActionListener {
        mcpWarning.isVisible = (mcpVersionBox.selectedItem as? McpVersionEntry)?.isRed == true
    }

    private val minecraftBoxActionListener = ActionListener {
        val tempVersion = version
        runAsync {
            getData(tempVersion)
        }.done { data ->
            invokeLaterAny {
                mcVersionUpdate(data)
            }
        }
    }

    private var apiWorker = ForgeWorker(null)

    init {
        generateDocsCheckbox.isVisible = false
        mcpWarning.isVisible = false
    }

    override fun getComponent(): JComponent? {
        settings = creator.settings[PlatformType.FORGE] as? ForgeProjectConfiguration
        if (settings == null) {
            return null
        }

        pluginNameField.text = WordUtils.capitalize(creator.artifactId)
        pluginVersionField.text = creator.version

        if (settings != null && !settings!!.isFirst) {
            pluginNameField.isEditable = false
            pluginVersionField.isEditable = false
        }

        mainClassField.text = "${this.creator.groupId.toLowerCase()}.${this.creator.artifactId.toLowerCase()}." +
            WordUtils.capitalize(this.creator.artifactId)

        if (creator.settings.size > 1) {
            mainClassField.text = mainClassField.text + PlatformType.FORGE.normalName
        }

        loadingBar.isIndeterminate = true

        apiWorker.execute()

        return panel
    }

    override fun updateStep() {
        if (settings is SpongeForgeProjectConfiguration) {
            if (UIUtil.isUnderDarcula()) {
                title.icon = PlatformAssets.SPONGE_FORGE_ICON_2X_DARK
            } else {
                title.icon = PlatformAssets.SPONGE_ICON_2X_DARK
            }
            title.text = "<html><font size=\"5\">SpongeForge Settings</font></html>"
            generateDocsCheckbox.isVisible = true

            minecraftVersionLabel.text = "Sponge API Version"
        } else {
            title.icon = PlatformAssets.FORGE_ICON_2X
            title.text = "<html><font size=\"5\">Forge Settings</font></html>"
            generateDocsCheckbox.isVisible = false

            minecraftVersionLabel.text = "Minecraft Version"
        }

        if ((forgeVersion == null || mcpVersion == null || spongeVersion == null) && (apiWorker.isCancelled || apiWorker.isDone)) {
            // A SwingWorker will only run once, so we need to create a new instance
            apiWorker = ForgeWorker(version)
            apiWorker.execute()
        } else if (forgeVersion != null && mcpVersion != null && spongeVersion != null) {
            // always make sure versions are reset in case of a state change
            val tempVersion = version
            runAsync {
                getData(tempVersion)
            }.done { data ->
                invokeLaterAny {
                    update(data)
                }
            }
        }
    }

    private fun setForgeVersion(data: Data) {
        forgeVersionBox.model = DefaultComboBoxModel<String>(data.forgeVersions.toTypedArray())
        forgeVersionBox.selectedIndex = data.forgeSelectedIndex
    }

    private val version: String?
        get() {
            if (isSpongeForge == true || (isSpongeForge == null && settings is SpongeForgeProjectConfiguration)) {
                return spongeVersion?.let { it.versions[minecraftVersionBox.selectedItem as? String] }
            } else {
                return minecraftVersionBox.selectedItem as? String
            }
        }

    override fun validate(): Boolean {
        return validate(pluginNameField, pluginVersionField, mainClassField, authorsField, dependField, MinecraftModuleWizardStep.pattern) && !loadingBar.isVisible
    }

    override fun isStepVisible(): Boolean {
        settings = creator.settings[PlatformType.FORGE] as? ForgeProjectConfiguration
        return settings != null
    }

    override fun onStepLeaving() {
        if (loadingBar.isVisible || errorLabel.isVisible) {
            // we're in a cancel state
            apiWorker.cancel(true)
{'fraction_non_alphanumeric': 0.0784729586426299, 'fraction_numerical': 0.0015149219815179518, 'mean_word_length': 2.8203125, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 33, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7322766', 'n_tokens_mistral': 3671, 'n_tokens_neox': 3518, 'n_words': 821}