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

namespace Mesnac.Controls.Feeding
{
    /// <summary>
    /// 只管
    /// </summary>
    [ToolboxBitmap( typeof( StraightTube2 ) , "ICONS.StraightTube.bmp" )]//新添加的代码
    public partial class StraightTube2 : FeedingControl
    {
        public enum Types
        {
            ptLeftToRight = 0 ,
            ptRightToLeft = 1 ,
            ptUpToDown = 2 ,
            ptDownToUp = 3
        }
        public enum Statuses
        {
            ptTurnOn = 0 ,
            ptTurnOff = 1
        }

        #region 变量
        Types _type;
        Statuses _status;
        string[] sImages;
        private string _master;
        string _statusName;
        //int iWidth=45;
        //int iHeight=5;
        #endregion

        #region 属性
        public string StatusName
        {
            get
            {
                return _statusName;
            }
            set
            {
                _statusName = value;
            }
        }
        public Types Type
        {
            get
            {
                return _type;
            }
            set
            {
                if (bFirstCreated == true)
                {
                    bFirstCreated = false;
                }

                Types _tmpType = _type;
                _type = value;
                if ( ( ( _tmpType == Types.ptLeftToRight || _tmpType == Types.ptRightToLeft ) && ( value == Types.ptDownToUp || value == Types.ptUpToDown ) )
                    || ( ( value == Types.ptLeftToRight || value == Types.ptRightToLeft ) && ( _tmpType == Types.ptDownToUp || _tmpType == Types.ptUpToDown ) ) )
                {
                    int i = this.Width;
                    this.Width = this.Height;
                    this.Height = i;
                }

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

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

        [TypeConverter( typeof( StringListConverterStraightTube2 ) )]
        public string Master
        {
            get
            {
                return _master;
            }
            set
            {
                _master = value;
            }
        }
       #endregion


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

            Init();

            ReloadStream();

            this.Reload();
        }

        #region 方法
        public void SetStatus( OnoffValve.Statuses s )
        {
            switch(s)
            {
                case OnoffValve.Statuses.zsTurnOn:
                    _status = Statuses.ptTurnOn;
                    break;
                case OnoffValve.Statuses.zsTurnOff:
                    _status = Statuses.ptTurnOff;
                    break;
                default:
                    break;
            }
            ReloadStream();
            this.Reload();
        }

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

            sImages = new string[ 6 ];
            sImages[ 0 ] = "Mesnac.Controls.Feeding.Resources.zg.ZGHBlack1.gif";//
            sImages[ 1 ] = "Mesnac.Controls.Feeding.Resources.zg.ZGHBlack2.gif";//
            sImages[ 2 ] = "Mesnac.Controls.Feeding.Resources.zg.ZGVBlack1.gif";//
            sImages[ 3 ] = "Mesnac.Controls.Feeding.Resources.zg.ZGVBlack2.gif";//
            sImages[ 4 ] = "Mesnac.Controls.Feeding.Resources.zg.ZGH.png";//
            sImages[ 5 ] = "Mesnac.Controls.Feeding.Resources.zg.ZGV.png";//
        }

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

        private void Reload()
        {
            if ( _imageStream != null )
            {
                Image img = Image.FromStream( _imageStream );
                pictureBox1.Image = img;
                pictureBox1.Refresh();
            }

        }

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

                Reload();
            }
        }
        #endregion

        private void StraightTube2_Resize( object sender , EventArgs e )
        {
            if ( _type == Types.ptLeftToRight || _type == Types.ptRightToLeft )
            {
                this.Height = 5;
                //this.Width = Math.Max( this.Width , 45 );
            }
            else if ( _type == Types.ptDownToUp || _type == Types.ptUpToDown )
            {
                this.Width = 5;
                //this.Height = Math.Max( this.Height , 45 );
            }
        }
    }

    public class StringListConverterStraightTube2 : StringListConverterParent
    {
        public override StandardValuesCollection GetStandardValues( ITypeDescriptorContext context )
        {
            List<string> list=new List<string>();
            System.ComponentModel.Design.IDesignerHost host = context.GetService( typeof( System.ComponentModel.Design.IDesignerHost ) ) as System.ComponentModel.Design.IDesignerHost;

            foreach ( Component cmpnt in host.Container.Components )
            //foreach ( Component cmpnt in context.Container.Components )
            {
                if ( cmpnt is OnoffValve )
                {
                    list.Add( cmpnt.Site.Name );
                }
            }
            return new StandardValuesCollection( list );
        }
    }
}