using System.Collections.Generic;
using System;
using System.Drawing;
using System.Diagnostics;
using System.Data;
using System.Collections;
using System.Windows.Forms;
using System.Linq;
namespace vmsnet
{
    sealed class PUBC
	{
		
		/// 
		/// 설정된 DEVICE 의 채널 갯수를반환
		/// 
		/// 
		/// 
		public static int GetChannelCount()
		{
			int chcount = 0;
			foreach (DocumentElement.DEVICERow DR in PUB.DS.DEVICE.Select("use=1")) // Dt.Rows
			{
				string buf = DR.CHCOUNT;
				if (buf.Trim() == "")
				{
					continue;
				}
				foreach (string C in buf.Split(",".ToCharArray()))
				{
					int cc = 0;
					if (C.IndexOf("*") == -1)
					{
						cc = int.Parse(C);
					}
					else
					{
						cc = System.Convert.ToInt32(C.Split("*".ToCharArray())[1]);
					}
					chcount += cc;
				}
			}
			return chcount;
		}
		
		public static int GetUseDeviceCount()
		{
			return PUB.DS.DEVICE.Select("use=1").Length;
		}
		
		public static int GetWindowCount()
		{
			var dr = PUB.DS.WIN.Select("use=1") as DocumentElement.WINRow[];
			return dr.Length;
		}
		
		public static string GetWindowName(int idx)
		{
			var dr = PUB.DS.WIN.Select("IDX=" + System.Convert.ToString(idx)) as DocumentElement.WINRow[];
			if (dr.Length != 1)
			{
				return "";
			}
			return dr[0].TITLE;
		}
		
		public static string GetGrpName(int idx)
		{
			var dr = PUB.DS.GRP.Select("IDX=" + System.Convert.ToString(idx)) as DocumentElement.GRPRow[];
			if (dr.Length != 1)
			{
				return "";
			}
			return dr[0].TITLE;
		}
		
		public static string GetChannelName(int idx)
		{
			var dr = PUB.DS.CHANNEL.Select("IDX=" + System.Convert.ToString(idx)) as DocumentElement.CHANNELRow[];
			if (dr.Length != 1)
			{
				return "";
			}
			return dr[0].TITLE;
		}
		
		/// 
		/// 널밸런스 옵셋값을 설정합니다.
		/// 
		/// 
		/// 
		/// 
		/// 
		public static bool UpdateNullBalanceOffset(int idx, float nb)
		{
			//Dim sql As String = "UPDATE GRP set NBOFF=" & value & " WHERE IDX=" & idx
			var DR = PUB.DS.GRP.Select("idx=" + idx.ToString()) as DocumentElement.GRPRow[];
			if (DR.Length != 1)
			{
				return false;
			}
			DR[0].NBOFF = nb;
			DR[0].AcceptChanges();
			PUB.DS.GRP.AcceptChanges();
			return true;
		}
		
		/// 
		/// viewgoupr에 데이터를 넣습니다.
		/// 
		/// 
		/// 
		/// 
		/// 
		/// 
		public static short AddviewGroup(string title, string val, string gname)
		{
			var maxidrow = PUB.DS.VIEWGROUP.Select("", "idx desc") as DocumentElement.VIEWGROUPRow[];
			short maxid = (short) 0;
			if (maxidrow.GetUpperBound(0) == -1)
			{
				maxid = (short) 1;
			}
			else
			{
				////160315 error
				maxid = (short) (maxidrow[0].IDX + 1);
			}
			
			try
			{
				DocumentElement.VIEWGROUPRow newdr = PUB.DS.VIEWGROUP.NewVIEWGROUPRow();
				newdr.IDX = maxid;
				newdr.TITLE = title;
				newdr.VAL = val;
				newdr.GNAME = gname;
				PUB.DS.VIEWGROUP.AddVIEWGROUPRow(newdr);
				PUB.DS.VIEWGROUP.AcceptChanges();
				return maxid;
			}
			catch (Exception)
			{
				return (short)  -1;
			}
		}
		
		public static bool DeleteViewGroup(int gidx, string gname)
		{
			var dr = PUB.DS.VIEWGROUP.Select("idx=" + gidx.ToString() + " and gname='" + gname + "'") as DocumentElement.VIEWGROUPRow[];
			if (dr.Length == 1)
			{
				PUB.DS.VIEWGROUP.Rows.Remove(dr[0]);
				PUB.DS.VIEWGROUP.AcceptChanges();
			}
			else
			{
				return false;
			}
			return true;
		}
		
		public static bool UpdateGroup(int idx, string alamtype, float alamh, float alaml, float autoh, float autol, float nbh, float nbl)
		{
			//string sql = "update GRP set ALAMTYPE=@ALAMTYPE,ALAMH=@ALAMH,ALAML=@ALAML,AUTOH=@AUTOH,AUTOL=@AUTOL,NBH=@NBH,NBL=@NBL WHERE IDX=" + System.Convert.ToString(idx);
			var dr = PUB.DS.GRP.Select("idx=" + idx.ToString()) as DocumentElement.GRPRow[];
			if (dr.Length == 1)
			{
				DocumentElement.GRPRow Drow = dr[0];
				Drow.ALAMTYPE = alamtype;
				Drow.ALAMH = alamh;
				Drow.ALAML = alaml;
				Drow.AUTOH = autoh;
				Drow.AUTOL = autol;
				Drow.NBH = nbh;
				Drow.NBL = nbl;
				Drow.AcceptChanges();
				PUB.DS.AcceptChanges();
			}
			else
			{
				return false;
			}
			return true;
		}
		
		public static dynamic UpdateWin(int idx, string title)
		{
			//string sql = "UPDATE WIN set TITLE=@TITLE WHERE IDX=@IDX";
			var Drow = PUB.DS.WIN.Where(t => t.IDX == idx).FirstOrDefault();// Select("idx=" + idx.ToString()) as DocumentElement.WINRow[];
			if (Drow != null)
			{
				//DocumentElement.WINRow Drow = dr[0];
				Drow.TITLE = title;
                Drow.EndEdit();
                Drow.AcceptChanges();
				PUB.DS.AcceptChanges();
			}
			else
			{
				return false;
			}
			return true;
		}
		
		public static bool UpdateChannel(int idx, string title, int alamtype, int enable, float alamh, float alaml, float autoh, float autol)
		{
			//string sql = "UPDATE CHANNEL set TITLE=@TITLE,ALAMTYPE=@ALAMTYPE,ENABLE=@ENABLE,ALAMH=@ALAMH,ALAML=@ALAML,AUTOH=@AUTOH,AUTOL=@AUTOL WHERE IDX=@IDX";
			var Drow = PUB.DS.CHANNEL.Where(t => t.IDX == idx).FirstOrDefault();// .Select("idx=" + idx.ToString()) as DocumentElement.CHANNELRow[];
            if (Drow != null)
            {
				//DocumentElement.CHANNELRow Drow = dr[0];
				Drow.TITLE = title;
				Drow.ALAMTYPE = alamtype;
				Drow.ENABLE = enable;
				Drow.ALAMH = alamh;
				Drow.ALAML = alaml;
				Drow.AUTOH = autoh;
				Drow.AUTOL = autol;
				Drow.EndEdit();
				Drow.AcceptChanges();
				PUB.DS.AcceptChanges();
			}
			else
			{
				return false;
			}
			return true;
		}
		
	}
	
}