namespace FlowChartNetBeta
{
    using MindFusion.Diagramming.WinForms;
    using MindFusion.Diagramming;
    using System.Drawing.Drawing2D;
    using MindFusion.Diagramming.Animations;
    using MindFusion.Animations;

    public partial class Form1 : Form
    {
        DiagramPage diagram;
        PagedDiagramView diagramView;
        int animationType = 0;
        int diagramAnimationType = 0;

        Color[] backgrounds = {
            Color.FromArgb(230, 230, 255),
            Color.FromArgb(230, 255, 204),
            Color.FromArgb(255, 230, 255),
            Color.FromArgb(255, 255, 204),
            Color.FromArgb(237, 237, 222)
            };

        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            diagramView = new PagedDiagramView();
            diagramView.Dock = DockStyle.Fill;
            diagramView.AllowInplaceEdit = true;
            diagramView.ShowAddButton = true;
            diagramView.ShowRemoveButton = true;
            diagramView.PageAdded += DiagramView_PageAdded;



            diagram = new DiagramPage();
            diagram.Title = "Header";
            diagram.BackBrush =
                new MindFusion.Drawing.SolidBrush(Color.FromArgb(255, 235, 204));

            setNodeStyle();

            diagramView.Document = new DiagramDocument();
            diagramView.Document.Pages.Add(diagram);
            this.Controls.Add(diagramView);
        }

        private void DiagramView_PageAdded(object? sender, DiagramPageEventArgs e)
        {
            diagram = e.DiagramPage;

            int pagesCount = diagramView.Document.Pages.Count;

            if (pagesCount <= 5)
                diagram.BackBrush = new MindFusion.Drawing.SolidBrush(
                    backgrounds[pagesCount - 1]);
            else
                diagram.BackBrush = new MindFusion.Drawing.SolidBrush(Color.White);

            setNodeStyle();
        }

        private void setNodeStyle()
        {
            Theme theme = new Theme();
            ShapeNodeStyle style = new ShapeNodeStyle();
            style.FontFamily = "Courier New";
            style.FontSize = 20;
            style.Brush = new MindFusion.Drawing.LinearGradientBrush(Color.Pink,
                Color.Orange);
            style.Stroke = new MindFusion.Drawing.SolidBrush(Color.Gray);
            theme.RegisterStyle(typeof(ShapeNode), style);
            diagram.Theme = theme;
        }

        private void btnAnimateDiagram_Click(object sender, EventArgs e)
        {
            if (diagramAnimationType == 0)
            {
                diagramView.Animate(
                new ScrollAnimation(
                    new AnimationOptions
                    {
                        FromValue = new PointF(diagramView.ScrollX, diagramView.ScrollY),
                        ToValue = new PointF(diagramView.ScrollX + 20, diagramView.ScrollY + 20),
                        AnimationType = AnimationType.Power,
                        EasingType = EasingType.EaseInOut,
                        Reverse = true,
                        Repeat = true,
                        RepeatCount = 2
                    }));

                label.Text = "Diagram Scroll Animation";
            }
            else
            {
                diagramView.Animate(
                new ZoomAnimation(
                    new AnimationOptions
                    {
                        FromValue = 100,
                        ToValue = 80,
                        AnimationType = AnimationType.Circular,
                        EasingType = EasingType.EaseInOut,
                        Repeat = true,
                        RepeatCount = 4,
                        Reverse = true
                    }));

                label.Text = "Diagram Zoom Animation";
            }

            diagramAnimationType++;

            if (diagramAnimationType > 1)
                diagramAnimationType = 0;


        }

        private void btnAnimateNode_Click(object sender, EventArgs e)
        {
            diagram = diagramView.CurrentPage;

            ShapeNode node = null;
            if (diagram.Selection.Nodes.Count > 0)
                node = diagram.Selection.Nodes[0] as ShapeNode;

            if (node != null)
            {
                switch (animationType)
                {
                    case 0:
                        {
                            node.Animate(
                                new PathAnimation(new List<PointF>
                                {
                        new PointF(30, 30),
                        new PointF(50, 30),
                        new PointF(75, 66),
                        new PointF(125, 88)
                                },
                                new AnimationOptions
                                {
                                    KeepLastValue = true,
                                    Duration = 1000
                                }));

                            label.Text = "Path Animation";

                            break;
                        }
                    case 1:
                        {
                            node.Animate(
                                new OpacityAnimation(
                                    new AnimationOptions
                                    {
                                        FromValue = 1,
                                        ToValue = 0.5,
                                        Duration = 500,
                                        Repeat = true,
                                        RepeatCount = 5,
                                        Reverse = true,
                                        AnimationType = AnimationType.BackBow
                                    }));

                            label.Text = "Opacity Animation";

                            break;
                        }
                    case 2:
                        {
                            node.Animate(
                                new BounceAnimation(
                                    new AnimationOptions
                                    {
                                        FromValue = 0,
                                        ToValue = -5,
                                        Duration = 222,
                                        Repeat = true,
                                        RepeatCount = 4,
                                        Reverse = true,
                                        AnimationType = AnimationType.BackBow
                                    }));

                            label.Text = "Bounce Animation";

                            break;
                        }
                    case 3:
                        {
                            node.Animate(
                                new FillColorAnimation(
                                    new AnimationOptions
                                    {
                                        FromValue = Color.Pink,
                                        ToValue = Color.Orange,
                                        Duration = 700,
                                        Repeat = true,
                                        RepeatCount = 5,
                                        Reverse = true,

                                    })); ;

                            label.Text = "FillColor Animation";

                            break;
                        }
                    case 4:
                        {
                            node.Animate(
                                new StrokeColorAnimation(
                                    new AnimationOptions
                                    {
                                        FromValue = Color.Red,
                                        ToValue = Color.DarkOrange,
                                        Duration = 700,
                                        Repeat = true,
                                        RepeatCount = 5,
                                        Reverse = true,

                                    })); ;

                            label.Text = "StrokeColor Animation";

                            break;
                        }
                    case 5:
                    default:
                        {
                            node.Animate(
                                new PulseAnimation(
                                    new AnimationOptions
                                    {
                                        Duration = 2000,
                                        Repeat = true,
                                        RepeatCount = 3,
                                        AnimationType = AnimationType.Circular
                                    }));

                            label.Text = "Pulse Animation";
                            break;
                        }
                }//end of switch

            }//end of if node != null

            animationType++;

            if (animationType == 6)
                animationType = 0;

        }//end of event handler method
    }//end of class
}//end of namespace