using System;
using System.Drawing;
using System.Windows.Forms;
using ChuPiao.AutoPrint.Desktop.Properties;
using ChuPiao.Common.Utils;

namespace ChuPiao.AutoPrint.Desktop.StatusBar
{
    //IProgressMonitor
    public class SdStatusBar : StatusStrip
    {
        ToolStripProgressBar statusProgressBar = new ToolStripProgressBar();
        ToolStripStatusLabel jobNamePanel = new ToolStripStatusLabel();

        ToolStripStatusLabel txtStatusBarPanel = new ToolStripStatusLabel();
        ToolStripStatusLabel cursorStatusBarPanel = new ToolStripStatusLabel();
        ToolStripStatusLabel modeStatusBarPanel = new ToolStripStatusLabel();
        ToolStripStatusLabel springLabel = new ToolStripStatusLabel();

        ToolStripSplitButton stopWarnButton=new ToolStripSplitButton();

        public ToolStripStatusLabel CursorStatusBarPanel
        {
            get
            {
                return cursorStatusBarPanel;
            }
        }

        public ToolStripStatusLabel ModeStatusBarPanel
        {
            get
            {
                return modeStatusBarPanel;
            }
        }

        public ToolStripStatusLabel JobNamePanel
        {
            get
            {
                return jobNamePanel;
            }
        }

        public SdStatusBar()
        {

            springLabel.Spring = true;
            cursorStatusBarPanel.AutoSize = false;
            cursorStatusBarPanel.Width = 150;
            modeStatusBarPanel.AutoSize = false;
            modeStatusBarPanel.Width = 25;
            statusProgressBar.Visible = false;
            statusProgressBar.Width = 100;

            stopWarnButton.Image = Resources.Warning;
            stopWarnButton.ForeColor = Color.Red;
            stopWarnButton.Text = "停止报警";
            stopWarnButton.Visible = false;
            stopWarnButton.Click += new EventHandler(toolStripSplitButton_Click);

            Items.AddRange(new ToolStripItem[] { txtStatusBarPanel, springLabel, jobNamePanel, statusProgressBar, cursorStatusBarPanel, modeStatusBarPanel,stopWarnButton });
        }

        void toolStripSplitButton_Click(object sender, EventArgs e)
        {
            BeepUtil.Instance().StopCurrent();
            stopWarnButton.Visible = false;
        }

        public void ShowWarnButton()
        {
            
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new MethodInvoker(ShowWarnButton));
            }
            else
            {
                stopWarnButton.Visible = true;
                for (int i = 50; i > 0; i--)
                {
                    BeepUtil.MessageBeep(0);
                    BeepUtil.MessageBeep(16);
                    BeepUtil.MessageBeep(32);
                    BeepUtil.MessageBeep(48);
                    BeepUtil.MessageBeep(64);
                }
            }
        }
        public void StopWarnButton()
        {

            if (this.InvokeRequired)
            {
                this.BeginInvoke(new MethodInvoker(StopWarnButton));
            }
            else
            {
                stopWarnButton.Visible = false;
            }
        }

        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            UpdateText();
        }

        public void ShowErrorMessage(string message)
        {
            SetMessage("Error : " + message);
        }

        public void ShowErrorMessage(Image image, string message)
        {
            SetMessage(image, "Error : " + message);
        }

        string currentMessage;

        public void SetMessage(string message)
        {
            SetMessage(message, false);
        }

        public void SetMessage(string message, bool highlighted)
        {
            if (highlighted)
            {
                txtStatusBarPanel.BackColor = SystemColors.Highlight;
                txtStatusBarPanel.ForeColor = Color.White;
            }
            else if (txtStatusBarPanel.BackColor == SystemColors.Highlight)
            {
                txtStatusBarPanel.BackColor = SystemColors.Control;
                txtStatusBarPanel.ForeColor = SystemColors.ControlText;
            }

            currentMessage = message;
            if (this.IsHandleCreated)
                BeginInvoke(new MethodInvoker(UpdateText));
        }

        void UpdateText()
        {
            txtStatusBarPanel.Text = currentMessage;
        }

        public void SetMessage(Image image, string message)
        {
            SetMessage(message);
        }

        // Progress Monitor implementation
        int totalWork;

        public void BeginTask(string name, int totalWork, bool allowCancel)
        {
            taskName = name;
            this.totalWork = totalWork;
            if (this.IsHandleCreated)
            {
                this.BeginInvoke(new MethodInvoker(MakeVisible));
            }
        }

        void MakeVisible()
        {
            statusProgressBar.Value = 0;
            statusProgressBar.Maximum = totalWork;
            SetTaskName();
            jobNamePanel.Visible = true;
            statusProgressBar.Visible = true;
        }

        void MakeInvisible()
        {
            // Setting jobNamePanel.Visible = false will also hide the other labels to the right (WinForms Bug?)
            jobNamePanel.Text = "";
            statusProgressBar.Visible = false;
        }

        int workDone;

        public int WorkDone
        {
            get
            {
                return workDone;
            }
            set
            {
                if (workDone == value) return;
                workDone = value;
                this.BeginInvoke(new MethodInvoker(SetWorkDone));
            }
        }

        void SetWorkDone()
        {
            if (workDone < statusProgressBar.Maximum)
            {
                statusProgressBar.Value = workDone;
            }
        }

        public void Done()
        {
            taskName = null;
            if (this.IsHandleCreated)
            {
                this.BeginInvoke(new MethodInvoker(MakeInvisible));
            }
        }

        string taskName;

        public string TaskName
        {
            get
            {
                return taskName;
            }
            set
            {
                if (taskName == value) return;
                taskName = value;
                this.BeginInvoke(new MethodInvoker(SetTaskName));
            }
        }

        void SetTaskName()
        {
            jobNamePanel.Text = taskName;
        }

        bool showingDialog;

        public bool ShowingDialog
        {
            get { return showingDialog; }
            set { showingDialog = value; }
        }

        public bool IsCancelled
        {
            get { return false; }
        }
    }
}
