using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

namespace FamilyTree
{
    public class DrawablePerson : IDrawable
    {
        // Constructor.
        public Image Picture = null;
        public string Description;
        public bool Selected = false;
        public DrawablePerson(string description, Image picture)
        {
            Description = description;
            Picture = picture;
        }

        // The size of the drawn rectangles.
        public SizeF NodeSize = new SizeF(100, 100);

        // Set the object's required size.
        public void SetSize(Graphics gr, Font font)
        {
            // Pick a size at most 100x100 with the same aspect ratio as the picture.
            RectangleF rect = FitRectangle(Picture, new RectangleF(0, 0, 100, 100));
            NodeSize = rect.Size;
        }

        // Return the object's required size.
        public SizeF Size { get { return NodeSize; } }

        // Return a RectangleF giving the node's location.
        private RectangleF Location(PointF center)
        {
            return new RectangleF(
                center.X - NodeSize.Width / 2,
                center.Y - NodeSize.Height / 2,
                NodeSize.Width, NodeSize.Height);
        }

        // Return True if the target is under this node.
        public bool IsAtPoint(PointF center, PointF point)
        {
            RectangleF rect = Location(center);
            return rect.Contains(point);
        }

        // Draw the person.
        public void Draw(PointF center, Graphics gr, Pen pen, Brush bgBrush, Brush textBrush, Font font)
        {
            // Draw a border.
            RectangleF rectf = Location(center);
            Rectangle rect = Rectangle.Round(rectf);
            if (Selected)
            {
                gr.FillRectangle(Brushes.White, rect);
                ControlPaint.DrawBorder3D(gr, rect,
                    Border3DStyle.Sunken);
            }
            else
            {
                gr.FillRectangle(Brushes.LightGray, rect);
                ControlPaint.DrawBorder3D(gr, rect,
                    Border3DStyle.Raised);
            }

            // Draw the picture.
            rectf.Inflate(-5, -5);
            rectf = FitRectangle(Picture, rectf);
            gr.DrawImage(Picture, rectf);
        }

        // Return the biggest rectangle that fits within bounds
        // and that has the same aspect ratio as the picture.
        private RectangleF FitRectangle(Image picture, RectangleF bounds)
        {
            // Get the X and Y scales.
            float picWid = picture.Width;
            float picHgt = picture.Height;
            float picAspect = picWid / picHgt;
            float rectAspect = bounds.Width / bounds.Height;
            float scale = 1;
            if (picAspect > rectAspect)
                scale = bounds.Width / picWid;
            else
                scale = bounds.Height / picHgt;

            // See where we need to draw.
            picWid *= scale;
            picHgt *= scale;
            RectangleF drawingRect = new RectangleF(
                bounds.X + (bounds.Width - picWid) / 2,
                bounds.Y + (bounds.Height - picHgt) / 2,
                picWid, picHgt);
            return drawingRect;
        }
    }
}
