package utils;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.*;

@SuppressWarnings("serial")
public class DrawGraph extends JPanel {
    private double MIN_VALUE = 0;
    private double MAX_VALUE = 0;
    private double MAX_SCORE = 20;
    private static final int PREF_W = 800;
    private static final int PREF_H = 650;
    private static final int BORDER_GAP = 15;
    private static final Color GRAPH_COLOR = Color.black;
    private static final Stroke GRAPH_STROKE = new BasicStroke(1f);
    private static final int GRAPH_POINT_WIDTH = 10;
    private static final int Y_HATCH_CNT = 20;
    private List<Double> scoresBase;
    private List<Double> scores1;
    private List<Double> scores2;
    private List<Point.Double> graphPointsBuy;
    private List<Point.Double> graphPointsSell;


    public DrawGraph(List<Double> scoresBase, List<Double> scores1, List<Double> scores2, List<Point.Double> graphPointsBuy, List<Point.Double> graphPointsSell) {
        this.scores1 = scores1;
        this.scores2 = scores2;
        this.scoresBase = scoresBase;
        this.graphPointsBuy = graphPointsBuy;
        this.graphPointsSell = graphPointsSell;

        this.MIN_VALUE = Collections.min(scoresBase);
        this.MAX_VALUE = Collections.max(scoresBase);
        this.MAX_SCORE = MAX_VALUE - MIN_VALUE;

        System.out.println("MAX_SCORE "+MAX_SCORE+" MIN_VALUE "+MIN_VALUE+" MAX_VALUE "+MAX_VALUE);
    }

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

        double xScale = ((double) getWidth() - 2 * BORDER_GAP) / (scores1.size() - 1);
        double yScale = ((double) getHeight() - 2 * BORDER_GAP) / (MAX_SCORE - 1);

        List<Point.Double> graphPointsBase = new ArrayList<>();
        List<Point.Double> graphPoints1 = new ArrayList<>();
        List<Point.Double> graphPoints2 = new ArrayList<>();

        for (int i = 0; i < scores1.size(); i++) {
            double x1 = i * xScale + BORDER_GAP;
            double y1_base =  (MAX_SCORE - (scoresBase.get(i)-MIN_VALUE)) * yScale + BORDER_GAP;
            double y1_1 =  (MAX_SCORE - (scores1.get(i)-MIN_VALUE)) * yScale + BORDER_GAP;
            double y1_2 =  (MAX_SCORE - (scores2.get(i)-MIN_VALUE)) * yScale + BORDER_GAP;
            graphPointsBase.add(new Point.Double(x1, y1_base));
            graphPoints1.add(new Point.Double(x1, y1_1));
            graphPoints2.add(new Point.Double(x1, y1_2));
        }

        // create x and y axes
        g2.drawLine(BORDER_GAP, getHeight() - BORDER_GAP, BORDER_GAP, BORDER_GAP);
        g2.drawLine(BORDER_GAP, getHeight() - BORDER_GAP, getWidth() - BORDER_GAP, getHeight() - BORDER_GAP);


        // create hatch marks for y axis.
        for (int i = 0; i < Y_HATCH_CNT; i++) {
            int x0 = BORDER_GAP;
            int x1 = GRAPH_POINT_WIDTH + BORDER_GAP;
            int y0 = getHeight() - (((i + 1) * (getHeight() - BORDER_GAP * 2)) / Y_HATCH_CNT + BORDER_GAP);
            int y1 = y0;
            g2.drawLine(x0, y0, x1, y1);
        }

        /*
        // and for x axis
        for (int i = 0; i < scores1.size() - 1; i++) {
            int x0 = (i + 1) * (getWidth() - BORDER_GAP * 2) / (scores1.size() - 1) + BORDER_GAP;
            int x1 = x0;
            int y0 = getHeight() - BORDER_GAP;
            int y1 = y0 - GRAPH_POINT_WIDTH;
            g2.drawLine(x0, y0, x1, y1);
        }*/

        Stroke oldStroke = g2.getStroke();

        g2.setStroke(GRAPH_STROKE);

        g2.setColor(Color.black);

        Line2D line = new Line2D.Double();
        for (int i = 0; i < graphPoints1.size() - 1; i++) {
            double x1 = graphPointsBase.get(i).x;
            double y1 = graphPointsBase.get(i).y;
            double x2 = graphPointsBase.get(i + 1).x;
            double y2 = graphPointsBase.get(i + 1).y;
            line.setLine(x1, y1, x2, y2);
            g2.draw(line);
        }

        g2.setColor(Color.gray);

        for (int i = 0; i < graphPoints1.size() - 1; i++) {
            double x1 = graphPoints1.get(i).x;
            double y1 = graphPoints1.get(i).y;
            double x2 = graphPoints1.get(i + 1).x;
            double y2 = graphPoints1.get(i + 1).y;
            line.setLine(x1, y1, x2, y2);
            g2.draw(line);
        }

        g2.setColor(Color.darkGray);

        for (int i = 0; i < graphPoints2.size() - 1; i++) {
            double x1 = graphPoints2.get(i).x;
            double y1 = graphPoints2.get(i).y;
            double x2 = graphPoints2.get(i + 1).x;
            double y2 = graphPoints2.get(i + 1).y;
            line.setLine(x1, y1, x2, y2);
            g2.draw(line);
        }

        g2.setStroke(oldStroke);
        g2.setColor(Color.green);

        for (int i = 0; i < graphPointsBuy.size(); i++) {
            double x = graphPointsBuy.get(i).x * xScale + BORDER_GAP - GRAPH_POINT_WIDTH / 2;
            double y = (MAX_SCORE - (graphPointsBuy.get(i).y-MIN_VALUE)) * yScale + BORDER_GAP - GRAPH_POINT_WIDTH / 2;

            int ovalW = GRAPH_POINT_WIDTH;
            int ovalH = GRAPH_POINT_WIDTH;

            Ellipse2D.Double elshape = new Ellipse2D.Double(x, y, ovalW, ovalH);
            g2.draw(elshape);
        }

        g2.setColor(Color.red);

        for (int i = 0; i < graphPointsSell.size(); i++) {
            double x = graphPointsSell.get(i).x * xScale + BORDER_GAP - GRAPH_POINT_WIDTH / 2;
            double y = (MAX_SCORE - (graphPointsSell.get(i).y-MIN_VALUE)) * yScale + BORDER_GAP - GRAPH_POINT_WIDTH / 2;

            int ovalW = GRAPH_POINT_WIDTH;
            int ovalH = GRAPH_POINT_WIDTH;

            Ellipse2D.Double elshape = new Ellipse2D.Double(x, y, ovalW, ovalH);
            g2.draw(elshape);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(PREF_W, PREF_H);
    }

}