using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using static MedicineAllConfig;

public static class MedicineAllConfigConfiguration
{
    public static string ExcellPath = Application.dataPath + "/Config/MedicineAllConfig.xlsx";

    public static string excellDataPath = "Assets/Resources/Config/Excel/MedicineAllConfig.asset";







    public static int id = 0;
    public static int name = 1;

    public static int pinType = 2;

    public static int characteristic = 3;

    public static int growIntroduce = 4;
    public static int morphology = 5;


    public static void UpdateConfig()
    {
        MedicineAllConfig config = ScriptableObject.CreateInstance<MedicineAllConfig>();


        var info = ExcelTool.ReadExcel(ExcellPath);
        try
        {
            if (info.Item1 == null)
            {

                File.Delete(info.Item2);
                Debug.LogError("读表失败！");
                return;
            }

            List<MedicineEle> items = new List<MedicineEle>();
            MedicineEle medele;
            // 遍历工作表的行
            for (int rowIndex = 2; rowIndex <= info.Item1.LastRowNum; rowIndex++)
            {
                IRow row = info.Item1.GetRow(rowIndex);
                if (row != null)
                {
                    medele = new MedicineEle();
                    if (string.IsNullOrEmpty(row.GetCell(id).GetCellContent()))
                        continue;



                    //// 遍历行中的单元格
                    medele.id = int.Parse(row.GetCell(id).GetCellContent());
                    medele.name = string.IsNullOrEmpty(row.GetCell(name).ToString()) ? "" : row.GetCell(name).ToString();
                    medele.pinType = string.IsNullOrEmpty(row.GetCell(pinType).ToString()) ? char.MinValue : char.Parse(row.GetCell(pinType).ToString());
                    medele.characteristic = string.IsNullOrEmpty(row.GetCell(characteristic).ToString()) ? "" : row.GetCell(characteristic).ToString();
                    medele.growIntroduce = string.IsNullOrEmpty(row.GetCell(growIntroduce).ToString()) ? "" : row.GetCell(growIntroduce).ToString();
                    medele.morphology = string.IsNullOrEmpty(row.GetCell(morphology).ToString()) ? "" : row.GetCell(morphology).ToString();
                    items.Add(medele);
                }
            }

            config.list = items;


            File.Delete(info.Item2);




            config.SaveConfig(excellDataPath);

        }
        catch (global::System.Exception e)
        {
            File.Delete(info.Item2);


            Debug.LogException(e);
        }

    }

}



public static class ExcellReadWrite
{



    [MenuItem("Tools/更新表格")]
    static void UpdateConfig()
    {
        MedicineAllConfigConfiguration.UpdateConfig();




        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();

        Debug.Log("更新完成！");

    }


    public static void SaveConfig(this ScriptableObject config, string excellAssetPath)
    {
        if (string.IsNullOrEmpty(AssetDatabase.GetAssetPath(config)))
            AssetDatabase.CreateAsset(config, excellAssetPath);

        EditorUtility.SetDirty(config);


    }

    // 获取单元格的文本内容
    public static string GetCellContent(this ICell cell)
    {
        if (cell != null)
        {
            switch (cell.CellType)
            {
                case CellType.String:
                    return cell.StringCellValue;
                case CellType.Numeric:
                    if (DateUtil.IsCellDateFormatted(cell))
                    {
                        return cell.DateCellValue.ToString();
                    }
                    else
                    {
                        return cell.NumericCellValue.ToString();
                    }
                case CellType.Boolean:
                    return cell.BooleanCellValue.ToString();
                case CellType.Formula:
                    return cell.CellFormula;
                case CellType.Blank:
                    return string.Empty;
                default:
                    return cell.ToString();
            }
        }
        return string.Empty;
    }



}
