using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Text;

using Tamas = SphericalLib;
#if false
namespace SphericalVisualizer
{
    public class RegionDraw
    {
       	//-------------------------------------		
		// Limits of the input parameters
		//-------------------------------------
		const int		max_zoom	=       7;		
		const int		max_width	=    2048;
		const int		max_height	=    2048;
		const int		min_width	=      16;
		const int		min_height	=      16;
		const double	max_scale	=    1200;
		const double	min_scale	=   0.015;
		//-------------------------------------
		// Input parameters
		//-------------------------------------
		private double	ra, dec;				// normalized [ra,dec]
		private Int32	width, height;			// image size
		private double  scale;					// arcseconds/pixel
		//-------------------------------------
		// Derived parameters
		//-------------------------------------
		// zoom: the image pyramid level [0..max_zoom]
		// imageScale: the additional scaling of 
		// the image beyond walking the pyramid
		//-------------------------------------
		private int		zoom;					// zoom level 
		private double	ppd;					// pixels per degree
		private double	imageScale;				// scale of the image
		private float	zoomScale;				// reference scale
		private float	size;					// size of the symbols drawn
		private double	radius, fradius;		// radius for field searches
		//-------------------------------------
		// Debug support
		//-------------------------------------
		private StringBuilder dbgMsg = new StringBuilder();		
		private bool debug			 = false;
		//-------------------------------------
		RegionCanvas canvas				= null;
		//-------------------------------------

	
        /// <summary>
        /// This is the main entry point for the visualizer
        /// </summary>
        /// <param name="region"></param>
        /// <param name="ra"></param>
        /// <param name="dec"></param>
        /// <param name="scale"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        //public static Bitmap Image(
        //    List<Tamas.SuperRegion> regionList, // regions to be visualized
        //    double	ra,						    // right ascension in J2000 degrees
        //    double	dec, 						// declination in J2000 degrees
        //    double	scale,						// arcsec/pixel (0.3961267 is native 1:1 for SDSS)
        //    Int32	width,						// image width  (in pixels)
        //    Int32	height, 					// image height (in pixels))
        //    bool invert
        //    )
        //{
        //    RegionDraw d = new RegionDraw();
        //    return d.GetImage(regionList,ra,dec,scale,width,height,invert);
        //}

		private Bitmap GetImage(	
            Tamas.SuperRegion region,
			double	ra,						// right ascension in J2000 degrees
			double	dec, 						// declination in J2000 degrees
			double	scale,						// arcsec/pixel (0.3961267 is native 1:1 for SDSS)
			Int32	width,						// image width  (in pixels)
			Int32	height, 					// image height (in pixels)
            bool invert
			)
		{	
			//-------------------------------------
			// validate ranges and values of input
			//-------------------------------------
			//validateInput(ra_,dec_,scale_,height_,width_,opt_);
            this.ra = ra;
            this.dec = dec;
            this.scale = scale;
            this.width = width;
            this.height = height;

			//--------------------------
			// set image scale and zoom 
			//--------------------------
			zoom = 0;
			ppd  = 3600.0/scale;
			
			imageScale = ppd/Frame.PixelsPerDegree;
			while (zoom < max_zoom & imageScale <= .5)	
			{											
				zoom++;											// go higher in the pyramid
				imageScale *= 2;								// change the scaling accordingly
			}
			zoomScale = (float) Math.Pow(2,zoom);				// set the scale according to the real zoom
			size	  = (float) ((zoom>3)? 6: 12*imageScale);	// set symbol size based on zoom level
			
			//---------------------------------------------
			// set SQL search radii for fields and objects
			//---------------------------------------------
			radius	= 60.0*0.5*Math.Sqrt(width*width+height*height)/ppd;
			fradius = Frame.FrameHalfDiag + radius;

			//---------------------------------------------------
			// initialize the canvas, connection and projection
			//---------------------------------------------------
            canvas = new RegionCanvas(width, height, imageScale, ppd, debug);

            try 
            {
				canvas.InitializeProjection(ra, dec, ppd, "STR");

                HTMRegion h = new HTMRegion(0, 0, "SECTOR");
                ArrayList arcs = new ArrayList();
                for (int ic = 0; ic < region.convexList.Count; ic++)
                {
                    Tamas.SuperConvex convex = region.convexList[ic];
                    //Tamas.PatchFactory pf = new Tamas.PatchFactory(convex);
                    List<Tamas.Patch> pl = convex.patchList;
                    for (int ip = 0; ip < pl.Count; ip++)
                    {
                        for (int ia = 0; ia < pl[ip].arcList.Length; ia++)
                        {
                            Tamas.Arc ta = pl[ip].arcList[ia];
                            HTMArc a = new HTMArc(ic, ic * 10 + ip, 1,
                                ta.p1.RA, ta.p1.Dec,
                                ta.p2.RA, ta.p2.Dec,
                                ta.circle.x, ta.circle.y, ta.circle.z,
                                ta.circle.cos0,
                                ic * 100 + ip * 10 + ia);
                            arcs.Add(a);
                        }
                    }
                }
                //------------------------------------
                // copy the ArrayList into the array
                //------------------------------------
                h.arc = (HTMArc[])arcs.ToArray(typeof(HTMArc));
                h.count = h.arc.Length;

                //-----------------
                // draw the region
                //-----------------
                canvas.drawRegion(h, 1);

                //canvas.displayMessage(scale.ToString());

                // ----
				this.getLabel(region.name);
                try
                {
                    canvas.drawGrid();
                }
                catch { }

                if (invert)
                    canvas.Invert();

            }
            catch (Exception e)
            {
                canvas.displayMessage(e.Message);
            }
            return canvas.Image;
		}

        /// <summary>
        /// getLabel. Display the standard label on the image.
        /// </summary>
        private void getLabel(string name)
        {
            string theLabel = String.Format("RA: {0:N3} Dec: {1:N3}\npixels per degree: {2}\n", ra, dec, ppd);
            canvas.drawLabel(String.Format("{0}\n{1}",name,theLabel));
        }
      
    }
}
#endif