using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using SouthSmart.GISService.Infrastructure.WebGIS;

namespace SouthSmart.GISService.Infrastructure.Module
{
    public static class ArcgisJsonHelper
    {
        public static WebGISPolygon GeometryConvert2WebGISPolygon ( this IGeometry geometry )
        {

            //图形转为点集
            IPointCollection pPointColl = geometry as IPointCollection;
            if ( pPointColl.PointCount == 0 )
            {
                return null;
            }
            var listPositionArr = new List<List<double[]>> ();
            var listPosition = new List<double[]> ();
            //遍历点集
            for ( int j = 0; j < pPointColl.PointCount; j++ )
            {
                var point = pPointColl.get_Point ( j );
                //输出点坐标
                listPosition.Add ( new double[] { point.X, point.Y } );
            }
            if ( listPosition.Count >= 4 )
            {
                if ( listPosition[0][0] == listPosition[listPosition.Count - 1][0] && listPosition[0][1] == listPosition[listPosition.Count - 1][1] )
                {
                    listPositionArr.Add ( listPosition );
                    var polygon = new WebGISPolygon ()
                    {
                        rings = listPositionArr,
                        type = "polygon",
                        spatialReference = new WebGISSpatialReference () { wkid = Convert.ToInt32 ( ConfigurationManager.AppSettings["SpatialReference"] ) }
                    };
                    return polygon;
                }

            }
            else
            {
                return null;
            }
            return null;

        }
        public static List<Infrastructure.WebGIS.WebGISPolygon> FeaturesConvert2WebGISPolygon ( this List<IFeature> features )
        {
            var listGeometry = new List<Infrastructure.WebGIS.WebGISPolygon> ();
            for ( int i = 0; i < features.Count; i++ )
            {
                var thisFeature = features[i];
                switch ( features[i].Shape.GeometryType )
                {
                    case esriGeometryType.esriGeometryPolygon:
                        var geometryConvert = thisFeature.Shape.GeometryConvert2WebGISPolygon ();
                        if ( geometryConvert != null )
                            listGeometry.Add ( geometryConvert );
                        break;
                    default:
                        throw new ArgumentOutOfRangeException ();
                }
            }
            return listGeometry;
        }
        public static List<Infrastructure.WebGIS.WebGISPolygon> GeometryConvert2WebGISPolygon ( this List<IGeometry> geometrys )
        {
            var listGeometry = new List<Infrastructure.WebGIS.WebGISPolygon> ();
            for ( int i = 0; i < geometrys.Count; i++ )
            {
                var geometry = geometrys[i];
                switch ( geometrys[i].GeometryType )
                {
                    case esriGeometryType.esriGeometryPolygon:
                        var geometryConvert = geometry.GeometryConvert2WebGISPolygon ();
                        if ( geometryConvert != null )
                            listGeometry.Add ( geometryConvert );
                        break;
                    default:
                        throw new ArgumentOutOfRangeException ();
                }
            }
            return listGeometry;
        }
        public static string GeometryToJsonString ( this IGeometry geometry )
        {
            try
            {
                IJSONWriter jsonWriter = new JSONWriterClass ();
                jsonWriter.WriteToString ();
                JSONConverterGeometryClass jsonCon = new JSONConverterGeometryClass ();
                jsonCon.WriteGeometry ( jsonWriter, null, geometry, false );
                return Encoding.UTF8.GetString ( jsonWriter.GetStringBuffer () );
            }
            catch ( Exception ex )
            {
                throw ex;
            }
        }
        /// <summary>    
        /// JSON字符串转成IGeometry    
        /// </summary>    
        public static IGeometry GeometryFromJsonString ( string strJson, esriGeometryType type, bool bHasZ, bool bHasM )
        {
            try
            {
                IJSONReader jsonReader = new JSONReaderClass ();
                jsonReader.ReadFromString ( strJson );

                JSONConverterGeometryClass jsonCon = new JSONConverterGeometryClass ();
                return jsonCon.ReadGeometry ( jsonReader, type, bHasZ, bHasM );
            }
            catch ( Exception ex )
            {
                throw ex;
            }
        }

    }
}