package org.hanuna.gitalk.swing_ui;

import org.hanuna.gitalk.printmodel.GraphPrintCell;
import org.hanuna.gitalk.ui_controller.EventsController;
import org.hanuna.gitalk.ui_controller.table_models.GraphCommitCell;
import org.hanuna.gitalk.ui_controller.UI_Controller;
import org.hanuna.gitalk.graph.graph_elements.GraphElement;
import org.hanuna.gitalk.swing_ui.render.GraphCommitCellRender;
import org.hanuna.gitalk.swing_ui.render.painters.GraphCellPainter;
import org.hanuna.gitalk.swing_ui.render.painters.SimpleGraphCellPainter;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @author erokhins
 */
public class UI_GraphTable extends JTable {
    private final UI_Controller ui_controller;
    private final GraphCellPainter graphPainter = new SimpleGraphCellPainter();
    private final MouseAdapter mouseAdapter = new MyMouseAdapter();

    public UI_GraphTable(UI_Controller ui_controller) {
        super(ui_controller.getGraphTableModel());
        this.ui_controller = ui_controller;
        prepare();
    }

    private void prepare() {
        setDefaultRenderer(GraphCommitCell.class, new GraphCommitCellRender(graphPainter));
        setRowHeight(GraphCommitCell.HEIGHT_CELL);
        setShowHorizontalLines(false);
        setIntercellSpacing(new Dimension(0, 0));

        getColumnModel().getColumn(0).setPreferredWidth(800);
        getColumnModel().getColumn(1).setMinWidth(80);
        getColumnModel().getColumn(2).setMinWidth(80);

        addMouseMotionListener(mouseAdapter);
        addMouseListener(mouseAdapter);

        ui_controller.addControllerListener(new EventsController.ControllerListener() {
            @Override
            public void jumpToRow(int rowIndex) {
                scrollRectToVisible(getCellRect(rowIndex, 0, false));
                setRowSelectionInterval(rowIndex, rowIndex);
                scrollRectToVisible(getCellRect(rowIndex, 0, false));
            }

            @Override
            public void updateTable() {
                updateUI();
            }
        });

    }

    private class MyMouseAdapter extends MouseAdapter {
        @Nullable
        private GraphElement overCell(MouseEvent e) {
            int rowIndex = e.getY() / GraphCommitCell.HEIGHT_CELL;
            int y = e.getY() - rowIndex * GraphCommitCell.HEIGHT_CELL;
            int x = e.getX();
            GraphPrintCell row = ui_controller.getGraphPrintCell(rowIndex);
            return graphPainter.mouseOver(row, x, y);
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            ui_controller.click(overCell(e));
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            ui_controller.over(overCell(e));
        }
    }

}

@@ -59,90 +59,93 @@
 alk.
-printmodel.GraphPrintCell;%0Aimport org.hanuna.gitalk.ui_controller.EventsController
+graph.graph_elements.GraphElement;%0Aimport org.hanuna.gitalk.printmodel.GraphPrintCell
 ;%0Aim
@@ -171,34 +171,23 @@
 alk.
-ui_controller.table_models
+swing_ui.render
 .Gra
@@ -194,24 +194,30 @@
 phCommitCell
+Render
 ;%0Aimport org
@@ -235,301 +235,303 @@
 alk.
-ui_controller.UI_Controller;%0Aimport org.hanuna.gitalk.graph.graph_elements.GraphElement;%0Aimport org.hanuna.gitalk.swing_ui.render.GraphCommitCellRender;%0Aimport org.hanuna.gitalk.swing_ui.render.painters.GraphCellPainter;%0Aimport org.hanuna.gitalk.swing_ui.render.painters.SimpleGraphCellPainter
+swing_ui.render.painters.GraphCellPainter;%0Aimport org.hanuna.gitalk.swing_ui.render.painters.SimpleGraphCellPainter;%0Aimport org.hanuna.gitalk.ui_controller.EventsController;%0Aimport org.hanuna.gitalk.ui_controller.UI_Controller;%0Aimport org.hanuna.gitalk.ui_controller.table_models.GraphCommitCell
 ;%0Aim
@@ -2468,41 +2468,117 @@
 raph
-Prin
+Commi
 tCell 
-row = ui_controller.get
+commitCell = (GraphCommitCell) UI_GraphTable.this.getModel().getValueAt(rowIndex, 0);%0A            
 Grap
@@ -2587,25 +2587,47 @@
 rintCell
-(rowIndex
+ row = commitCell.getPrintCell(
 );%0A     

fix stupid function getGraphPrintCell in UI_Controller