﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using querySOAPandREST2.client; //adding the service reference

namespace querySOAPandREST2
{
    class WOF
    {

        public string USGSXMLPath
        {
            get;
            set;

        }

        public IEnumerable<string> WOFMethod(double xmin,double xmax,double ymin, double ymax,string conceptKeyword,
            string networkIDs, string beginDate, string endDate, string statCd, string parameterCd)
        {

            //Create an instance of the WCF proxy, "hiscentralSoap" came from the name field of App.config
            // hiscentralSoapClient came from hiscentralSoapClient in Service References. client
            hiscentralSoapClient client = new hiscentralSoapClient("hiscentralSoap");

            GenerateUSGSXMLPath(xmin, xmax, ymin, ymax, beginDate, endDate, statCd, parameterCd);

            Delimiter();

            //using the autogenerated code
            SeriesRecord[] records = client.GetSeriesCatalogForBox2(xmin, xmax, ymin, ymax, conceptKeyword, networkIDs, beginDate, endDate);


            //creat a IEnumerable<string> for return
            List<String> Records = new List<String>();

            foreach (var record in records)
            {

                string str = record.Sitename; 
                //add the sitename to our list
                Records.Add(str);
                //Console.WriteLine(str);

            }

            //convert our List<String> to IEnumerable<string>
            IEnumerable<string> sitename = Records;


            sitename = ListPostProcessing(sitename);
            WriteNameArray(sitename);

            //Closing the client to closes the connection
            client.Close();
            return Records;

        }

        private void Delimiter()
        {
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("");

        }
        /// <summary>
        /// Generate valid web path to call USGS RESTful Services
        /// This include reformat strings to become valid USGS RESTful Services parameters
        /// </summary>
        /// <param name="xmin"></param>
        /// <param name="xmax"></param>
        /// <param name="ymin"></param>
        /// <param name="ymax"></param>
        /// <param name="beginDate"></param>
        /// <param name="endDate"></param>
        /// <param name="statCd"></param>
        /// <param name="parameterCd"></param>
        private void GenerateUSGSXMLPath(double xmin, double xmax, double ymin, double ymax, string beginDate, string endDate, string statCd, string parameterCd)
        {
            /* String Format for Double and Int
             * http://www.csharp-examples.net/string-format-double/
             * */
            string xminStr = String.Format("{0:0.000000}", xmin);
            string xmaxStr = String.Format("{0:0.000000}", xmax);
            string yminStr = String.Format("{0:0.000000}", ymin);
            string ymaxStr = String.Format("{0:0.000000}", ymax);

            int statCdInt = Int32.Parse(statCd);
            int parameterCdInt = Int32.Parse(parameterCd);

            //example: statCdInt = 3 ===>> statCdStr = 00003
            string statCdStr = String.Format("{0:00000}", statCdInt);
            string parameterCdStr = String.Format("{0:00000}", parameterCdInt);

            //desired path http://waterservices.usgs.gov/nwis/dv/?format=waterml,1.1&bBox=-83.000000,36.500000,-81.000000,38.500000&startDT=2014-01-01&endDT=2014-02-01&statCd=00003&parameterCd=00060
            USGSXMLPath = "http://waterservices.usgs.gov/nwis/dv/?format=waterml,1.1&bBox=" + xminStr + "," + yminStr + "," + xmaxStr + "," + ymaxStr + "&startDT=" + beginDate + "&endDT=" + endDate
                + "&statCd=" + statCdStr + "&parameterCd=" + parameterCdStr;


        }
        /// <summary>
        /// Post processing for the Ienumerable, this include: select distinct elem, and order them alphabetically
        /// </summary>
        /// <param name="sitename"></param>
        /// <returns></returns>
        private static IEnumerable<string> ListPostProcessing(IEnumerable<string> sitename)
        {
            //Returns distinct elements using the default equality comparer to compare values.
            sitename = sitename.Distinct();
            //sort the IEnumerable<string>, note: not in place
            sitename = sitename.OrderBy(s => s);
            return sitename;
        }

        private static void WriteNameArray(IEnumerable<string> sitename)
        {

            Console.WriteLine("=========================================================");
            Console.WriteLine("SiteName return by WOF Service");
            foreach (string name in sitename)
                Console.WriteLine(name);
        }
    }
}
