package com.axis.innovators.box.gui.renderer;

import javax.swing.*;
import javax.swing.border.AbstractBorder;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;

public class DarculaCompletionCellRenderer extends JPanel implements ListCellRenderer<Object> {
    private final JLabel titleLabel = new JLabel();
    private final JLabel descLabel = new JLabel();
    private final Color defaultBg = new Color(0x2B2B2B);
    private final Color hoverBg = new Color(0x323232);
    private final Color selectedBg = new Color(0x214283);
    private final Color textColor = new Color(0xBBBBBB);
    private final Color descColor = new Color(0x808080);

    public DarculaCompletionCellRenderer() {
        setLayout(new BorderLayout());
        setOpaque(false); // 确保面板透明
        setBorder(new RoundedBorder(8, 1, new Color(0x3C3F41))); // 圆角边框

        Font baseFont;
        try {
            baseFont = new Font("JetBrains Mono", Font.PLAIN, 13);
        } catch (Exception e) {
            baseFont = new Font("Consolas", Font.PLAIN, 13);
        }
        titleLabel.setFont(baseFont);
        titleLabel.setForeground(textColor);
        add(titleLabel, BorderLayout.NORTH);

        try {
            baseFont = new Font("JetBrains Mono", Font.PLAIN, 11);
        } catch (Exception e) {
            baseFont = new Font("Consolas", Font.PLAIN, 11);
        }
        descLabel.setFont(baseFont);
        descLabel.setForeground(descColor);
        add(descLabel, BorderLayout.SOUTH);

        setBorder(BorderFactory.createCompoundBorder(
                getBorder(),
                BorderFactory.createEmptyBorder(4, 12, 4, 12)
        ));
    }

    @Override
    public Component getListCellRendererComponent(
            JList<?> list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus) {

        if (value instanceof Completion comp) {
            titleLabel.setText(comp.getTitle());
            descLabel.setText(comp.getDescription());
        } else {
            titleLabel.setText(value.toString());
            descLabel.setText("");
        }

        if (isSelected) {
            setBackground(selectedBg);
            titleLabel.setForeground(Color.WHITE);
        } else {
            setBackground(index % 2 == 0 ? defaultBg : hoverBg);
            titleLabel.setForeground(textColor);
        }

        return this;
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g2.setColor(getBackground());
        g2.fillRoundRect(0, 0, getWidth(), getHeight(), 8, 8);

        g2.dispose();
        super.paintComponent(g);
    }

    private static class RoundedBorder extends AbstractBorder {
        private final int radius;
        private final int thickness;
        private final Color color;

        public RoundedBorder(int radius, int thickness, Color color) {
            this.radius = radius;
            this.thickness = thickness;
            this.color = color;
        }

        @Override
        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(color);
            g2.draw(new RoundRectangle2D.Float(x, y, width-1, height-1, radius, radius));
            g2.dispose();
        }

        @Override
        public Insets getBorderInsets(Component c) {
            return new Insets(thickness, thickness, thickness, thickness);
        }
    }

    public interface Completion {
        String getTitle();
        String getDescription();
    }
}
