package com.jetbrains.packagesearch.intellij.plugin.ui.toolwindow.panels.management.modules

import com.intellij.icons.AllIcons
import com.intellij.ui.ColoredTreeCellRenderer
import com.intellij.ui.SimpleTextAttributes
import com.intellij.ui.speedSearch.SpeedSearchUtil
import com.jetbrains.packagesearch.intellij.plugin.PackageSearchBundle
import com.jetbrains.packagesearch.intellij.plugin.ui.toolwindow.models.TargetModules
import com.jetbrains.packagesearch.intellij.plugin.ui.util.scaled
import javax.swing.JTree
import javax.swing.tree.DefaultMutableTreeNode

class ModulesTreeItemRenderer : ColoredTreeCellRenderer() {

    override fun customizeCellRenderer(
        tree: JTree,
        value: Any?,
        selected: Boolean,
        expanded: Boolean,
        leaf: Boolean,
        row: Int,
        hasFocus: Boolean
    ) {
        if (value !is DefaultMutableTreeNode) return
        clear()

        @Suppress("MagicNumber") // Swing dimension constants
        iconTextGap = 4.scaled()

        when (val nodeTarget = value.userObject as TargetModules) {
            TargetModules.None, is TargetModules.All -> {
                icon = AllIcons.Nodes.ModuleGroup
                append(
                    PackageSearchBundle.message("packagesearch.ui.toolwindow.allModules"),
                    SimpleTextAttributes.REGULAR_ATTRIBUTES
                )
            }
            is TargetModules.One -> {
                val projectModule = nodeTarget.module.projectModule

                icon = projectModule.moduleType.icon ?: AllIcons.Nodes.Module
                append(projectModule.name, SimpleTextAttributes.REGULAR_ATTRIBUTES)
            }
        }

        SpeedSearchUtil.applySpeedSearchHighlighting(tree, this, true, selected)
    }
}
