//
// Copyright (c) 2022, MindFusion LLC - Bulgaria.
//

using System;
using System.Drawing;
using System.Windows.Forms;

using MindFusion.Diagramming;


namespace MindFusion.Diagramming.WinForms.Samples.CS.StressTest
{
	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
		}

		private void btnGenerate_Click(object sender, EventArgs e)
		{
			// adding items is slower when the diagram is shown inside a view
			// so we create a new diagram here
			diagram = new Diagram();
			diagram.BackBrush = diagramView.Diagram.BackBrush;
			diagram.ShapeNodeStyle.Brush = diagramView.Diagram.ShapeNodeStyle.Brush;
			diagram.DiagramLinkStyle.Stroke = new MindFusion.Drawing.SolidBrush(Color.FromArgb(0, 52, 102));
			diagram.DefaultShape = Shapes.Rectangle;
			diagram.ShadowsStyle = ShadowsStyle.None;

			// adding a lot of items is faster when these properties are disabled
			diagram.ValidityChecks = false;
			diagram.SelectAfterCreate = false;
			diagram.AutoResize = AutoResize.None;
			diagram.SelectionOnTop = false;

			var total = 10000;
			var rowSize = (int)Math.Sqrt(total);
			var size = 20;
			var dist = 30;

			var diagSize = rowSize * dist;
			diagram.Bounds = new RectangleF(
				0, 0, diagSize, diagSize);

			var x = diagram.Bounds.Left;
			var y = diagram.Bounds.Top;
			for (int i = 0; i < total; i++)
			{
				var node = diagram.Factory.CreateShapeNode(x, y, size, size);
				node.Text = i.ToString();
				if (x > diagram.Bounds.Left)
				{
					diagram.Factory.CreateDiagramLink(
						diagram.Nodes[i - 1], diagram.Nodes[i]);
				}

				x += dist;
				if (x >= diagram.Bounds.Right)
				{
					x = diagram.Bounds.Left;
					y += dist;
				}
			}

			// index item positions for faster viewport clipping and hit testing
			diagram.EnableSpatialIndex = true;

			// finally show the diagram inside the view
			diagramView.Diagram = diagram;
		}
	}
}
