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

namespace Mesnac.Controls.Feeding
{
    /// <summary>
    /// 通断阀
    /// </summary>
    [ToolboxBitmap( typeof( OnoffValve ) , "ICONS.OnoffValve.bmp" )]//新添加的代码
    public partial class OnoffValve : 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
        {
            //zsTurnOn = 0, zsTurnOff = 1, zsAlarm = 2
            zsTurnOff = 0, zsTurnOn = 1, zsAlarm = 2
        }

        Statuses _status;
        string[,] sImages;

        public enum Types
        {
            ttHorizontal = 0, ttVertical = 1
        }

        Types _type;
        
        public OnoffValve():base()
        {
            InitializeComponent();

            Init();

            ReloadStream();

            this.Reload();
        }

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

            sImages = new string[ 2 , 3 ];
            sImages[ 0 , 0 ] = "Mesnac.Controls.Feeding.Resources.tdf.FMTP20.png";//dkNull
            sImages[ 0 , 1 ] = "Mesnac.Controls.Feeding.Resources.tdf.FMTP21.png";//dkNull
            sImages[ 0 , 2 ] = "Mesnac.Controls.Feeding.Resources.tdf.FMTP2ARM.gif";//dkNull
            sImages[ 1 , 0 ] = "Mesnac.Controls.Feeding.Resources.tdf.FMTP10.png";//dkNull
            sImages[ 1 , 1 ] = "Mesnac.Controls.Feeding.Resources.tdf.FMTP11.png";//dkNull
            sImages[ 1 , 2 ] = "Mesnac.Controls.Feeding.Resources.tdf.FMTP1ARM.gif";//dkNull

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

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

        public Types Type
        {
            get
            {
                return _type;
            }
            set
            {
                if (bFirstCreated == true)
                {
                    bFirstCreated = false;
                }

                if (_type != value)
                {
                    if (this.DesignMode == true)
                    {
                        bNewPic = true;
                    }
                }

                _type = value;
                ReloadStream();
                //this.Refresh();
                this.Reload();
            }
        }
        public Statuses Status
        {
            get
            {
                return _status;
            }
            set
            {
                if (bFirstCreated == true)
                {
                    bFirstCreated = false;
                }
                bool flag = false;
                //if (_status != value &&(this.Size.Width != 150 || this.Size.Height != 150)  )
                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.Refresh();
                    this.Reload();

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

                #region 2013-4-18 liym
                if ( this.DesignMode )
                {
                    foreach ( Component cmpnt in this.Container.Components )
                    {
                        if ( cmpnt is StraightTube2 )
                        {
                            if ( ( (StraightTube2)cmpnt ).Master == this.Name )
                            {
                                ( (StraightTube2)cmpnt ).SetStatus( (Statuses)value );
                            }
                        }
                    }
                }
                else if ( this.Parent != null )
                {
                    foreach ( Component cmpnt in this.Parent.Controls )
                    {
                        if ( cmpnt is StraightTube2 )
                        {
                            if ( ( (StraightTube2)cmpnt ).Master == this.Name )
                            {
                                ( (StraightTube2)cmpnt ).SetStatus( (Statuses)value );
                            }
                        }
                    }
                }
                #endregion
            }
        }

        private void Reload()
        {
            if (_imageStream != null)
            {
                Image img = Image.FromStream(_imageStream);
                if (bNewPic == true)
                {
                    this.Size = img.Size;
                    bNewPic = false;
                }
                pictureBox1.Image = img;
                pictureBox1.Refresh();
            }

        }

        protected override void OnPaint(PaintEventArgs e)
        {
            //if (_imageStream != null)
            //{
            //    Graphics g = e.Graphics;
            //    Image img = Image.FromStream(_imageStream);
            //    //if (_type == Types.ttHorizontal)
            //    //{
            //    //    //img.RotateFlip(RotateFlipType.
            //    //}
            //    //else if (_type == Types.ttVertical)
            //    //{
            //    //    img.RotateFlip(RotateFlipType.Rotate90FlipNone);
            //    //}

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

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

        }

    }
}