using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using FastReport.Design;
using FastReport.Design.PageDesigners.Dialog;
using FastReport.Utils;

namespace FastReport.Dialog
{
    partial class DialogPage
    {
        #region Private Fields

        private Bitmap formBitmap;

        #endregion Private Fields

        #region Public Properties

        /// <inheritdoc/>
        [Browsable(false)]
        public override float Left
        {
            get { return base.Left; }
            set { base.Left = value; }
        }

        /// <inheritdoc/>
        public override SizeF SnapSize
        {
            get { return new SizeF(DialogWorkspace.Grid.SnapSize, DialogWorkspace.Grid.SnapSize); }
        }

        /// <inheritdoc/>
        [Browsable(false)]
        public override float Top
        {
            get { return base.Top; }
            set { base.Top = value; }
        }

        #endregion Public Properties

        #region Internal Properties

        [Browsable(false)]
        internal Bitmap FormBitmap
        {
            get
            {
                if (formBitmap == null)
                    ResetFormBitmap();
                return formBitmap;
            }
        }

        #endregion Internal Properties

        #region Public Methods

        /// <inheritdoc/>
        public override void DrawSelection(FRPaintEventArgs e)
        {
            float m = Form.DpiMultiplier();
            Pen pen = e.Cache.GetPen(Color.Gray, m, DashStyle.Dot);
            e.Graphics.DrawRectangle(pen, -Offset().X - 2 * m, -Offset().Y - 2 * m, Width + 3.5f * m, Height + 3.5f * m);
            base.DrawSelection(e);
        }

        /// <inheritdoc/>
        public override Type GetPageDesignerType()
        {
            return typeof(DialogPageDesigner);
        }

        /// <inheritdoc/>
        public override ContextMenuBase GetContextMenu()
        {
            return new DialogPageMenu(Report.Designer);
        }

        /// <inheritdoc/>
        public override void HandleDoubleClick()
        {
            Report.Designer.ActiveReportTab.SwitchToCode();
            if (String.IsNullOrEmpty(LoadEvent))
            {
                string newEventName = Name + "_Load";
                if (Report.CodeHelper.AddHandler(typeof(EventHandler), newEventName))
                {
                    LoadEvent = newEventName;
                    Report.Designer.SetModified(null, "Change");
                }
            }
            else
            {
                Report.CodeHelper.LocateHandler(LoadEvent);
            }
        }

        /// <inheritdoc/>
        public override void HandleMouseDown(FRMouseEventArgs e)
        {
            e.handled = true;
            e.mode = WorkspaceMode2.SelectionRect;
            e.activeObject = this;
        }

        /// <inheritdoc/>
        public override void HandleMouseHover(FRMouseEventArgs e)
        {
            base.HandleMouseHover(e);
            if (e.handled)
                e.cursor = Cursors.Default;
        }

        /// <inheritdoc/>
        public override void HandleMouseUp(FRMouseEventArgs e)
        {
            base.HandleMouseUp(e);
            if (e.mode == WorkspaceMode2.SelectionRect)
            {
                SelectedObjectCollection selection = Report.Designer.SelectedObjects;
                selection.Clear();
                // find objects inside the selection rect
                foreach (DialogComponentBase c in Controls)
                {
                    e.handled = false;
                    c.HandleMouseUp(e);
                    // object is inside
                    if (e.handled)
                        selection.Add(c);
                }
                if (selection.Count == 0)
                    selection.Add(this);
            }
        }

        /// <inheritdoc/>
        public override void SetDefaults()
        {
            ButtonControl btnOk = new ButtonControl();
            btnOk.Parent = this;
            btnOk.Name = CreateButtonName("btnOk");
            btnOk.Text = Res.Get("Buttons,OK");
            btnOk.Location = new Point((int)ClientSize.Width - 166, (int)ClientSize.Height - 31);
            btnOk.Size = new Size(75, 23);
            btnOk.DialogResult = DialogResult.OK;
            btnOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

            ButtonControl btnCancel = new ButtonControl();
            btnCancel.Parent = this;
            btnCancel.Name = CreateButtonName("btnCancel");
            btnCancel.Text = Res.Get("Buttons,Cancel");
            btnCancel.Location = new Point((int)ClientSize.Width - 83, (int)ClientSize.Height - 31);
            btnCancel.Size = new Size(75, 23);
            btnCancel.DialogResult = DialogResult.Cancel;
            btnCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

            AcceptButton = btnOk;
            CancelButton = btnCancel;
            Form.AutoScaleDimensions = new SizeF(96f, 96f);
            Form.AutoScaleMode = AutoScaleMode.Dpi;

            base.SetDefaults();
        }

        #endregion Public Methods

        #region Internal Methods

        internal void ResetFormBitmap()
        {
            if (formBitmap != null)
            {
                formBitmap.Dispose();
                formBitmap = null;
            }
            if (!IsDesigning)
                return;

            formBitmap = DrawUtils.DrawToBitmap(Form, false);
            // form has round edges which are filled black (DrawToBitmap issue/bug).
            DrawUtils.FloodFill(formBitmap, 0, 0, Color.FromArgb(0, 0, 0), Color.White);
            DrawUtils.FloodFill(formBitmap, formBitmap.Width - 1, 0, Color.FromArgb(0, 0, 0), Color.White);
        }

        #endregion

        #region Protected Methods

        /// <inheritdoc/>
        protected override SelectionPoint[] GetSelectionPoints()
        {
            return new SelectionPoint[] {
                new SelectionPoint(Width - Offset().X + 1, Height - Offset().Y + 1, SizingPoint.RightBottom),
                new SelectionPoint(Width / 2 - Offset().X, Height - Offset().Y + 1, SizingPoint.BottomCenter),
                new SelectionPoint(Width - Offset().X + 1, Height / 2 - Offset().Y, SizingPoint.RightCenter) };
        }

        #endregion Protected Methods

        #region Private Methods

        private Point Offset()
        {
            Point offset = new Point(0, 0);
            offset = Form.PointToScreen(offset);
            offset.X -= Form.Left;
            offset.Y -= Form.Top;
            return offset;
        }

        private bool ShouldSerializeBackColor()
        {
            return BackColor != SystemColors.Control;
        }

        //private bool ShouldSerializeFont()
        //{
        //    return Font.Name != "Tahoma" || Font.Size != 8 || Font.Style != FontStyle.Regular;
        //}

        #endregion Private Methods
    }

    internal class DialogPageMenu : ComponentBaseMenu
    {
        #region Private Methods

        private void miCopy_Click(object sender, EventArgs e)
        {
            Designer.cmdCopyPage.Invoke();
        }

        private void miDelete_Click(object sender, EventArgs e)
        {
            Designer.cmdDeletePage.Invoke();
        }

        #endregion // Private Methods

        #region Constructors

        public DialogPageMenu(Designer designer) : base(designer)
        {
            miEdit.Visible = Designer.cmdEdit.Enabled;
            miCut.Visible = false;
            miPaste.Visible = true;
            miBringToFront.Visible = false;
            miSendToBack.Visible = false;

            miCopy.BeginGroup = true;

            miCopy.Enabled = Designer.cmdCopyPage.Enabled;
            miPaste.Enabled = Designer.cmdPaste.Enabled;
            miDelete.Enabled = Designer.cmdDeletePage.Enabled;

            miCopy.Click += miCopy_Click;
            miDelete.Click += miDelete_Click;
        }

        #endregion // Constructors
    }
}