using System;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using Mesnac.Controls.Base;
using System.Collections.Generic;

namespace Mesnac.Controls.Feeding
{
    /// <summary>
    /// 螺旋给料
    /// </summary>
    [ToolboxBitmap( typeof( SpiralFeeder ) , "ICONS.SpiralFeeder.bmp" )]//新添加的代码
    public partial class SpiralFeeder : FeedingControl
    {
        #region 事件定义

        /// <summary>
        /// 报警事件
        /// </summary>
        public event EventHandler OnAlarm;

        #endregion

        #region 事件属性

        private List<DesignAction> _alarmActionList = new List<DesignAction>();

        public List<DesignAction> AlarmActionList
        {
            get { return _alarmActionList; }
            set { _alarmActionList = value; }
        }

        #endregion

        bool bNewPic = false;
        string _statusName;
        private string _alarmValue;
        private string _alarmValue_;

        /// <summary>
        /// 报警信息属性
        /// </summary>
        public string AlarmValue
        {
            get { return _alarmValue; }
            set { _alarmValue = value; }
        }
        /// <summary>
        /// 报警信息
        /// </summary>
        [Alarm]
        public string AlarmValue_
        {
            get { return _alarmValue_; }
            set { _alarmValue_ = value; }
        }

        public string StatusName
        {
            get
            {
                return _statusName;
            }
            set
            {
                _statusName = value;
            }
        }
        public enum Statuses
        {
            zsTurnOff = 0, zsTurnOn = 1, zsAlarm = 2
        }

        Statuses _status;
        string[] sImages;

        public enum Types
        {
            ttleft = 0, ttright = 1
        }

        Types _type;

        public SpiralFeeder():base()
        {
            InitializeComponent();

            Init();

            ReloadStream();

            this.Reload();
        }

        protected override void Init()
        {
            base.Init();
            _imageStream = null;
            
            sImages = new string[6];
            sImages[ 0 ] = "Mesnac.Controls.Feeding.Resources.lxgl.lxgl01.png";//
            sImages[ 1 ] = "Mesnac.Controls.Feeding.Resources.lxgl.lxgl00.gif";//
            sImages[ 2 ] = "Mesnac.Controls.Feeding.Resources.lxgl.lxgl02.gif";//
            sImages[ 3 ] = "Mesnac.Controls.Feeding.Resources.lxgl.lxgl11.png";//
            sImages[ 4 ] = "Mesnac.Controls.Feeding.Resources.lxgl.lxgl10.gif";//
            sImages[ 5 ] = "Mesnac.Controls.Feeding.Resources.lxgl.lxgl12.gif";//

            _status = Statuses.zsTurnOff;
            _type = Types.ttleft;
        }

        private void ReloadStream()
        {
            //CloseStream();
            _imageStream = _assembly.GetManifestResourceStream( sImages[ 3 * (int)_type + (int)_status ] );
        }

        public Types Type
        {
            get
            {
                return _type;
            }
            set
            {
                if (bFirstCreated == true)
                {
                    bFirstCreated = false;
                }
                bool flag = false;
                if (_type != value)
                {
                    flag = true;
                    if (this.DesignMode == true)
                    {
                        bNewPic = true;
                    }
                }

                _type = value;
                if (flag)
                {
                    ReloadStream();
                    this.Reload();
                }
            }
        }
        public Statuses Status
        {
            get
            {
                return _status;
            }
            set
            {
                if (bFirstCreated == true)
                {
                    bFirstCreated = false;
                }
                bool flag = false;
                if ( _status != value )
                {
                    flag = true;
                    if (this.DesignMode == true)
                    {
                        bNewPic = true;
                    }

                }

                if ( (int)value < 0 )
                {
                    _status = (Statuses)0;
                }
                else if ( (int)value > 2 )
                {
                    _status = (Statuses)2;
                }
                else
                {
                    _status = value;
                }
                if (flag)
                {
                    ReloadStream();
                    this.Reload();

                    if (_status == Statuses.zsAlarm)
                    {
                        this._alarmValue_ = this._alarmValue;   //设置报警信息
                    }
                    else
                    {
                        this._alarmValue_ = String.Empty;       //清空报警信息
                    }
                    //有事件订阅者,则触发报警事件
                    if (OnAlarm != null)
                    {
                        OnAlarm(this, System.EventArgs.Empty);
                    }
                }
            }
        }
        private void Reload()
        {
            if (_imageStream != null)
            {
                //Graphics g = e.Graphics;

                Image img = Image.FromStream(_imageStream);


                if (bNewPic == true)
                {
                    this.Size = img.Size;
                    bNewPic = false;
                }
                pictureBox1.Image = img;
                pictureBox1.Refresh();
                //g.DrawImage(img, 0, 0, this.Width, this.Height);
            }

        }
        protected override void OnPaint(PaintEventArgs e)
        {

        }

        private void SpiralFeeder_Load(object sender, EventArgs e)
        {
            if (bFirstCreated == true && this.DesignMode == true)
            {
                bNewPic = true;
                bFirstCreated = false;

                Reload();
            }

        }

    }
}