using System;
using System.Collections;
using System.IO;
using System.Data;
using NUnit.Framework;
using NUnit.Core;
using net.ivoa;
using net.ivoa.VOTable;

namespace net.ivoa.data {
	[TestFixture]
	public class DataWrapperTests {
		public DataSet ds;
		public VOTABLE vot;
		
		[TestFixtureSetUp] 
		public void Init(){
			ds = MakeDataSet();
			vot = VOTableWrapper.Wrapper2VOTable(new DataSetWrapper(ds));
		}

		private DataSet MakeDataSet(){
			DataSet d = new DataSet();
			d.Tables.Add();
			d.Tables[0].Columns.Add("number",typeof(int));
			d.Tables[0].Columns.Add("word",typeof(string));
			DataRow workRow;
			for (int i = 0; i <= 9; i++) {
				workRow = d.Tables[0].NewRow();
				workRow[0] = i;
				workRow[1] = "CustName" + i.ToString();
				d.Tables[0].Rows.Add(workRow);
			}
			return d;
		}

		[Test]
		public void VOTableWrapperVariousProperties(){
			VOTableWrapper votw = new VOTableWrapper(vot);
			Properties(votw);
		}
		[Test]
		public void DataSetWrapperVariousProperties(){
			DataSetWrapper dsw = new DataSetWrapper(ds);
			Properties(dsw);
		}
		[Test]
		public void CSVWrapperVariousProperties(){
			CSVWrapper dsw = getcsvw();
			Properties(dsw);
		}

		private CSVWrapper getcsvw(){
			FileStream fs = new FileStream("../../testfiles/messy.csv",FileMode.Open);
			CSVWrapper dsw = CSVWrapper.Stream2CSVWrapper(fs);
			fs.Close();
			return dsw;
		}

		private void Properties(DataWrapper dw){
			Assert.AreEqual(dw.ActiveTable,0);
			Assert.AreEqual(dw.TableCount,1);
			Assert.AreEqual(dw.RowCount,10);
			Assert.AreEqual(dw.ColCount,2);
			Assert.AreEqual(dw.ColName(0),"number");
			Assert.AreEqual(dw.MSColType(0),typeof(int));
		}

		[Test]
		public void DataSetWrapperRead(){
			DataSetWrapper dsw = new DataSetWrapper(ds);
			DataWrapperRead(dsw);
		}
		[Test]
		public void VOTableWrapperRead(){
			VOTableWrapper votw= new VOTableWrapper(vot);
			DataWrapperRead(votw);
		}
		[Test]
		public void CSVWrapperRead(){
			DataWrapperRead(getcsvw());
		}

		private void DataWrapperRead(DataWrapper dw){
			for (int r = 0; r < ds.Tables[0].Rows.Count; r++) 
				for(int c =0;c<ds.Tables[0].Columns.Count;c++)
					Assert.IsTrue(ds.Tables[0].Rows[r][c].ToString().CompareTo(dw[r,c].ToString()) == 0);
		}
		[Test]
		public void CSVWrapperWrite(){
			DataWrapperWrite(getcsvw());
		}
		[Test]
		public void DataSetWrapperWrite(){
			DataSet ds2 = MakeDataSet();
			DataSetWrapper dsw = new DataSetWrapper(ds2);
			DataWrapperWrite(dsw);
		}
		[Test]
		public void VOTableWrapperWrite(){
			VOTABLE vot = VOTableUtil.DataSet2VOTable(MakeDataSet());
			VOTableWrapper votw = new VOTableWrapper(vot);
			DataWrapperWrite(votw);
		}

		private void DataWrapperWrite(DataWrapper dw){
			for (int r = 0; r < dw.RowCount; r++) {
				dw[r,0] = 0;
				dw[r,1] = "nothing";
			}
			for (int r = 0; r < dw.RowCount; r++) {
				Assert.IsTrue(dw[r,0].ToString().CompareTo("0") == 0);
				Assert.IsTrue(dw[r,1].ToString().CompareTo("nothing") == 0);
			}
		}

		[Test]
		public void Wrapper2DataSet(){
			VOTABLE vot = VOTableUtil.DataSet2VOTable(ds);
			VOTableWrapper votw = new VOTableWrapper(vot);
			DataSet ds2 = DataSetWrapper.Wrapper2DataSet(votw);
			DataSetWrapper dsw = new DataSetWrapper(ds2);
			DataWrapperRead(dsw);
		}

		[Test]
		public void Wrapper2VOTable(){
			DataSetWrapper dsw = new DataSetWrapper(ds);
			VOTABLE vot = VOTableWrapper.Wrapper2VOTable(dsw);
			VOTableWrapper votw = new VOTableWrapper(vot);
			DataWrapperRead(votw);
		}
		
		[Test]
		public void DataSetWrapperAddCol(){
			DataSet ds2 = MakeDataSet();
			DataSetWrapper dsw = new DataSetWrapper(ds2);
			DataWrapperAddCol(dsw);
		}
		[Test]
		public void VOTableWrapperAddCol(){
			VOTABLE vot = VOTableUtil.DataSet2VOTable(MakeDataSet());
			VOTableWrapper votw = new VOTableWrapper(vot);
			DataWrapperAddCol(votw);
		}

		private void DataWrapperAddCol(DataWrapper dw){
			string name = "test";
			Type type = typeof(int);
			int cindex = dw.AddCol(name,type);
			Assert.AreEqual(name,dw.ColName(cindex));
			Assert.AreEqual(type,dw.MSColType(cindex));
			for (int r = 0; r < dw.RowCount; r++)
				dw[r,cindex] = r;
			for (int r = 0; r < dw.RowCount; r++)
				Assert.AreEqual(dw[r,cindex].ToString().CompareTo(r.ToString()),0);

		}


	}
}
