using System.IO.Compression;
using System.Text;
using NetTopologySuite.Geometries;
using NetTopologySuite.IO;

namespace Infrastructure.Helpers;

public class ShpUtil
{
    public static bool CheckShpFileIntegrity(string shpFile)
    {
        string[] requiredExtensions = { ".shp", ".shx", ".dbf" };
        string[] optionalExtensions = { ".prj", ".cpg" };
        var directoryPath = Path.GetDirectoryName(shpFile);
        var baseFileName = Path.GetFileNameWithoutExtension(shpFile);
        foreach (var ext in requiredExtensions)
        {
            var filePath = Path.Combine(directoryPath, baseFileName + ext);
            if (!File.Exists(filePath))
            {
                Console.WriteLine($"缺失文件: {filePath}");
                return false;
            }
        }

        // 检查可选文件
        foreach (var ext in optionalExtensions)
        {
            var filePath = Path.Combine(directoryPath, baseFileName + ext);
            if (!File.Exists(filePath))
            {
                Console.WriteLine($"缺失可选文件: {filePath}");
            }
        }

        return true;
    }

    public static dynamic GetShpInfo(string shpPath)
    {
        using var dataReader = new ShapefileDataReader(shpPath, GeometryFactory.Default);
        Console.WriteLine("记录数" + dataReader.RecordCount);
        return new
        {
            Count = dataReader.RecordCount
        };
    }

    /// <summary>
    /// 读取所有数据
    /// </summary>
    /// <param name="shpPath"></param>
    /// <returns></returns>
    public static List<Dictionary<string, object>> ReadAllGeometry(string shpPath)
    {
        //  取得后缀
        var fileExtension = Path.GetExtension(shpPath);
        if (!fileExtension.Equals(".shp"))
        {
            throw new Exception("不支持的文件格式");
        }

        using var dataReader = new ShapefileDataReader(shpPath, GeometryFactory.Default);
        Console.WriteLine("记录数" + dataReader.RecordCount);
        List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
        while (dataReader.Read())
        {
            Dictionary<string, object> row = new Dictionary<string, object>();
            // 读取列
            for (int i = 0, j = 0; i < dataReader.FieldCount; i++)
            {
                var colName = dataReader.GetName(i);
                var value = dataReader.GetValue(i);
                row.Add(colName, value);
            }

            rows.Add(row);
        }

        return rows;
    }

    /// <summary>
    /// 是否有乱码
    /// </summary>
    /// <param name="content"></param>
    /// <returns></returns>
    private static bool CheckForGarbledText(string content)
    {
        try
        {
            // 检查是否有不可打印的字符
            foreach (char c in content)
            {
                if (char.IsControl(c) && !char.IsWhiteSpace(c))
                {
                    return true; // 发现不可打印的控制字符
                }
            }

            // 检查是否有不常见的字符
            foreach (char c in content)
            {
                if (c > 0x7F && !char.IsLetterOrDigit(c) && !char.IsPunctuation(c) && !char.IsSymbol(c) &&
                    !char.IsWhiteSpace(c))
                {
                    return true; // 发现不常见的字符
                }
            }

            return false; // 没有发现乱码
        }
        catch (DecoderFallbackException)
        {
            return true; // 解码失败，存在乱码
        }
        catch (Exception ex)
        {
            return true; // 其他异常，可能表示存在乱码
        }
    }
}