using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Common;
using Infragistics.Win.UltraWinEditors;

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


        public ucWeightCarTJXJG()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 默认加载窗格线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ucWeightCarTJXJG_Load(object sender, EventArgs e)
        {
            tableLayoutPanelWeigtAndType.CellBorderStyle = (TableLayoutPanelCellBorderStyle)BorderStyle.FixedSingle;
        }

        #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;
            }
        }

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

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

        /// <summary> 
        /// 写入重量类型
        /// PbCache.isLockWgt 重量未锁定则可写入，否则调用了也不会写入
        /// </summary>
        /// <param name="db">重量信息</param>
        public void setWeightType(string db)
        {
            if (!PbCache.isLockWgt)
            {
                if (txtWeightType.IsHandleCreated)
                {
                    txtWeightType.Invoke(new UpdateUIEventHander(UpdateUI_Method), txtWeightType, new UpdateUIArgs(db + ""));
                }
            }
        }

        /// <summary>
        /// 设置重量字体的背景颜色:Lime及NotShow（不显示）
        /// </summary>
        /// <param name="db"></param>
        public void setWgtBackColor(bool bLime)
        {
            if (!PbCache.isLockWgt)
            {
                if (txtWeight.IsHandleCreated)
                {
                    isGreenWgt = bLime;
                    string sValue = isGreenWgt ? "Lime" : "NotShow";
                    txtWeight.Invoke(new UpdateUIEventHander(UpdateUIBackColor_Method), txtWeight, new UpdateUIArgs(sValue));
                }
            }
        }

        /// <summary>
        /// 设置背景色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void UpdateUIBackColor_Method(object sender, UpdateUIArgs args)
        {
            if (sender is UltraTextEditor)
            {
                if (args.textValue == "Lime")
                {
                    ((UltraTextEditor)sender).Appearance.BackColor = Color.Black;
                    ((UltraTextEditor)sender).Appearance.ForeColorDisabled = Color.Black;
                }
                else
                {
                    //System.Drawing.Color.Transparent;跟随背景色
                    ((UltraTextEditor)sender).Appearance.BackColor = Color.Transparent;
                    ((UltraTextEditor)sender).Appearance.ForeColorDisabled = Color.Transparent;
                    //((UltraTextEditor)sender).Appearance.BackColor = Color.FromArgb(((int)(((byte)(236)))), ((int)(((byte)(244)))), ((int)(((byte)(252))))); 
                    //((UltraTextEditor)sender).Appearance.ForeColorDisabled = Color.FromArgb(((int)(((byte)(236)))), ((int)(((byte)(244)))), ((int)(((byte)(252)))));
                }
            }
        }

        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\\不具备.png");
                }
                else
                {
                    ((PictureBox)sender).Load(PbCache.path + "\\image\\icon\\具备.png");
                }
            }
        }

        #endregion

    }
}
