<commit_before>package com.github.jk1.ytplugin.setupWindow

import com.github.jk1.ytplugin.ComponentAware
import com.github.jk1.ytplugin.tasks.YouTrackServer
import com.github.jk1.ytplugin.ui.HyperlinkLabel
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.DialogWrapper
import com.intellij.tasks.youtrack.YouTrackRepository
import com.intellij.tasks.youtrack.YouTrackRepositoryType
import com.intellij.ui.components.*
import com.intellij.util.net.HttpConfigurable
import com.jetbrains.rd.swing.textProperty
import java.awt.*
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.awt.event.KeyEvent
import javax.swing.*
import javax.swing.text.AttributeSet
import javax.swing.text.SimpleAttributeSet
import javax.swing.text.StyleConstants
import javax.swing.text.StyleContext


open class SetupDialog(override val project: Project, inputRepo: YouTrackServer) : DialogWrapper(project, false), ComponentAware {


    private val testConnectionAction = TestConnectionCommandAction("Test Connection")
    private val okAction = OkCommandAction("Ok")

    private lateinit var bigTabFrame: JBTabbedPane
    private lateinit var tabPanel: JBPanel<JBPanelWithEmptyText>
    private lateinit var tab2Panel: JBPanel<JBPanelWithEmptyText>

    private lateinit var serverUrl: JBLabel
    private lateinit var notifyField: JBLabel
    private lateinit var notifyFieldTab2: JBLabel

    private lateinit var proxyDescription: JBLabel

    private lateinit var tokenField: JBLabel
    private lateinit var getTokenField: HyperlinkLabel
    private lateinit var advertiserField: HyperlinkLabel
    private lateinit var controlPanel: JBPanel<JBPanelWithEmptyText>
    private lateinit var shareUrl: JBCheckBox
    private lateinit var useProxy: JBCheckBox
    private lateinit var useHTTP: JBCheckBox
    private lateinit var loginAnon: JBCheckBox
    private lateinit var testConnectPanel: JBPanel<JBPanelWithEmptyText>
    private lateinit var proxyPanel: JBPanel<JBPanelWithEmptyText>
    private lateinit var okPanel: JBPanel<JBPanelWithEmptyText>
    private lateinit var cancelPanel: JBPanel<JBPanelWithEmptyText>

    private val repo: YouTrackServer = inputRepo
    private var myRepository: YouTrackRepository = repo.getRepo()

    private var proxySettingsButton = JButton("Proxy settings...")
    var inputUrl = JTextPane()
    var inputToken = JBPasswordField()

    override fun init() {
        title = "YouTrack"
        super.init()
    }

    override fun show() {
        init()
        super.show()
    }

    private fun loginAnonymouslyChanged(enabled: Boolean) {
        inputToken.isEnabled = enabled
        tokenField.isEnabled = enabled
        useHTTP.isEnabled = enabled
    }

    private fun testConnectionAction() {
        val setup = SetupTask()
        setup.correctUrl = inputUrl.text
        val fontColor = inputToken.foreground


        val myRepositoryType = YouTrackRepositoryType()

        myRepository.url = inputUrl.text
        myRepository.password = <PASSWORD>
        myRepository.username = "random" // could be anything
        myRepository.repositoryType = myRepositoryType
        myRepository.storeCredentials()

        myRepository.isShared = shareUrl.isSelected()
        myRepository.isUseProxy = useProxy.isSelected()
        myRepository.isUseHttpAuthentication = useHTTP.isSelected()
        myRepository.isLoginAnonymously = loginAnon.isSelected()

        setup.testConnection(myRepository, project)

        val oldUrl = inputUrl.text
        inputUrl.text = ""

        if (oldUrl == setup.correctUrl) {
            println("here")
            inputUrl.text = oldUrl
        } else {
            if (!oldUrl.contains("/youtrack") && setup.noteState == NotifierState.SUCCESS) {
                if (!oldUrl.contains("https") && oldUrl.contains("http") && setup.correctUrl.contains("https")) {
                    appendToPane(inputUrl, "https", Color.GREEN)
                    appendToPane(inputUrl, setup.correctUrl.substring(5, setup.correctUrl.length - 9), fontColor)
                    appendToPane(inputUrl, "/youtrack", Color.GREEN)
                }
                else{
                    appendToPane(inputUrl, setup.correctUrl.substring(0, setup.correctUrl.length - 9), fontColor)
                    appendToPane(inputUrl, "/youtrack", Color.GREEN)
                }
            }
            else {
                if (!oldUrl.contains("https") && oldUrl.contains("http") && setup.correctUrl.contains("https")) {
                    appendToPane(inputUrl, "https", Color.GREEN)
                    appendToPane(inputUrl, setup.correctUrl.substring(5, setup.correctUrl.length), fontColor)
                } else {
                    inputUrl.text = oldUrl
                }
            }
        }

        if (myRepository.url.isBlank() || myRepository.password.isBlank()) {
            notifyField.foreground = Color.red
            notifyField.text = "Url and token fields are mandatory"
        } else if (myRepository.isLoginAnonymously && setup.noteState != NotifierState.UNKNOWN_HOST) {
            notifyField.foreground = Color.green
            notifyField.text = "Login as a guest"
        } else
            setup.setNotifier(notifyField)

        notifyFieldTab2.text = notifyField.text
        notifyFieldTab2.foreground = notifyField.foreground

        if (setup.noteState == NotifierState.SUCCESS || myRepository.isLoginAnonymously){
            val setupWindow = SetupWindowManager(project)
            setupWindow.showIssues(myRepository)

        }
        myRepository.url = setup.correctUrl
        myRepository.password = <PASSWORD>
    }

    private fun appendToPane(tp: JTextPane, msg: String, c: Color) {
        val sc = StyleContext.getDefaultStyleContext()
        var aset: AttributeSet? = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c)
        aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED)
        val len = tp.document.length
        tp.caretPosition = len
        tp.setCharacterAttributes(aset, false)
        tp.replaceSelection(msg)
    }

    private fun enableButtons() {
        this.useProxy.isEnabled = HttpConfigurable.getInstance().USE_HTTP_PROXY
        if (!HttpConfigurable.getInstance().USE_HTTP_PROXY) {
            this.useProxy.isSelected = false
        }
    }

    private fun prepareTabbedPane(): JTabbedPane {

//        val color = (inputToken.border as LineBorder?)?.lineColor
//        val size = (inputToken.border as LineBorder?)?.thickness
        serverUrl = JBLabel("Server Url:")
        serverUrl.setBounds(65, 60, 100, 22)
        inputUrl.apply {
            layout = BorderLayout()
            border = inputToken.border
            text = repo.url
//            border = MatteBorder(size!!, size, size, size, Color.red)
            background = inputToken.background
            setBounds(152, 60, 374, 24)
        }

        tokenField = JBLabel("Permanent token:")
        tokenField.setBounds(15, 120, 150, 22)
        inputToken.apply {
            text = repo.password
            echoChar = '\u25CF'
            setBounds(150, 120, 378, 31)
        }

        advertiserField = HyperlinkLabel("Not YouTrack customer yet? Get YouTrack", "https://www.jetbrains.com/youtrack/download/get_youtrack.html?idea_integration")
        advertiserField.setBounds(240, 30, 300, 17)

        getTokenField = HyperlinkLabel("Get token", "https://www.jetbrains.com/help/youtrack/incloud/Manage-Permanent-Token.html")
        getTokenField.setBounds(457, 155, 100, 17)

        notifyField = JBLabel("").apply {
            foreground = Color.red;
            setBounds(150, 158, 250, 17)
        }

        notifyFieldTab2 = JBLabel("").apply {
            foreground = Color.red;
            setBounds(220, 170, 250, 17)
        }

        shareUrl = JBCheckBox("Share Url", false)
        shareUrl.setBounds(440, 95, 100, 17)

        loginAnon = JBCheckBox("Login Anonymously", false)
        loginAnon.setBounds(150, 95, 170, 17)

        useHTTP = JBCheckBox("Use HTTP", false)
        useHTTP.setBounds(20, 50, 100, 17);

        useProxy = JBCheckBox("Use Proxy", false)
        useProxy.setBounds(20, 100, 100, 17)

        proxyDescription = JBLabel("You can configure the HTTP Proxy to:")
        proxyDescription.setBounds(220, 20, 370, 20)


        proxySettingsButton.addActionListener(ActionListener {
            HttpConfigurable.editConfigurable(controlPanel)
            enableButtons()
        })

        loginAnon.addActionListener(ActionListener { loginAnonymouslyChanged(!loginAnon.isSelected()) })

        proxyPanel = JBPanel<JBPanelWithEmptyText>().apply {
            add(proxySettingsButton)
            setBounds(15, 155, 140, 40)
        }

        controlPanel = JBPanel<JBPanelWithEmptyText>().apply { layout = null }<commit_msg>cosmetic<commit_after>package com.github.jk1.ytplugin.setupWindow

import com.github.jk1.ytplugin.ComponentAware
import com.github.jk1.ytplugin.tasks.YouTrackServer
import com.github.jk1.ytplugin.ui.HyperlinkLabel
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.DialogWrapper
import com.intellij.tasks.youtrack.YouTrackRepository
import com.intellij.tasks.youtrack.YouTrackRepositoryType
import com.intellij.ui.components.*
import com.intellij.util.net.HttpConfigurable
import com.jetbrains.rd.swing.textProperty
import java.awt.*
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.awt.event.KeyEvent
import javax.swing.*
import javax.swing.text.AttributeSet
import javax.swing.text.SimpleAttributeSet
import javax.swing.text.StyleConstants
import javax.swing.text.StyleContext


open class SetupDialog(override val project: Project, inputRepo: YouTrackServer) : DialogWrapper(project, false), ComponentAware {


    private val testConnectionAction = TestConnectionCommandAction("Test Connection")
    private val okAction = OkCommandAction("Ok")

    private lateinit var bigTabFrame: JBTabbedPane
    private lateinit var tabPanel: JBPanel<JBPanelWithEmptyText>
    private lateinit var tab2Panel: JBPanel<JBPanelWithEmptyText>

    private lateinit var serverUrl: JBLabel
    private lateinit var notifyField: JBLabel
    private lateinit var notifyFieldTab2: JBLabel

    private lateinit var proxyDescription: JBLabel

    private lateinit var tokenField: JBLabel
    private lateinit var getTokenField: HyperlinkLabel
    private lateinit var advertiserField: HyperlinkLabel
    private lateinit var controlPanel: JBPanel<JBPanelWithEmptyText>
    private lateinit var shareUrl: JBCheckBox
    private lateinit var useProxy: JBCheckBox
    private lateinit var useHTTP: JBCheckBox
    private lateinit var loginAnon: JBCheckBox
    private lateinit var testConnectPanel: JBPanel<JBPanelWithEmptyText>
    private lateinit var proxyPanel: JBPanel<JBPanelWithEmptyText>
    private lateinit var okPanel: JBPanel<JBPanelWithEmptyText>
    private lateinit var cancelPanel: JBPanel<JBPanelWithEmptyText>

    private val repo: YouTrackServer = inputRepo
    private var myRepository: YouTrackRepository = repo.getRepo()

    private var proxySettingsButton = JButton("Proxy settings...")
    var inputUrl = JTextPane()
    var inputToken = JBPasswordField()

    override fun init() {
        title = "YouTrack"
        super.init()
    }

    override fun show() {
        init()
        super.show()
    }

    private fun loginAnonymouslyChanged(enabled: Boolean) {
        inputToken.isEnabled = enabled
        tokenField.isEnabled = enabled
        useHTTP.isEnabled = enabled
    }

    private fun testConnectionAction() {
        val setup = SetupTask()
        setup.correctUrl = inputUrl.text
        val fontColor = inputToken.foreground


        val myRepositoryType = YouTrackRepositoryType()

        myRepository.url = inputUrl.text
        myRepository.password = inputToken.text
        myRepository.username = "random" // could be anything
        myRepository.repositoryType = myRepositoryType
        myRepository.storeCredentials()

        myRepository.isShared = shareUrl.isSelected()
        myRepository.isUseProxy = useProxy.isSelected()
        myRepository.isUseHttpAuthentication = useHTTP.isSelected()
        myRepository.isLoginAnonymously = loginAnon.isSelected()

        setup.testConnection(myRepository, project)

        val oldUrl = inputUrl.text
        inputUrl.text = ""

        if (oldUrl == setup.correctUrl) {
            inputUrl.text = oldUrl
        } else {
            if (!oldUrl.contains("/youtrack") && setup.noteState == NotifierState.SUCCESS) {
                if (!oldUrl.contains("https") && oldUrl.contains("http") && setup.correctUrl.contains("https")) {
                    appendToPane(inputUrl, "https", Color.GREEN)
                    appendToPane(inputUrl, setup.correctUrl.substring(5, setup.correctUrl.length - 9), fontColor)
                    appendToPane(inputUrl, "/youtrack", Color.GREEN)
                }
                else{
                    appendToPane(inputUrl, setup.correctUrl.substring(0, setup.correctUrl.length - 9), fontColor)
                    appendToPane(inputUrl, "/youtrack", Color.GREEN)
                }
            }
            else {
                if (!oldUrl.contains("https") && oldUrl.contains("http") && setup.correctUrl.contains("https")) {
                    appendToPane(inputUrl, "https", Color.GREEN)
                    appendToPane(inputUrl, setup.correctUrl.substring(5, setup.correctUrl.length), fontColor)
                } else {
                    inputUrl.text = oldUrl
                }
            }
        }

        if (myRepository.url.isBlank() || myRepository.password.isBlank()) {
            notifyField.foreground = Color.red
            notifyField.text = "Url and token fields are mandatory"
        } else if (myRepository.isLoginAnonymously && setup.noteState != NotifierState.UNKNOWN_HOST) {
            notifyField.foreground = Color.green
            notifyField.text = "Login as a guest"
        } else
            setup.setNotifier(notifyField)

        notifyFieldTab2.text = notifyField.text
        notifyFieldTab2.foreground = notifyField.foreground

        if (setup.noteState == NotifierState.SUCCESS || myRepository.isLoginAnonymously){
            val setupWindow = SetupWindowManager(project)
            setupWindow.showIssues(myRepository)

        }
        myRepository.url = setup.correctUrl
        myRepository.password = <PASSWORD>
    }

    private fun appendToPane(tp: JTextPane, msg: String, c: Color) {
        val sc = StyleContext.getDefaultStyleContext()
        var aset: AttributeSet? = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c)
        aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED)
        val len = tp.document.length
        tp.caretPosition = len
        tp.setCharacterAttributes(aset, false)
        tp.replaceSelection(msg)
    }

    private fun enableButtons() {
        this.useProxy.isEnabled = HttpConfigurable.getInstance().USE_HTTP_PROXY
        if (!HttpConfigurable.getInstance().USE_HTTP_PROXY) {
            this.useProxy.isSelected = false
        }
    }

    private fun prepareTabbedPane(): JTabbedPane {

//        val color = (inputToken.border as LineBorder?)?.lineColor
//        val size = (inputToken.border as LineBorder?)?.thickness
        serverUrl = JBLabel("Server Url:")
        serverUrl.setBounds(65, 60, 100, 22)
        inputUrl.apply {
            layout = BorderLayout()
            border = inputToken.border
            text = repo.url
//            border = MatteBorder(size!!, size, size, size, Color.red)
            background = inputToken.background
            setBounds(152, 60, 374, 24)
        }

        tokenField = JBLabel("Permanent token:")
        tokenField.setBounds(15, 120, 150, 22)
        inputToken.apply {
            text = repo.password
            echoChar = '\u25CF'
            setBounds(150, 120, 378, 31)
        }

        advertiserField = HyperlinkLabel("Not YouTrack customer yet? Get YouTrack", "https://www.jetbrains.com/youtrack/download/get_youtrack.html?idea_integration")
        advertiserField.setBounds(240, 30, 300, 17)

        getTokenField = HyperlinkLabel("Get token", "https://www.jetbrains.com/help/youtrack/incloud/Manage-Permanent-Token.html")
        getTokenField.setBounds(457, 155, 100, 17)

        notifyField = JBLabel("").apply {
            foreground = Color.red;
            setBounds(150, 158, 250, 36)
        }

        notifyFieldTab2 = JBLabel("").apply {
            foreground = Color.red;
            setBounds(220, 160, 250, 36)
        }

        shareUrl = JBCheckBox("Share Url", false)
        shareUrl.setBounds(440, 95, 100, 17)

        loginAnon = JBCheckBox("Login Anonymously", false)
        loginAnon.setBounds(150, 95, 170, 17)

        useHTTP = JBCheckBox("Use HTTP", false)
        useHTTP.setBounds(20, 50, 100, 17);

        useProxy = JBCheckBox("Use Proxy", false)
        useProxy.setBounds(20, 100, 100, 17)

        proxyDescription = JBLabel("You can configure the HTTP Proxy to:")
        proxyDescription.setBounds(220, 20, 370, 20)


        proxySettingsButton.addActionListener(ActionListener {
            HttpConfigurable.editConfigurable(controlPanel)
            enableButtons()
        })

        loginAnon.addActionListener(ActionListener { loginAnonymouslyChanged(!loginAnon.isSelected()) })

        proxyPanel = JBPanel<JBPanelWithEmptyText>().apply {
            add(proxySettingsButton)
            setBounds(15, 155, 140, 40)
        }

        controlPanel = JBPanel<JBPanelWithEmptyText>().apply { layout = null }
{'fraction_non_alphanumeric': 0.09147644214550771, 'fraction_numerical': 0.017766782862925896, 'mean_word_length': 2.7612603087333474, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7321295', 'n_tokens_mistral': 5253, 'n_tokens_neox': 4928, 'n_words': 1037}