using System;
using System.IO;
using System.Data;
using System.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Hochbaustatistik.Business
{
    public static class CSVToExcel
    {
        public static string ReadExcel(string filename, string vorgangId)
        {
            Excel.Application excel = null;
            excel = new Excel.Application();
            excel.Visible = true;
            Excel.Workbook wkb = null;

            wkb = Open(excel, filename);
            Excel.Range searchedRange = excel.get_Range("A1", "XFD1048576");
            Excel.Range currentFind = searchedRange.Find(vorgangId);
            Excel.Sheets excelSheets = wkb.Worksheets;
            Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(1);
            string res = string.Empty;

            if (currentFind != null)
            {
                var cellValue = Convert.ToString((excelWorksheet.Cells[currentFind.Row, currentFind.Column + 10] as Excel.Range).Value);
                DateTime dt = DateTime.Parse(cellValue);
                res = dt.ToShortDateString();
            }
            else
            {
                res = "01.01.1900";
                //TODO: Logger, falls nicht gefunden. Und sonst wird 01.01.1900 als default gesetzt
            }

            wkb.Close(true);
            excel.Quit();
            return res;
        }

        public static Excel.Workbook Open(Excel.Application excelInstance,
                       string fileName, bool readOnly = false, bool editable = true,
                       bool updateLinks = true)
        {
            Excel.Workbook book = excelInstance.Workbooks.Open(
                fileName, updateLinks, readOnly,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, editable, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing);
            return book;
        }

        public static void ReleaseComObjects(ref List<object> comObjects)
        {
            for (int i = 0; i < comObjects.Count; i++)
            {
                object obj = comObjects[i];
                if (Marshal.IsComObject(obj))
                {
                    while (obj != null && Marshal.ReleaseComObject(obj) != 0) ;
                    while (obj != null && Marshal.FinalReleaseComObject(obj) != 0) ;
                }
                obj = null;
            }

            GC.Collect();
            GC.WaitForPendingFinalizers();
        }

        public static int GetColumnNumber(string column)//A --> 1 B --> 2 CI --> 87
        {
            int col = 0;
            char[] temp = column.ToUpper().ToCharArray();
            for (int i = temp.Length - 1; i >= 0; i--)
            {
                int num = Convert.ToInt32(temp[i]) - 64;
                col += num * Convert.ToInt32(Math.Pow(26, temp.Length - 1 - i));
            }
            return col;
        }
    }

}


