using System;
using System.Windows.Forms;
using System.ComponentModel;
using FastReport.Forms;
using FastReport.Utils;
using FastReport.DevComponents.DotNetBar;
using FastReport.Design.PageDesigners.Page;
using System.Drawing;

namespace FastReport.Design.StandardDesigner
{
    /// <summary>
    /// Represents the designer's statusbar.
    /// </summary>
    [ToolboxItem(false)]
    public class DesignerStatusBar : Bar, IDesignerPlugin
    {
        #region Fields
        private Designer designer;
        private LabelItem lblLocation;
        private LabelItem lbllocationRightBot;
        private LabelItem lblSize;
        private LabelItem lblText;
        private ItemContainer pnZoom;
        private ItemContainer pnZoomButtons;
        private ButtonItem btnZoomPageWidth;
        private ButtonItem btnZoomWholePage;
        private ButtonItem btnZoom100;
        private SliderItem slZoom;
        private bool updatingZoom;
        private Timer zoomTimer;
        private float zoomToUpdate;
        #endregion

        #region Properties
        private Designer Designer
        {
            get { return designer; }
        }

        #endregion

        #region Private Methods
        private void UpdateZoom()
        {
            updatingZoom = true;

            int zoom = (int)(Designer.Zoom * 100);
            slZoom.Text = zoom.ToString() + "%";
            if (zoom < 100)
                zoom = (int)Math.Round((zoom - 25) / 0.75f);
            else if (zoom > 100)
                zoom = (zoom - 100) / 4 + 100;
            slZoom.Value = zoom;

            updatingZoom = false;
        }

        private void btnZoomPageWidth_Click(object sender, EventArgs e)
        {
            Designer.ZoomPageWidth();
        }

        private void btnZoomWholePage_Click(object sender, EventArgs e)
        {
            Designer.ZoomWholePage();
        }

        private void btnZoom100_Click(object sender, EventArgs e)
        {
            Designer.Zoom = 1;
        }

        private void slZoom_ValueChanged(object sender, EventArgs e)
        {
            if (updatingZoom)
                return;

            int val = slZoom.Value;
            if (val < 100)
                val = (int)Math.Round(val * 0.75f) + 25;
            else
                val = (val - 100) * 4 + 100;

            slZoom.Text = val.ToString() + "%";
            zoomToUpdate = val / 100f;
            zoomTimer.Start();
        }

        private void FZoomTimer_Tick(object sender, EventArgs e)
        {
            zoomTimer.Stop();
            Designer.Zoom = zoomToUpdate;
        }
        #endregion

        #region IDesignerPlugin
        /// <inheritdoc/>
        public string PluginName
        {
            get { return Name; }
        }

        /// <inheritdoc/>
        public void SaveState()
        {
        }

        /// <inheritdoc/>
        public void RestoreState()
        {
        }

        /// <inheritdoc/>
        public void SelectionChanged()
        {
            UpdateContent();
        }

        /// <inheritdoc/>
        public void UpdateContent()
        {
            UpdateZoom();
        }

        /// <inheritdoc/>
        public void Lock()
        {
        }

        /// <inheritdoc/>
        public void Unlock()
        {
            UpdateContent();
        }

        /// <inheritdoc/>
        public void Localize()
        {
            UpdateContent();
        }

        /// <inheritdoc/>
        public DesignerOptionsPage GetOptionsPage()
        {
            return null;
        }

        /// <inheritdoc/>
        public void UpdateUIStyle()
        {
            Style = UIStyleUtils.GetDotNetBarStyle(Designer.UIStyle);
        }

        /// <inheritdoc/>
        public new void UpdateDpiDependencies()
        {
            base.UpdateDpiDependencies();

            Font = Designer.Font;
            lblLocation.Image = Designer.GetImage(62);
            Image image = (Image)Designer.GetImage(62).Clone();
            image.RotateFlip(RotateFlipType.Rotate180FlipNone);
            lbllocationRightBot.Image = image;
            lblSize.Image = Designer.GetImage(63);
            btnZoomPageWidth.Image = Designer.GetImage(235);
            btnZoomWholePage.Image = Designer.GetImage(236);
            btnZoom100.Image = Designer.GetImage(237);
            lblLocation.Width = this.LogicalToDevice(120);
            lbllocationRightBot.Width = this.LogicalToDevice(120);
            lblSize.Width = this.LogicalToDevice(120);
            slZoom.UpdateDpiDependencies();
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Updates the information about location and size.
        /// </summary>
        /// <param name="location">The location.</param>
        /// <param name="size">The size.</param>
        public void UpdateLocationAndSize(string location, string size, string locationRightBot)
        {
            lblLocation.Visible = !String.IsNullOrEmpty(location);
            lblLocation.Text = location;
            lblSize.Visible = !String.IsNullOrEmpty(size);
            lblSize.Text = size;
            lbllocationRightBot.Visible = !String.IsNullOrEmpty(locationRightBot);
            lbllocationRightBot.Text = locationRightBot;
        }

        /// <summary>
        /// Updates the name and text information.
        /// </summary>
        /// <param name="s">The text.</param>
        public void UpdateText(string s)
        {
            SelectedObjectCollection selection = designer.SelectedObjects;
            string text = selection.Count == 0 ? "" : selection.Count > 1 ?
              String.Format(Res.Get("Designer,ToolWindow,Properties,NObjectsSelected"), selection.Count) :
              selection[0].Name;
            if (!String.IsNullOrEmpty(s))
                text += ":  " + s.Replace('\r', ' ').Replace('\n', ' ');

            lblText.Text = text;
        }
        #endregion


        /// <summary>
        /// Initializes a new instance of the <see cref="DesignerStatusBar"/> class with default settings.
        /// </summary>
        /// <param name="designer">The report designer.</param>
        public DesignerStatusBar(Designer designer) : base()
        {
            Name = "StatusBar";
            this.designer = designer;
            BarType = eBarType.StatusBar;
            GrabHandleStyle = eGrabHandleStyle.ResizeHandle;
            PaddingBottom = 2;
            PaddingTop = 3;
            //Style = FastReport.DevComponents.DotNetBar.eDotNetBarStyle.StyleManagerControlled;

            lblLocation = new LabelItem();
            lblLocation.Width = 160;
            lblLocation.Height = 19;

            lbllocationRightBot = new LabelItem();
            lbllocationRightBot.Width = 160;
            lbllocationRightBot.Height = 19;

            lblSize = new LabelItem();
            lblSize.Width = 160;
            lblSize.Height = 19;

            lblText = new LabelItem();
            lblText.Height = 19;
            lblText.Stretch = true;
            lblText.EnableMarkup = false;

            pnZoom = new ItemContainer();
            pnZoom.BackgroundStyle.Class = "Office2007StatusBarBackground2";

            pnZoomButtons = new ItemContainer();
            pnZoomButtons.BeginGroup = true;
            pnZoomButtons.VerticalItemAlignment = eVerticalItemsAlignment.Middle;

            btnZoomPageWidth = new ButtonItem();
            btnZoomPageWidth.Click += btnZoomPageWidth_Click;

            btnZoomWholePage = new ButtonItem();
            btnZoomWholePage.Click += btnZoomWholePage_Click;

            btnZoom100 = new ButtonItem();
            btnZoom100.Click += btnZoom100_Click;

            pnZoomButtons.SubItems.AddRange(new BaseItem[] { btnZoomPageWidth, btnZoomWholePage, btnZoom100 });

            slZoom = new SliderItem();
            slZoom.Maximum = 200;
            slZoom.Step = 5;
            slZoom.Text = "100%";
            slZoom.Value = 100;
            slZoom.Width = 120;
            slZoom.ValueChanged += slZoom_ValueChanged;

            pnZoom.SubItems.AddRange(new BaseItem[] { pnZoomButtons, slZoom });

            Items.AddRange(new BaseItem[] { lblLocation, lbllocationRightBot, lblSize, lblText, pnZoom });
            Dock = DockStyle.Bottom;
            this.designer.Controls.Add(this);

            zoomTimer = new Timer();
            zoomTimer.Interval = 50;
            zoomTimer.Tick += FZoomTimer_Tick;
        }
    }

}
