package org.hanuna.gitalk.swing_ui.render;

import org.hanuna.gitalk.ui.tables.GraphCommitCell;
import org.hanuna.gitalk.swing_ui.render.painters.GraphCellPainter;
import org.hanuna.gitalk.swing_ui.render.painters.RefPainter;

import javax.swing.*;
import java.awt.*;
import java.awt.font.FontRenderContext;

import static org.hanuna.gitalk.swing_ui.render.Print_Parameters.*;

/**
 * @author erokhins
 */
public class GraphCommitCellRender extends AbstractPaddingCellRender {
    private final GraphCellPainter graphPainter;
    private final RefPainter refPainter = new RefPainter();

    public GraphCommitCellRender(GraphCellPainter graphPainter) {
        this.graphPainter = graphPainter;
    }

    private GraphCommitCell getAssertGraphCommitCell(Object value) {
        assert value instanceof GraphCommitCell;
        return (GraphCommitCell) value;
    }

    @Override
    protected int getLeftPadding(JTable table, Object value) {
        GraphCommitCell cell = getAssertGraphCommitCell(value);

        FontRenderContext fontContext = ((Graphics2D) table.getGraphics()).getFontRenderContext();
        int refPadding = refPainter.padding(cell.getRefsToThisCommit(), fontContext);

        int countCells = cell.getPrintCell().countCell();
        int graphPadding = countCells * WIDTH_NODE;

        return refPadding + graphPadding;
    }

    @Override
    protected String getCellText(JTable table, Object value) {
        GraphCommitCell cell = getAssertGraphCommitCell(value);
        return cell.getText();
    }

    @Override
    protected void additionPaint(Graphics g, JTable table, Object value) {
        GraphCommitCell cell = getAssertGraphCommitCell(value);
        Graphics2D g2 = (Graphics2D) g;
        graphPainter.draw(g2, cell.getPrintCell());

        int countCells = cell.getPrintCell().countCell();
        int padding = countCells * WIDTH_NODE;
        refPainter.draw(g2, cell.getRefsToThisCommit(), padding);
    }



}

@@ -300,16 +300,53 @@
 Context;
+%0Aimport java.awt.image.BufferedImage;
 %0A%0Aimport
@@ -1721,48 +1721,201 @@
 e);%0A
-        Graphics2D g2 = (Graphics2D) g
+%0A        BufferedImage image = new BufferedImage(1000, HEIGHT_CELL, BufferedImage.TYPE_INT_ARGB);%0A        Graphics2D g2 = image.createGraphics();%0A        g2.setBackground(new Color(0,0,0,0))
 ;%0A
+%0A
     
@@ -2068,16 +2068,16 @@
 H_NODE;%0A
-
         
@@ -2133,16 +2133,56 @@
 adding);
+%0A%0A        g.drawImage(image,0, 0, null);
 %0A    %7D%0A%0A

fix long draw line