//
        // GET: /Markers/
        //public IEnumerable<Marker> GetAllUrines()
        //{
        //    var rnd = new Random();
        //    var randomIndexs = Enumerable.Range(0, Common.Constants.NumberOfPoints).Select(r => rnd.Next(6000)).ToList();
        //    var result = new DataProvider()
        //        .GetAntiSocialDataV1()
        //        .Where(a => a.Urine)
        //        .Select((a, i) => new
        //        {
        //            index = i,
        //            m = new Marker
        //                {
        //                    lng = a.Location.Longitude,
        //                    lat = a.Location.Latitude,
        //                    r = new Random().Next(100)
        //                }
        //        })
        //         .Where(item => randomIndexs.Contains(item.index))
        //         .Select(item => item.m)
        //         .ToList();
        //    return result;
        //}
        public IEnumerable<Marker> GetAllUrines()
        {
            var result = new DataProvider()
                .GetAntiSocialDataV2()
                .Where(a => a.Category == "urine")
                .Select((a, i) => new Marker
                {
                    lng = a.Location.Longitude,
                    lat = a.Location.Latitude,
                    r = a.Radius * 100
                })
                .ToList();

            return result;
        }
        //
        // GET: /Markers/
        public IEnumerable<Marker> GetAllMarkers()
        {
            var rnd = new Random();
            var randomIndexs = Enumerable.Range(0, 100).Select(r => rnd.Next(6000)).ToList();
            var result = new DataProvider()
                .GetAntiSocialDataV1()
                .Where(a => a.Urine)
                .Select((a, i) => new
                {
                    index = i,
                    m = new Marker
                        {
                            lng = a.Location.Longitude,
                            lat = a.Location.Latitude,
                            r = new Random().Next(100)
                        }
                })
                 .Where(item => randomIndexs.Contains(item.index))
                 .Select(item => item.m)
                 .ToList();

            return result;
        }
        public IEnumerable<Marker> GetAllIBloods()
        {
            var rnd = new Random(100);
            var randomIndexs = Enumerable.Range(0, Common.Constants.NumberOfPoints).Select(r => rnd.Next(6000)).ToList();
            var result = new DataProvider()
                .GetAntiSocialDataV1()
                .Where(a => a.Blood)
                .Select((a, i) => new
                {
                    index = i,
                    m = new Marker
                    {
                        lng = a.Location.Longitude,
                        lat = a.Location.Latitude,
                        r = 50
                    }
                })
                 //.Where(item => randomIndexs.Contains(item.index))
                 .Select(item => item.m)
                 .ToList();

            return result;
        }
        public IEnumerable<Marker> GetAllClusteredBloods()
        {
            var result = new DataProvider()
                .GetAntiSocialDataV1()
                .Select(a =>
                    new Marker
                    {
                        lng = a.Location.Longitude,
                        lat = a.Location.Latitude,
                        r = 1 * 100
                    })
                 .ToList();

            var rawData = result.Select(r => new double[] { r.lng, r.lat }).ToArray();

            var clusters = Clustring.KMean(rawData, 5, 30);

            var clustersGrouped = clusters.GroupBy(a => a);

            var itemsInClusters = new List<Marker>();

            var index = 0;

            foreach (var item in clusters)
            {
                var newResult = Clustring.DataPointsPerCluster(rawData, index, clusters, 50).ToList();

                foreach (var r in newResult)
                {
                    itemsInClusters.Add(new Marker { lat = r.Item2, lng = r.Item1, r = 100 });
                }

                index++;
            }

            return itemsInClusters;
        }
        public static IEnumerable<Marker> GetClusterMakers(int clusterNumber)
        {
            var result = new DataProvider()
                .GetAntiSocialDataV1()
                .Select(a =>
                    new Marker
                    {
                        lng = a.Location.Longitude,
                        lat = a.Location.Latitude,
                        r = 1 * 100
                    })
                 .ToList();

            var rawData = result.Select(r => new double[] { r.lng, r.lat }).ToArray();

            var clusters = Clustring.KMean(rawData, 5, 1000);

            var clustersGrouped = clusters.GroupBy(a => a);

            var itemsInClusters = new List<Marker>();

            return Clustring.DataPointsPerCluster(rawData, clusterNumber, clusters, 1000)
                    .Select(r => new Marker { lat = r.Item2, lng = r.Item1, r = 100 }).ToList();
        }
        //static void Main(string[] args)
        //{
        //    try
        //    {
        //        Console.WriteLine("\nBegin outlier data detection using k-means clustering demo\n");
        //        Console.WriteLine("Loading all (height-weight) data into memory");
        //        string[] attributes = new string[] { "Height", "Weight" };
        //        double[][] rawData = new double[20][];  // in most cases data will be in a text file or SQl table
        //        rawData[0] = new double[] { 65.0, 220.0 };  // if data won't fit into memory, stream through external storage
        //        rawData[1] = new double[] { 73.0, 160.0 };
        //        rawData[2] = new double[] { 59.0, 110.0 };
        //        rawData[3] = new double[] { 61.0, 120.0 };
        //        rawData[4] = new double[] { 75.0, 150.0 };
        //        rawData[5] = new double[] { 67.0, 240.0 };
        //        rawData[6] = new double[] { 68.0, 230.0 };
        //        rawData[7] = new double[] { 70.0, 220.0 };
        //        rawData[8] = new double[] { 62.0, 130.0 };
        //        rawData[9] = new double[] { 66.0, 210.0 };
        //        rawData[10] = new double[] { 77.0, 190.0 };
        //        rawData[11] = new double[] { 75.0, 180.0 };
        //        rawData[12] = new double[] { 74.0, 170.0 };
        //        rawData[13] = new double[] { 70.0, 210.0 };
        //        rawData[14] = new double[] { 61.0, 110.0 };
        //        rawData[15] = new double[] { 58.0, 100.0 };
        //        rawData[16] = new double[] { 66.0, 230.0 };
        //        rawData[17] = new double[] { 59.0, 120.0 };
        //        rawData[18] = new double[] { 68.0, 210.0 };
        //        rawData[19] = new double[] { 61.0, 130.0 };
        //        Console.WriteLine("\nRaw data:\n");
        //        ShowMatrix(rawData, rawData.Length, true);
        //        int numAttributes = attributes.Length;  // 2 in this demo (height,weight)
        //        int numClusters = 5;  // vary this to experiment (must be between 2 and number data tuples)
        //        int maxCount = 30;  // trial and error
        //        Console.WriteLine("\nBegin clustering data with k = " + numClusters + " and maxCount = " + maxCount);
        //        int[] clustering = Cluster(rawData, numClusters, numAttributes, maxCount);
        //        Console.WriteLine("\nClustering complete");
        //        Console.WriteLine("\nClustering in internal format: \n");
        //        ShowVector(clustering, true);  // true -> newline after display
        //        Console.WriteLine("\nClustered data:");
        //        ShowClustering(rawData, numClusters, clustering, true);
        //        double[] outlier = Outlier(rawData, clustering, numClusters, 0);
        //        Console.WriteLine("Outlier for cluster 0 is:");
        //        ShowVector(outlier, true);
        //        Console.WriteLine("\nEnd demo\n");
        //        Console.ReadLine();
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine(ex.Message);
        //        Console.ReadLine();
        //    }
        //} // Main
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("\nBegin outlier data detection using k-means clustering demo\n");

                Console.WriteLine("Loading all (height-weight) data into memory");
                string[] attributes = new string[] { "Height", "Weight" };
                double[][] rawData = new DataProvider().GetAntiSocialDataV1()
                    .Where(a => a.Vomit)
                    .Select(r => new double[] { r.Location.Longitude, r.Location.Latitude }).ToArray();

                Console.WriteLine("\nRaw data:\n");
                //ShowMatrix(rawData, rawData.Length, true);

                int numAttributes = attributes.Length;  // 2 in this demo (height,weight)
                int numClusters = 7;  // vary this to experiment (must be between 2 and number data tuples)
                int maxCount = 30;  // trial and error

                Console.WriteLine("\nBegin clustering data with k = " + numClusters + " and maxCount = " + maxCount);
                int[] clustering = Cluster(rawData, numClusters, numAttributes, maxCount);
                Console.WriteLine("\nClustering complete");

                var datapointsInEachCluster = clustering.GroupBy(a => a);

                Console.WriteLine("\nClustering in internal format: \n");
                foreach (var item in datapointsInEachCluster.OrderBy(a => a.Key))
                {
                    Console.WriteLine(string.Format("{0}, {1}", item.Key, item.Count()));
                }

                Console.WriteLine(string.Format("number of datapoints {0}, number of items {1}", rawData.Length, clustering.Length));

                Console.WriteLine("\nClustered data:");
                ShowClustering(rawData, numClusters, clustering, true);

                double[] outlier = Outlier(rawData, clustering, numClusters, 0);
                Console.WriteLine("Outlier for cluster 0 is:");
                //ShowVector(outlier, true);

                var outliersWithinCluster = outlier.GroupBy(a => a);
                Console.WriteLine("\nOutliers with each cluster: \n");
                foreach (var item in outliersWithinCluster.OrderBy(a => a.Key))
                {
                    Console.WriteLine(string.Format("{0}, {1}", item.Key, item.Count()));
                }

                Console.WriteLine("\nEnd demo\n");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }
        }