using System;
using System.Data;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Baza_Reklam.Classes.Helpers
{
    public class ExcelHelper
    {
        //TODO: replace ExcelHelper Export method with ExportToCsv (see below). This will remove unneccesary depedencies..
        public void Export(DataGridView source)
        {
            Excel.ApplicationClass excel = new Excel.ApplicationClass();
            excel.Application.Workbooks.Add(true);
            DataGridView table = source;
            int cIndex = 0;
            foreach (DataGridViewColumn col in table.Columns)
            {
                if (col.Visible)
                {
                    cIndex++;
                    excel.Cells[1, cIndex] = col.HeaderText;
                }
            }
            int rIndex = 0;

            foreach (DataGridViewRow row in table.Rows)
            {
                rIndex++;
                cIndex = 0;
                foreach (DataGridViewColumn col in table.Columns)
                {
                    if (col.Visible)
                    {
                        cIndex++;
                        excel.Cells[rIndex + 1, cIndex] = row.Cells[col.Name].Value == null
                                                              ? ""
                                                              : row.Cells[col.Name].Value.ToString();
                    }
                }
            }
            excel.Visible = true;
        }
        
        public void ExportToCsv(DataGridView gridView, string strFilePath)
        {
            StreamWriter sw = new StreamWriter(strFilePath, false, Encoding.Default);
            
            //write all the columns
            StringBuilder columns = new StringBuilder();
            for (int i = 0; i < gridView.Columns.Count; i++)
            {
                columns.Append(string.Format("\"{0}\",", gridView.Columns[i].HeaderText));
            }

            sw.WriteLine(columns.ToString().Remove(columns.ToString().Length-1));

            // Now write all the rows.
            StringBuilder rowBuilder;
            foreach (DataGridViewRow row in gridView.Rows)
            {
                rowBuilder = new StringBuilder();
                foreach (DataGridViewColumn column in gridView.Columns)
                    rowBuilder.Append(string.Format("\"{0}\",", row.Cells[column.Name].Value));

                sw.WriteLine(rowBuilder.ToString().Remove(rowBuilder.ToString().Length - 1));
            }
            sw.Flush();
            sw.Close();
            sw.Dispose();
        }

        public void ExportToCsv(DataTable dt, string strFilePath)
        {
            // Create the CSV file to which grid data will be exported.
            StreamWriter sw = new StreamWriter(strFilePath, false, Encoding.UTF8);

            // First we will write the headers.
            int iColCount = dt.Columns.Count;
            for (int i = 0; i < iColCount; i++)
            {
                sw.Write(string.Format("\"{0}\"", dt.Columns[i]));

                if (i < iColCount - 1) //write comma after every column, not after the last one
                    sw.Write(",");
            }

            sw.Write(sw.NewLine);

            // Now write all the rows.
            foreach (DataRow dr in dt.Rows)
            {
                for (int i = 0; i < iColCount; i++)
                {
                    if (!Convert.IsDBNull(dr[i]))
                        sw.Write(string.Format("\"{0}\"", dr[i]));

                    if (i < iColCount - 1) //write comma after every column, not after the last one
                        sw.Write(",");
                }

                sw.Write(sw.NewLine);
            }
            sw.Flush();
            sw.Close();
            sw.Dispose();
        }
    }
}