using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;

using Spherical;
using Spherical.IO;

/// <summary>
/// Summary description for Service
/// </summary>
[WebService(Namespace = "http://voservices.net/region",
    Description = "These XML Web services provide functionality to deal with regions on the sky, i.e. complex footprints of astronomical surveys. Please send comments, suggestions and bug-reports to <i>vo-help@skysrv.pha.jhu.edu!</i><br>Written by Tamas Budavari, 2005-2006<br><br><i>Conforms to WS-I Basic Profile 1.1</i><br><br><b>Note:</b><br><i>This is a preview of the region framework and may change without notice.</i>")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{

    public Service()
    {
    }

    [WebMethod]
    public Region test_super()
    {
        Region r = Samples.SdssDr4();
        r.Simplify();
        return r;
    }

    [WebMethod]
    public Region test_region()
    {
        return Samples.SdssDr4();
    }

    [WebMethod(Description =
        "Simplify a redundant region description, also make convexes disjoint<br><b>Input:</b> region<br><b>Output:</b> region")]
    public Region Simplify(Region region)
    {
        region.Simplify();
        return region;
    }

    [WebMethod(Description =
       "Derive the union of two regions<br><b>Input1:</b> region<br><b>Input2:</b> region<br><b>Output:</b> region")]
    public Region Union(Region reg1, Region reg2)
    {
        reg1.Simplify();
        reg2.Simplify();
        reg1.Union(reg2);
        reg1.Simplify();
        return reg1;
    }

    [WebMethod(Description =
       "Derive the intersection of two regions<br><b>Input1:</b> region<br><b>Input2:</b> region<br><b>Output:</b> region")]
    public Region Intersection(Region reg1, Region reg2)
    {
        reg1.Simplify();
        reg2.Simplify();
        Region r = reg1.Intersect(reg2);
        r.Simplify();
        return r;
    }

    [WebMethod(Description =
       "Derive the difference of a region and a convex<br><b>Input1:</b> region<br><b>Input2:</b> convex<br><b>Output:</b> region")]
    public Region Difference(Region region, Convex convex)
    {
        region.Simplify();
        convex.Simplify();
        Region diff = region.Difference(convex);
        diff.Simplify();
        return diff;
    }

    [WebMethod(Description =
        "Check to see if a point is in the given region<br><b>Input1:</b> region<br><b>Input2:</b> point<br><b>Output:</b> boolean")]
    public bool ContainsPoint(Region region, Cartesian point)
    {
        return region.Covers(point, 0);
    }

    [WebMethod(Description =
       "Check to see what the fractional overlap of a given circle with the region is<br><b>Input1:</b> region<br><b>Input2:</b> center (Cartesian)<br><b>Input2:</b> radius in arcmin (double)<br><b>Output:</b> ratio (double)")]
    public double ContainsCircle(Region region, Cartesian center, double arcmin)
    {
        Region p = new Region(new Halfspace(center, Math.Cos(arcmin * Const.Arcmin2Radian)));
        region.Simplify();
        Region i = region.Intersect(p);
        i.Simplify();
        double area = arcmin; /// todo
        return i.Area.Value / area;
    }

    [WebMethod(Description =
        "Calculate the area of a given region in square degrees<br><b>Input:</b> region<br><b>Output:</b> area (double)")]
    public double Area(Region region)
    {
        region.Simplify();
        return region.Area.Value;
    }

}

