using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Drawing.Drawing2D;
using System.Drawing.Text;

namespace FamilyTree
{
    public partial class frm66FamilyTree : Form
    {
        public frm66FamilyTree()
        {
            InitializeComponent();
        }

        // The root node.
        private DrawableTreeNode<DrawablePerson> root =
            new DrawableTreeNode<DrawablePerson>(
                new DrawablePerson("Queen Elizabeth II && Prince Philip Duke of Edinburgh",
                    Properties.Resources.Elizabeth_Philip));

        // Make a tree.
        private void frm66FamilyTree_Load(object sender, EventArgs e)
        {
            DrawableTreeNode<DrawablePerson> charles =
                new DrawableTreeNode<DrawablePerson>(
                    new DrawablePerson("Prince Charles, Prince of Wales",
                        Properties.Resources.Charles));
            DrawableTreeNode<DrawablePerson> anne =
                new DrawableTreeNode<DrawablePerson>(
                    new DrawablePerson("Princess Anne",
                        Properties.Resources.Anne));
            DrawableTreeNode<DrawablePerson> andrew =
                new DrawableTreeNode<DrawablePerson>(
                    new DrawablePerson("Prince Andrew, Duke of York",
                        Properties.Resources.Andrew));
            DrawableTreeNode<DrawablePerson> edward =
                new DrawableTreeNode<DrawablePerson>(
                    new DrawablePerson("Prince Edward, Earl of Wessex",
                        Properties.Resources.Edward));
            DrawableTreeNode<DrawablePerson> william =
                new DrawableTreeNode<DrawablePerson>(
                    new DrawablePerson("Prince William",
                        Properties.Resources.William));
            DrawableTreeNode<DrawablePerson> harry =
                new DrawableTreeNode<DrawablePerson>(
                    new DrawablePerson("Prince Henry (Harry)",
                        Properties.Resources.Harry));
            DrawableTreeNode<DrawablePerson> peter =
                new DrawableTreeNode<DrawablePerson>(
                    new DrawablePerson("Peter Phillips",
                        Properties.Resources.Peter));
            DrawableTreeNode<DrawablePerson> zara =
                new DrawableTreeNode<DrawablePerson>(
                    new DrawablePerson("Zara Phillips",
                        Properties.Resources.Zara));
            DrawableTreeNode<DrawablePerson> beatrice =
                new DrawableTreeNode<DrawablePerson>(
                    new DrawablePerson("Princess Beatrice",
                        Properties.Resources.Beatrice));
            DrawableTreeNode<DrawablePerson> eugenie =
                new DrawableTreeNode<DrawablePerson>(
                    new DrawablePerson("Princess Eugenie",
                        Properties.Resources.Eugenie));
            DrawableTreeNode<DrawablePerson> louise =
                new DrawableTreeNode<DrawablePerson>(
                    new DrawablePerson("Lady Louise",
                        Properties.Resources.Louise));
            DrawableTreeNode<DrawablePerson> severn =
                new DrawableTreeNode<DrawablePerson>(
                    new DrawablePerson("Viscount Severn",
                        Properties.Resources.Severn));

            root.AddChild(charles);
            charles.AddChild(william);
            charles.AddChild(harry);
            root.AddChild(anne);
            anne.AddChild(peter);
            anne.AddChild(zara);
            root.AddChild(andrew);
            andrew.AddChild(beatrice);
            andrew.AddChild(eugenie);
            root.AddChild(edward);
            edward.AddChild(louise);
            edward.AddChild(severn);

            // Arrange the tree.
            ArrangeTree();
        }

        // Draw the tree.
        private void treePictureBox_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
            using (Font font = new Font("Times New Roman", 10))
            {
                root.DrawTree(e.Graphics, Pens.Black, Brushes.White, Brushes.Black, font);
            }
        }

        // Center the tree on the form.
        private void treePictureBox_Resize(object sender, EventArgs e)
        {
            ArrangeTree();
        }
        private void ArrangeTree()
        {
            using (Graphics gr = treePictureBox.CreateGraphics())
            {
                // Size the tree's nodes.
                root.SetSizes(gr, Font);

                // Arrange the tree once to see how big it is.
                float x = 0, y = 0;
                root.Arrange(ref x, ref y);

                // Arrange the tree again to center it horizontally.
                x = (this.ClientSize.Width - x) / 2;
                y = 10;
                root.Arrange(ref x, ref y);
            }

            treePictureBox.Refresh();
        }

        // The currently selected node.
        private DrawableTreeNode<DrawablePerson> SelectedNode;

        private void picTree_MouseClick(object sender, MouseEventArgs e)
        {
            FindNodeUnderMouse(e.Location);
        }

        // Set SelectedNode to the node under the mouse.
        private void FindNodeUnderMouse(PointF pt)
        {
            // Deselect the previously selected node.
            if (SelectedNode != null)
            {
                SelectedNode.Data.Selected = false;
                nodeTextLabel.Text = "";
            }

            // Find the node at this position (if any).
            SelectedNode = root.NodeAtPoint(pt);

            // Select the node.
            if (SelectedNode != null)
            {
                SelectedNode.Data.Selected = true;
                nodeTextLabel.Text = SelectedNode.Data.Description;
            }

            // Redraw.
            treePictureBox.Refresh();
        }


        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.treePictureBox = new System.Windows.Forms.PictureBox();
            this.statusStrip1 = new System.Windows.Forms.StatusStrip();
            this.nodeTextLabel = new System.Windows.Forms.ToolStripStatusLabel();
            ((System.ComponentModel.ISupportInitialize)(this.treePictureBox)).BeginInit();
            this.statusStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // treePictureBox
            // 
            this.treePictureBox.BackColor = System.Drawing.Color.LightGray;
            this.treePictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
            this.treePictureBox.Location = new System.Drawing.Point(0, 0);
            this.treePictureBox.Name = "treePictureBox";
            this.treePictureBox.Size = new System.Drawing.Size(684, 339);
            this.treePictureBox.TabIndex = 10;
            this.treePictureBox.TabStop = false;
            this.treePictureBox.Paint += new System.Windows.Forms.PaintEventHandler(this.treePictureBox_Paint);
            this.treePictureBox.MouseClick += new System.Windows.Forms.MouseEventHandler(this.picTree_MouseClick);
            this.treePictureBox.Resize += new System.EventHandler(this.treePictureBox_Resize);
            // 
            // statusStrip1
            // 
            this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.nodeTextLabel});
            this.statusStrip1.Location = new System.Drawing.Point(0, 339);
            this.statusStrip1.Name = "statusStrip1";
            this.statusStrip1.Size = new System.Drawing.Size(684, 22);
            this.statusStrip1.TabIndex = 9;
            this.statusStrip1.Text = "statusStrip1";
            // 
            // nodeTextLabel
            // 
            this.nodeTextLabel.Name = "nodeTextLabel";
            this.nodeTextLabel.Size = new System.Drawing.Size(0, 17);
            // 
            // frm66FamilyTree
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(684, 361);
            this.Controls.Add(this.treePictureBox);
            this.Controls.Add(this.statusStrip1);
            this.Name = "frm66FamilyTree";
            this.Text = "FamilyTree";
            this.Load += new System.EventHandler(this.frm66FamilyTree_Load);
            ((System.ComponentModel.ISupportInitialize)(this.treePictureBox)).EndInit();
            this.statusStrip1.ResumeLayout(false);
            this.statusStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.PictureBox treePictureBox;
        private System.Windows.Forms.StatusStrip statusStrip1;
        private System.Windows.Forms.ToolStripStatusLabel nodeTextLabel;
    }
}