using Common;
using Infragistics.Win.UltraWinEditors;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace MeterModuleLibrary
{
    public partial class ucStorageWeightT : UserControl
    {
        public ucStorageWeightT()
        {
            InitializeComponent();
        }

        #region 线程中赋值的控件需使用委托

        private delegate void UpdateUIEventHander(object sender, UpdateUIArgs args); //自定义事件用来从线程中更新控件的值

        public class UpdateUIArgs : EventArgs
        {
            public string textValue { get; private set; }

            public UpdateUIArgs(string textValue)
            {
                this.textValue = textValue;
            }
        }

        private void UpdateUI_Method(object sender, UpdateUIArgs args)
        {
            if (sender is UltraTextEditor)
            {
                if (args.textValue == "" || args.textValue == null)
                {
                    ((UltraTextEditor)sender).Text = "0";
                }
                else
                {
                    ((UltraTextEditor)sender).Text = args.textValue;
                }
            }
            else if (sender is Button)
            {
                if (args.textValue == "red")
                {
                    ((Button)sender).BackColor = Color.Red;
                    ((Button)sender).ForeColor = Color.White;
                }
                else if (args.textValue == "green")
                {
                    ((Button)sender).BackColor = Color.White;
                    ((Button)sender).ForeColor = Color.Black;
                }
                else
                {
                    ((Label)sender).Text = args.textValue;
                }
            }
            else if (sender is PictureBox)
            {
                if (args.textValue == "red")
                {
                    ((PictureBox)sender).Load(PbCache.path + "\\image\\icon\\red.gif");
                }
                else
                {
                    ((PictureBox)sender).Load(PbCache.path + "\\image\\icon\\green.gif");
                }
            }
        }

        /// <summary>
        /// 记录当前的颜色，在线程中窑调用下面的setControl方法前
        /// 可先判断当前的颜色与显示颜色是否一致
        /// 若一致则可不调用setControl，不一致再调用
        /// </summary>
        public bool isGreen = true;

        /// <summary>
        /// 重量稳定
        /// </summary>
        /// <param name="bGreen"></param>
        public void setStable(bool bGreen)
        {
            //if (!PbCache.isLockWgt)
            {
                isGreen = bGreen;
                string sValue = isGreen ? "green" : "red";
                picStable.Invoke(new UpdateUIEventHander(UpdateUI_Method), picStable, new UpdateUIArgs(sValue));
                btnStable.Invoke(new UpdateUIEventHander(UpdateUI_Method), btnStable, new UpdateUIArgs(sValue));
            }
        }

        public double getWgt()
        {
            if (txtWeight.Text.Trim() != "")
            {
                return Math.Round(Convert.ToDouble(txtWeight.Text.Trim()) / 1000, 2);
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// 实时写入重量
        /// PbCache.isLockWgt 重量未锁定则可写入，否则调用了也不会写入
        /// </summary>
        /// <param name="db">重量信息</param>
        public void setWgt(double db)
        {
            //if (!PbCache.isLockWgt)
            {
                txtWeight.Invoke(new UpdateUIEventHander(UpdateUI_Method), txtWeight, new UpdateUIArgs(Math.Round(db / 1000, 2) + ""));
            }
        }

        #endregion 线程中赋值的控件需使用委托
    }
}