using Autodesk.Revit.DB;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace uBIM_EarthTools
{
    class ExcelHelper
    {
        /// <summary>
        /// 读取地质数据
        /// 原数据单位为M,转换为MM
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        public static List<Borehole> GetDataFromExcel(string filePath)
        {
            List<Borehole> boreholeList = new List<Borehole>();
            Microsoft.Office.Interop.Excel.Workbook wb = null;
            Microsoft.Office.Interop.Excel.Worksheet ws = null;
            object missing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();//lauch excel application
            excel.DisplayAlerts = false;
            if (excel != null)
            {
                excel.Visible = false;
                excel.UserControl = true;// 以只读的形式打开EXCEL文件
                wb = excel.Workbooks.Open(filePath, missing, true, missing, missing, missing,
                 missing, missing, missing, true, missing, missing, missing, missing, missing);

                ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.Item[1];
                Dictionary<string, string> typeList = new Dictionary<string, string>();
                for (int i = 1; i < ws.UsedRange.Rows.Count + 1; i++)
                {
                    typeList.Add(ws.Cells[i, 1].Text, ws.Cells[i, 2].Text);
                }



                ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.Item[2];
                //取得总记录行数(包括标题列) 
                int rowsint = ws.UsedRange.Rows.Count; //得到行数 
                                                       //int rowsint = begin_row + 2*10;
                int columnsint = ws.UsedRange.Columns.Count;//得到列数 
                for (int i = 1; i <= rowsint; i += 2)
                {
                    Borehole borehole = new Borehole();
                    borehole.Name = ws.Cells[i, 1].Text;
                    double x = double.Parse(ws.Cells[i, 2].Text) * 1000 / 304.8;
                    double y = double.Parse(ws.Cells[i, 3].Text) * 1000 / 304.8;
                    for (int j = 4; j <= columnsint; j++)
                    {
                        if (null == ws.Cells[i + 1, j].Text || ws.Cells[i + 1, j].Text == "")
                            break;
                        else
                        {
                            string type = string.Empty;
                            try
                            {
                                type = "地质" + ws.Cells[i, j].Text + "-" + typeList[ws.Cells[i, j].Text] + "层";

                            }
                            catch (Exception e)
                            {
                                MessageBox.Show(i.ToString() + "," + j.ToString());
                                throw;
                            }
                             double z = double.Parse(ws.Cells[i+1, j].Text) * 1000 / 304.8;
                            XYZ xyz = new XYZ(x, y, z);
                            GeologyLayer geologyLayer = new GeologyLayer(type, xyz);
                            borehole.ValueList.Add(geologyLayer);
                        }
                    }
                    boreholeList.Add(borehole);
                }
            }
            KillProcess(excel);
            return boreholeList;
        }
        public static List<string> GetGeologyType(string filePath)
        {
            List<string> types = new List<string>();
            Microsoft.Office.Interop.Excel.Workbook wb = null;
            Microsoft.Office.Interop.Excel.Worksheet ws = null;
            object missing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();//lauch excel application
            excel.DisplayAlerts = false;
            if (excel != null)
            {
                excel.Visible = false;
                excel.UserControl = true;// 以只读的形式打开EXCEL文件
                wb = excel.Workbooks.Open(filePath, missing, true, missing, missing, missing,
                 missing, missing, missing, true, missing, missing, missing, missing, missing);

                ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.get_Item(1);
                for (int i = 1; i < ws.UsedRange.Rows.Count + 1; i++)
                {
                    types.Add("地质" + ws.Cells[i, 1].Text + "-" + ws.Cells[i, 2].Text + "层");
                }
            }
            KillProcess(excel);
            return types;
        }



        #region 结束excel进程
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        private static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
        private static void KillProcess(Microsoft.Office.Interop.Excel.Application excel)
        {
            IntPtr t = new IntPtr(excel.Hwnd);
            int k = 0;
            GetWindowThreadProcessId(t, out k);
            System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
            p.Kill();
        }
        #endregion
    }
}
