using System;
using System.Collections.Generic;
using System.Text;

using Spherical;
using Spherical.IO;
using Spherical.Visualizer;

namespace USA
{
    class Program
    {
        static void Main(string[] args)
        {
#if false
            Random random = new Random(42);
            RingInterval rint;
            List<int> lint = new List<int>();

            rint = new RingInterval(0, 5);
            Console.Out.WriteLine(rint);
            Console.Out.WriteLine("Thres: {0}", rint.threshold);
            Console.Out.WriteLine("Contains? {0}", rint.Contains(2));
            Console.Out.WriteLine("Contains? {0}", rint.Contains(7));
            for (int i=0; i<30; i++) Console.Out.Write(" {0}", rint.Random(random));
            Console.Out.WriteLine();

            rint = new RingInterval(5, 2, 10);
            Console.Out.WriteLine(rint);
            Console.Out.WriteLine("Thres: {0}", rint.threshold);
            Console.Out.WriteLine("Contains? {0}", rint.Contains(2));
            Console.Out.WriteLine("Contains? {0}", rint.Contains(7));
            for (int i = 0; i < 30; i++) Console.Out.Write(" {0}", rint.Random(random));
            Console.Out.WriteLine();

            rint = new RingInterval(5, 0, 10);
            Console.Out.WriteLine(rint);
            Console.Out.WriteLine("Thres: {0}", rint.threshold);
            Console.Out.WriteLine("Contains? {0}", rint.Contains(2));
            Console.Out.WriteLine("Contains? {0}", rint.Contains(7));
            for (int i = 0; i < 30; i++) Console.Out.Write(" {0}", rint.Random(random));
            Console.Out.WriteLine();

            return;
#endif
            DatReader st = new DatReader(@"..\..\st24_d00.dat");

            // read points
            List<Cartesian> pts = new List<Cartesian>();
            Cartesian mid;
            int id = st.NextPointList(pts, out mid);
            while (id != 1)
            {
                pts.Clear();
                id = st.NextPointList(pts, out mid);
            }
            Console.Out.WriteLine("ID: {0}", id);
            Console.Out.WriteLine(" N: {0}", pts.Count);
            Console.Out.WriteLine(" O: {0}", mid);

//            pts.Reverse();

            OutlineRandom o = new OutlineRandom(pts.AsReadOnly());
            Region r = o.Region(1,100,10000);
            //Console.Out.WriteLine(" R:\n    {0}", r);

            r.Simplify();

            Console.Out.WriteLine(" P: {0}", r.NumberOfPatches());
            Console.Out.WriteLine(" C: {0}", r.ConvexList.Count);

            Convex k = new Convex(r.ConvexList[0]);
            Region n = new Region(k, false);

            double ra, dec;
            ra = k.PatchList[0].ArcList[0].Point1.RA;
            dec = k.PatchList[0].ArcList[0].Point1.Dec;

            RegionImage.SaveGif(@"C:\Temp\rUSA.gif", r, ra,dec, 2000);
        }
    }
}
