using System.Net;
using Newtonsoft.Json.Linq;

namespace SimulationCommon;

public class Text_readNC
{
    public NCread windNCread = new NCread();
    public void GetNCData()
    {
        string url1 = $"{Util.baseURl}rescue-platform-service/api/v1/dem/getNcInfos?ncPath=D:/data/haiyang/DATA.nc&varName=v10&centerLon=116.41992593821296&centerLat=40.18801994965735";

        // Create a GET request to the specified URL
        HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(url1);
        request1.Method = "GET";

        // Send request and get response
        using (HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse())
        {
            using (StreamReader reader1 = new StreamReader(response1.GetResponseStream()))
            {
                string result1 = reader1.ReadToEnd();

                // Parse the JSON string into a JObject
                JObject resultObject1 = JObject.Parse(result1);

                // Extract the 'data' field from the parsed JObject
                JArray dataArray1 = (JArray)resultObject1["data"];

                // Determine the dimensions of the data array
                int timeSteps = dataArray1.Count;
                int latitudes = ((JArray)dataArray1[0]).Count;
                int longitudes = ((JArray)dataArray1[0][0]).Count;

                // Initialize float[][][] array
                float[][][] v10Array = new float[timeSteps][][];

                // Convert each element to float and populate v10Array
                for (int t = 0; t < timeSteps; t++)
                {
                    v10Array[t] = new float[latitudes][];

                    for (int lat = 0; lat < latitudes; lat++)
                    {
                        v10Array[t][lat] = new float[longitudes];

                        for (int lon = 0; lon < longitudes; lon++)
                        {
                            v10Array[t][lat][lon] = (float)dataArray1[t][lat][lon];
                        }
                    }
                }

                JArray lonArray = (JArray)resultObject1["data_lon"];
                JArray lanArray = (JArray)resultObject1["data_lat"];
                //JArray tArray = (JArray)resultObject["data_time"];

                int lons = lonArray.Count;
                int lans = lanArray.Count;
                //int times = tArray.Count;

                float[] longitudeArray = new float[lons];
                float[] latitudeArray = new float[lans];
                //float[] timeArray = new float[times];

                for (int i = 0; i < lons; i++)
                {
                    longitudeArray[i] = (float)lonArray[i];
                }

                for (int j = 0; j < lans; j++)
                {
                    latitudeArray[j] = (float)lanArray[j];
                }

                /* for (int k = 0; k < lons; k++)
                   {
                       timeArray[k] = (float)tArray[k];
                   }*/

                windNCread.latitudeArray = latitudeArray;
                windNCread.longitudeArray = longitudeArray;
                windNCread.v10Array = v10Array;
                // Example: Print the first value of the array
                //Console.WriteLine("a value of timeArray: " + timeArray[0]);
            }
        }

        string url2 = $"{Util.baseURl}rescue-platform-service/api/v1/dem/getNcInfos?ncPath=D:/data/haiyang/DATA.nc&varName=u10&centerLon=116.41992593821296&centerLat=40.18801994965735";

        // Create a GET request to the specified URL
        HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(url2);
        request1.Method = "GET";

        // Send request and get response
        using (HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse())
        {
            using (StreamReader reader2 = new StreamReader(response2.GetResponseStream()))
            {
                string result2 = reader2.ReadToEnd();

                // Parse the JSON string into a JObject
                JObject resultObject2 = JObject.Parse(result2);

                // Extract the 'data' field from the parsed JObject
                JArray dataArray2 = (JArray)resultObject2["data"];

                // Determine the dimensions of the data array
                int timeSteps2 = dataArray2.Count;
                int latitudes2 = ((JArray)dataArray2[0]).Count;
                int longitudes2 = ((JArray)dataArray2[0][0]).Count;

                // Initialize float[][][] array
                float[][][] u10Array = new float[timeSteps2][][];

                // Convert each element to float and populate v10Array
                for (int t = 0; t < timeSteps2; t++)
                {
                    u10Array[t] = new float[latitudes2][];

                    for (int lat = 0; lat < latitudes2; lat++)
                    {
                        u10Array[t][lat] = new float[longitudes2];

                        for (int lon = 0; lon < longitudes2; lon++)
                        {
                            u10Array[t][lat][lon] = (float)dataArray2[t][lat][lon];
                        }
                    }
                }
                
             
                windNCread.u10Array = u10Array;

            }
        }

        string url3 = $"{Util.baseURl}rescue-platform-service/api/v1/dem/getNcInfos?ncPath=D:/data/haiyang/DATA.nc&varName=p140208&centerLon=116.41992593821296&centerLat=40.18801994965735";

        // Create a GET request to the specified URL
        HttpWebRequest request3 = (HttpWebRequest)WebRequest.Create(url3);
        request1.Method = "GET";

        // Send request and get response
        using (HttpWebResponse response3 = (HttpWebResponse)request3.GetResponse())
        {
            using (StreamReader reader3 = new StreamReader(response3.GetResponseStream()))
            {
                string result3 = reader3.ReadToEnd();

                // Parse the JSON string into a JObject
                JObject resultObject3 = JObject.Parse(result3);

                // Extract the 'data' field from the parsed JObject
                JArray dataArray3 = (JArray)resultObject3["data"];

                // Determine the dimensions of the data array
                int timeSteps3 = dataArray3.Count;
                int latitudes3 = ((JArray)dataArray3[0]).Count;
                int longitudes3 = ((JArray)dataArray3[0][0]).Count;

                // Initialize float[][][] array
                float[][][] p140208Array = new float[timeSteps3][][];

                // Convert each element to float and populate v10Array
                for (int t = 0; t < timeSteps3; t++)
                {
                    p140208Array[t] = new float[latitudes3][];

                    for (int lat = 0; lat < latitudes3; lat++)
                    {
                        p140208Array[t][lat] = new float[longitudes3];

                        for (int lon = 0; lon < longitudes3; lon++)
                        {
                            p140208Array[t][lat][lon] = (float)dataArray3[t][lat][lon];
                        }
                    }
                }
                
                windNCread.p140208Array = p140208Array;
            }
        }

        string url4 = $"{Util.baseURl}rescue-platform-service/api/v1/dem/getNcInfos?ncPath=D:/data/haiyang/DATA.nc&varName=mwd&centerLon=116.41992593821296&centerLat=40.18801994965735";

        // Create a GET request to the specified URL
        HttpWebRequest request4 = (HttpWebRequest)WebRequest.Create(url4);
        request1.Method = "GET";

        // Send request and get response
        using (HttpWebResponse response4 = (HttpWebResponse)request4.GetResponse())
        {
            using (StreamReader reader4 = new StreamReader(response4.GetResponseStream()))
            {
                string result4 = reader4.ReadToEnd();

                // Parse the JSON string into a JObject
                JObject resultObject4 = JObject.Parse(result4);

                // Extract the 'data' field from the parsed JObject
                JArray dataArray4 = (JArray)resultObject4["data"];

                // Determine the dimensions of the data array
                int timeSteps4 = dataArray4.Count;
                int latitudes4 = ((JArray)dataArray4[0]).Count;
                int longitudes4 = ((JArray)dataArray4[0][0]).Count;

                // Initialize float[][][] array
                float[][][] mwdArray = new float[timeSteps4][][];

                // Convert each element to float and populate v10Array
                for (int t = 0; t < timeSteps4; t++)
                {
                    mwdArray[t] = new float[latitudes4][];

                    for (int lat = 0; lat < latitudes4; lat++)
                    {
                        mwdArray[t][lat] = new float[longitudes4];

                        for (int lon = 0; lon < longitudes4; lon++)
                        {
                            mwdArray[t][lat][lon] = (float)dataArray4[t][lat][lon];
                        }
                    }
                }
                
                windNCread.mwdArray = mwdArray;
            }
        }
    }

    public void GetWaveHighData()
    {
         //中心点坐标应该选取伤员初始位置坐标
        string url1 = $"{Util.baseURl}rescue-platform-service/api/v1/dem/getNcInfos?ncPath=D:/data/haiyang/WaveHeight.nc&varName=hmax&centerLon=116.41992593821296&centerLat=40.18801994965735";

        // Create a GET request to the specified URL
        HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(url1);
        request1.Method = "GET";

        // Send request and get response
        using (HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse())
        {
            using (StreamReader reader1 = new StreamReader(response1.GetResponseStream()))
            {
                string result1 = reader1.ReadToEnd();

                // Parse the JSON string into a JObject
                JObject resultObject1 = JObject.Parse(result1);

                // Extract the 'data' field from the parsed JObject
                JArray dataArray1 = (JArray)resultObject1["data"];

                // Determine the dimensions of the data array
                int timeSteps = dataArray1.Count;
                int latitudes = ((JArray)dataArray1[0]).Count;
                int longitudes = ((JArray)dataArray1[0][0]).Count;

                // Initialize float[][][] array
                float[][][] hmaxArray = new float[timeSteps][][];

                for (int t = 0; t < timeSteps; t++)
                {
                    hmaxArray[t] = new float[latitudes][];

                    for (int lat = 0; lat < latitudes; lat++)
                    {
                        hmaxArray[t][lat] = new float[longitudes];

                        for (int lon = 0; lon < longitudes; lon++)
                        {
                            hmaxArray[t][lat][lon] = (float)dataArray1[t][lat][lon];
                        }
                    }
                }

                JArray lonArray = (JArray)resultObject1["data_lon"];
                JArray lanArray = (JArray)resultObject1["data_lat"];
                //JArray tArray = (JArray)resultObject["data_time"];

                int lons = lonArray.Count;
                int lans = lanArray.Count;
                //int times = tArray.Count;

                float[] longitudeArray = new float[lons];
                float[] latitudeArray = new float[lans];
                //float[] timeArray = new float[times];

                for (int i = 0; i < lons; i++)
                {
                    longitudeArray[i] = (float)lonArray[i];
                }

                for (int j = 0; j < lans; j++)
                {
                    latitudeArray[j] = (float)lanArray[j];
                }
                windNCread.hmaxArray = hmaxArray;
                //Console.WriteLine("a value of timeArray: " + timeArray[0]);
            }
        }
    }
}