using MindFusion.Diagramming.Wpf;
using MindFusion.Diagramming.Wpf.Export;
using System.IO;
using System.Windows;
using System.Windows.Media;

namespace SvgExportIssues;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
	public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        diagram.DefaultShape = Shapes.Rectangle;
        diagram.Bounds = new Rect(0, 0, 3840, 3840);

        radioButton1.IsChecked = true;

        diagram.NodeEffects.Add(new GlassEffect());
        diagram.NodeEffects.Add(new AeroEffect());

        Style shapeNodeStyle = new Style();
        shapeNodeStyle.Setters.Add(new Setter(ShapeNode.BrushProperty,
            new LinearGradientBrush(Colors.LightGray, Colors.DarkTurquoise, 80)));
        shapeNodeStyle.Setters.Add(new Setter(ShapeNode.FontSizeProperty, 10.75));
        diagram.ShapeNodeStyle = shapeNodeStyle;

        apat2 = new AnchorPattern(new AnchorPoint[]
            {
                new AnchorPoint(10, 0, true, false, MarkStyle.Circle, Brushes.RoyalBlue),
                new AnchorPoint(50, 0, true, false, MarkStyle.Circle, Brushes.Blue),
                new AnchorPoint(90, 0, true, false, MarkStyle.Circle, Brushes.Firebrick),
                new AnchorPoint(10, 100, false, true, MarkStyle.Rectangle),
                new AnchorPoint(50, 100, false, true, MarkStyle.Rectangle),
                new AnchorPoint(90, 100, false, true, MarkStyle.Rectangle),
                new AnchorPoint(0, 50, true, true, MarkStyle.Custom)
            });

        var node1 = new ShapeNode();
        node1.Bounds = new Rect(100, 100, 100, 100);
        var node2 = new ShapeNode();
        node2.Bounds = new Rect(400, 50, 50, 50);
        var node3 = new ShapeNode();
        node3.Bounds = new Rect(200, 300, 100, 50);
        var link1 = new DiagramLink(diagram, node1, node2);
        link1.ZIndex = int.MaxValue;
        var link2 = new DiagramLink(diagram, node3, node2);
        link1.ControlPoints.Clear();
        link2.ControlPoints.Clear();

        link1.ControlPoints.Add(new Point(200, 150));
        link1.ControlPoints.Add(new Point(250, 150));
        link1.ControlPoints.Add(new Point(300, 150));
        link1.ControlPoints.Add(new Point(300, 75));
        link1.ControlPoints.Add(new Point(400, 75));

        link2.ControlPoints.Add(new Point(250, 300));
        link2.ControlPoints.Add(new Point(250, 25));
        link2.ControlPoints.Add(new Point(425, 25));
        link2.ControlPoints.Add(new Point(425, 50));

        diagram.Nodes.Add(node1);
        diagram.Nodes.Add(node2);
        diagram.Nodes.Add(node3);
        diagram.Nodes.Add(link1);
        diagram.Nodes.Add(link2);
        link1.UpdateFromPoints();
        link2.UpdateFromPoints();

        diagram.LinkCrossings = LinkCrossings.Arcs;
    }

    private void SaveToSvg()
    {
        var exporter = new SvgExporter();
        using var svgStream = new MemoryStream();
        exporter.InvisibleItems = false;
        exporter.Export(diagram, svgStream);
        svgStream.Seek(0, SeekOrigin.Begin);
        using FileStream fileStream = File.Create($"C:\\temp\\test.svg");
        svgStream.CopyTo(fileStream);
    }

    private void radioButton1_Checked(object sender, RoutedEventArgs e)
    {
        diagram.DefaultShape = Shapes.Rectangle;
    }

    private void radioButton2_Checked(object sender, RoutedEventArgs e)
    {
        diagram.DefaultShape = Shapes.Decision;
    }

    private void diagram_NodeCreated(object sender, NodeEventArgs e)
    {
        ShapeNode node = e.Node as ShapeNode;
        if (node == null)
            return;

        if (node.Shape == Shapes.Decision)
            node.AnchorPattern = AnchorPattern.Decision1In3Out;
        else
            node.AnchorPattern = apat2;
    }

    private void OnLoadClick(object sender, RoutedEventArgs e)
    {
        Microsoft.Win32.OpenFileDialog openDlg = new Microsoft.Win32.OpenFileDialog();
        if (openDlg.ShowDialog() == true)
            diagram.LoadFromXml(openDlg.FileName);
    }   

    private void OnSaveClick(object sender, RoutedEventArgs e)
    {
        SaveToSvg();
    }

    private AnchorPattern apat2;
}
