using System;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace Mesnac.Controls.Feeding
{
    /// <summary>
    /// 胶料称
    /// </summary>
    [ToolboxBitmap( typeof( RubberWeight ) , "ICONS.RubberWeight.bmp" )]//新添加的代码
    public partial class RubberWeight : FeedingControl
    {
        bool bNewPic = false;
        bool bSreamPic = false; //是否刷新图片
        string _statusName;
        private string _motorStatusName;
        private string _WeightValueName;
        private double _setValue = 10;
        private double _weightValue;

        public string StatusName
        {
            get
            {
                return _statusName;
            }
            set
            {
                _statusName = value;
            }
        }
        /// <summary>
        /// 称量值动画属性
        /// </summary>
        public string WeightValueName
        {
            get { return _WeightValueName; }
            set { _WeightValueName = value; }
        }
        /// <summary>
        /// 电机状态
        /// </summary>
        public string MotorStatusName
        {
            get { return _motorStatusName; }
            set { _motorStatusName = value; }
        }

        public enum Statuses
        {
            //rwNull = 0, rwWeight = 1, wFull = 2, rwUnload = 3, rwAlarm = 4
            rwNull = 0, rwUnload = 1, rwFull = 2, rwWeight =3
        }

        Statuses _status;
        private OnOffStatuses _motorStatus;

        string[] sImages;
        public RubberWeight():base()
        {
            InitializeComponent();

            Init();

            ReloadStream();
            this.Reload();
        }

        protected override void Init()
        {
            base.Init();
            _imageStream = null;

            sImages = new string[ 5 ];
            sImages[0] = "Mesnac.Controls.Feeding.Resources.jl.jl_null.png";//
            sImages[1] = "Mesnac.Controls.Feeding.Resources.jl.jl_unload.gif";//
            sImages[2] = "Mesnac.Controls.Feeding.Resources.jl.jl_full.png";//
            sImages[3] = "Mesnac.Controls.Feeding.Resources.jl.jl_weight.gif";//
            sImages[4] = "Mesnac.Controls.Feeding.Resources.jl.jl17.png";//

            _status = Statuses.rwNull;
        }

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

        public Statuses Status
        {
            get
            {
                return _status;
            }
            set
            {
                if (bFirstCreated == true)
                {
                    bFirstCreated = false;
                }


                //if (_status != value &&(this.Size.Width != 150 || this.Size.Height != 150)  )
                if ( _status != value )
                {
                    bSreamPic = true;
                    if (this.DesignMode == true)
                    {
                        bNewPic = true;
                    }
                }

                if ( (int)value < 0 )
                {
                    _status = (Statuses)0;
                }
                else if ( (int)value > 3 )
                {
                    _status = (Statuses)3;
                }
                else
                {
                    _status = value;
                }

                if (bSreamPic)
                {
                    ReloadStream();
                    this.Reload();
                }
            }
        }
        /// <summary>
        /// 电机状态
        /// </summary>
        public OnOffStatuses MotorStatus
        {
            get { return this._motorStatus; }
            set 
            {
                bool flag = false;
                if (this._motorStatus != value)
                {
                    flag = true;
                }
                if ((int)value < 0)
                {
                    this._motorStatus = OnOffStatuses.Off;
                }
                else if ((int)value > 1)
                {
                    this._motorStatus = OnOffStatuses.On;
                }
                else
                {
                    this._motorStatus = value;
                }
                if (flag)
                {
                    this.UpdateStatus();
                }
            }
        }
        /// <summary>
        /// 更新状态
        /// </summary>
        private void UpdateStatus()
        {
            if (this._motorStatus == OnOffStatuses.On)
            {
                this.Status = Statuses.rwUnload;
            }
            else
            {
                if (this._weightValue < this._setValue)
                {
                    this.Status = Statuses.rwNull;
                }
                else
                {
                    this.Status = Statuses.rwWeight;
                }
            }
            ReloadStream();
            this.Reload();
        }

        /// <summary>
        /// 设定值
        /// </summary>
        public double SetValue
        {
            get { return _setValue; }
            set { _setValue = value; this.UpdateStatus(); }
        }
        /// <summary>
        /// 称量值
        /// </summary>
        public double WeightValue
        {
            get { return _weightValue; }
            set { _weightValue = value; this.UpdateStatus(); }
        }

        private void Reload()
        {
            if (_imageStream != null)
            {
                Image img = Image.FromStream(_imageStream);
                if (bNewPic == true)
                {
                    //this.Size = img.Size;
                    this.Size = new System.Drawing.Size(98, 54);
                    bNewPic = false;
                }
                this.pictureBox1.Image = img;
                this.pictureBox1.Refresh();
            }
        }

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

        }

    }
}