//.################################################
//# Copyright © 2006-2010 Evolving Software Ltd   #
//# Author: Vijay Patel                           #
//################################################'

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;

using TestApplicationDomainModel;

using Evolving.DomainTypes;
using Evolving.TrueView.Domain.Attributes;
using Evolving.TrueView.UI;
using System.Drawing;
using System.Windows.Forms;
using TestApplicationDomainModel.BasicTypes;
using TestApplicationDomainModel.UserInterface;

namespace TestApplication
{

    public class CustomStylingPresenter : IPresenterInfrastructureService<CustomStylingExample>
    {

        private Font _WingDings = new Font("WingDings", 14);

        public void ApplyStyle(CustomStylingExample domainObject, System.Reflection.MemberInfo memberInfo, IStyleArgs memberStyle)
        {
            switch (memberInfo.Name)
            {
                case "BooleanValue":
                    if (domainObject.BooleanValue == true)
                    {
                        memberStyle.BackColor = Color.Red;
                        memberStyle.ForeColor = Color.White;
                    }
                    else
                    {
                        // Use the defaults:
                        memberStyle.BackColor = null;
                        memberStyle.ForeColor = null;
                    }

                    break;

                case "TextValue":
                    memberStyle.Font = _WingDings;
                    break;


                case "EnumValue":
                    switch (domainObject.EnumValue)
                    {
                        case EnumValues.IndividualOptions.None:
                            // Use the defaults:
                            memberStyle.IconName = null;
                            break;

                        case EnumValues.IndividualOptions.Red:
                            // memberStyle.IconName refers to the files in the "Icons" folder:
                            memberStyle.IconName = "ledred";
                            break;

                        case EnumValues.IndividualOptions.Blue:
                            // memberStyle.IconName refers to the files in the "Icons" folder:
                            memberStyle.IconName = "ledblue";
                            break;

                        case EnumValues.IndividualOptions.Green:
                            // memberStyle.IconName refers to the files in the "Icons" folder:
                            memberStyle.IconName = "ledgreen";
                            break;
                    }

                    break;
            }
        }

        public void ApplyStyle(CustomStylingExample domainObject, IStyleArgs objectStyle)
        {

        }

        public Control CreateAdornmentFor(CustomStylingExample domainObject)
        {
            return null;
        }

        public Control CreateAdornmentFor(CustomStylingExample domainObject, System.Reflection.PropertyInfo propertyInfo)
        {
            switch (propertyInfo.Name)
            {
                case "NumberValue":
                    // Create a control to represent NumberValue:
                    var panel = new Panel();
                    panel.Size = new Size(150, 20);
                    panel.Tag = domainObject;
                    panel.Paint += new PaintEventHandler(panel_Paint);
                    panel.Disposed += delegate
                    {
                        panel.Tag = null;
                    };

                    return panel;

                default:
                    return null;
            }
        }

        private Pen _Pen = new Pen(Color.DarkGray, 4);

        void panel_Paint(object sender, PaintEventArgs e)
        {
            var panel = (Panel)sender;
            var domainObject = (CustomStylingExample)panel.Tag;
            if (domainObject.NumberValue == 0)
                return;

            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            var circleDiameter = 10;
            var circleTop = (panel.Height - circleDiameter) / 2;
            var circlePadding = 6;
            for (var i = 0; i < domainObject.NumberValue; i++)
            {
                e.Graphics.DrawEllipse(_Pen,
                                       i * (circleDiameter + circlePadding) + _Pen.Width / 2,
                                       circleTop,
                                       circleDiameter,
                                       circleDiameter);
            }
        }

        public Control CreateViewFor(CustomStylingExample domainObject)
        {
            return null;
        }

    }
}
