using System;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
using System.Threading;

namespace KelimeEzber.General
{
	public class DataGridFuncs
	{
		private DataGrid MyDataGrid;
		private DataTable MyTable;
		private Excel.Application  ExcelObj = null; 
		private Excel._Workbook WorkBookObj;
		private Excel._Worksheet WorkSheetObj;
		private Excel.Range RangeObject;
		//private Thread _oFillThread = null;
		
		public DataGridFuncs()
		{
			MyDataGrid = null;
		}

		~DataGridFuncs()
		{
			Dispose();
		}

		public void Dispose()
		{
			if (ExcelObj != null) 			ExcelObj.Quit();
			MyDataGrid = null;
			ExcelObj =null;
			//if (_oFillThread!=null) StopFillThread();
		}

		/*public void DataGridExportToExcel(DataGrid comingDataGrid)
		{
			MyDataGrid = comingDataGrid;
			if(MyDataGrid != null && MyDataGrid.DataSource != null && MyDataGrid.DataSource is DataTable)
			{
				MyTable = (DataTable)comingDataGrid.DataSource;

				_oFillThread = new Thread(new ThreadStart(TableExportToExcel));

				_oFillThread.Start();
			}
		}

		private void StopFillThread()
		{
			try
			{
				if (_oFillThread != null)
				{
					_oFillThread.Abort();
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message.ToString());
			}
		}

		public void StopDataGridExportToExcel() 
		{
			StopFillThread();
			MessageBox.Show("Excel' e Aktarma İşlemi Durduruldu.");
		}*/

		//private void TableExportToExcel()

		public void DataGridExportToExcel(DataGrid comingDataGrid)
		{
			int iCol=0;
			int iRow=0;
			int iAktarilacak=0;
			string sep = string.Empty;

			MyDataGrid = comingDataGrid;
			if (!(MyDataGrid != null && MyDataGrid.DataSource != null && MyDataGrid.DataSource is DataTable))
			 return;
				
			MyTable = (DataTable)comingDataGrid.DataSource;
			try 
			{
				ExcelObj = new Excel.Application();

				if (ExcelObj  == null) 
				{
					MessageBox.Show("ERROR: EXCEL couldn't be started!");
				}
			
				ExcelObj.Visible = true;
				ExcelObj.UserControl = true;

				WorkBookObj = (Excel._Workbook)(ExcelObj.Workbooks.Add(Missing.Value));
				WorkSheetObj = (Excel._Worksheet)WorkBookObj.ActiveSheet;

				WorkSheetObj.Name = MyTable.TableName;
				if (MyTable.Rows.Count > 0)
				{
					iCol=0;
					foreach (DataColumn c in MyTable.Columns)
					{
						if(c.DataType != typeof(System.Guid) &&
							c.DataType != typeof(System.Byte[]))
						{
							iCol++;
							WorkSheetObj.Cells[1,iCol] = c.ColumnName;
						}
					}
					iCol=0;
					iRow=1;
					foreach(DataRow r in MyTable.Rows)
					{
						iRow++;
						iAktarilacak = MyTable.Rows.Count - iRow;
						ExcelObj.Caption = "Aktarılacak Kayıt Sayısı : " + iAktarilacak.ToString();
						sep = string.Empty;
						iCol=0;
						foreach(DataColumn c in MyTable.Columns)
						{
							if(c.DataType != typeof(System.Guid) &&
								c.DataType != typeof(System.Byte[]))
							{
								iCol++;
								if(!Convert.IsDBNull(r[c.ColumnName]))
									WorkSheetObj.Cells[iRow,iCol] = r[c.ColumnName].ToString();
							}
						}
					}
					RangeObject = WorkSheetObj.get_Range("A1", "Z1");
					RangeObject.Font.Bold=true;
					RangeObject.EntireColumn.AutoFit();
					ExcelObj.Caption = MyTable.TableName + " Aktarımı Bitti.";
				}
			}
			catch ( Exception ex )
			{
				string message = ex.Message;
				MessageBox.Show("Excel' e Aktarım Sırasında Hata Oluştu: \n" + ex.Message);
			}
		}

		public string TableToString(DataTable dt)
		{
			string strData = dt.TableName + "\r\n";
			string sep = string.Empty;
			if (dt.Rows.Count > 0)
			{
				foreach (DataColumn c in dt.Columns)
				{
					if(c.DataType != typeof(System.Guid) &&
						c.DataType != typeof(System.Byte[]))
					{
						strData += sep + c.ColumnName;
						sep = "\t";
					}
				}
				strData += "\r\n";
				foreach(DataRow r in dt.Rows)
				{
					sep = string.Empty;
					foreach(DataColumn c in dt.Columns)
					{
						if(c.DataType != typeof(System.Guid) &&
							c.DataType != typeof(System.Byte[]))
						{
							if(!Convert.IsDBNull(r[c.ColumnName]))
								strData += sep +
									r[c.ColumnName].ToString();
							else
								strData += sep + "";
							sep = "\t";
						}
					}
					strData += "\r\n";
				}
			}
			else
				strData += "\r\n---> Table was empty!";
			return strData;
		}

		public bool Kopyala(DataGrid DataGrid1) 
		{
			DataGrid1.Focus();
			SendKeys.Send ("^{C}");
			return false;
		}

		public bool Yapistir(DataGrid DataGrid1,int iRow,int iCol) 
		{
			int iFirstRow = iRow;
			int iFirstCol = iCol;

			int iTekrarSayisiCol = 0;
			int iTekrarSayisiRow = 0;

			try
			{
				General.RecordOperation islem = new General.RecordOperation();
				string strGelen = islem.getClipBoardStr();
				string [] strSatir = strGelen.Split('\n');
				string strTemp = "";
				DataRow currentRow;			

				if(strGelen != "" && DataGrid1 != null && DataGrid1.DataSource != null && DataGrid1.DataSource is DataTable)
				{
					MyTable = (DataTable)DataGrid1.DataSource;
					iTekrarSayisiRow = strGelen.Length - strGelen.Replace("\n","").Length;
					for (int i=0;i<=iTekrarSayisiRow;i++) 
					{
						if (MyTable.Rows.Count <= i + iRow) 
						{
							currentRow = MyTable.NewRow();
							MyTable.Rows.Add(currentRow);
						}
						strTemp = strSatir[i];
						iTekrarSayisiCol = strTemp.Length - strTemp.Replace("\t","").Length;
						string [] strSutun = strTemp.Split('\t');
						currentRow = MyTable.Rows[i];
						for (int j=0;j<=iTekrarSayisiCol;j++) 
						{
							if (MyTable.Columns.Count < j + iCol + 1) break;
							if (MyTable.Columns[j+iCol].ReadOnly != true)
								MyTable.Rows[i+iRow][j+iCol] = strSutun[j].Trim();
						}
					}
				}
			}
			catch ( Exception ex )
			{
				string message = ex.Message;
				MessageBox.Show("Excel' e Aktarım Sırasında Hata Oluştu: \n" + ex.Message);
				return true;
			}
			return false;
		}
	}
}