using System;
using System.IO;
using System.Collections.Specialized;
using Svg;
using System.Drawing.Imaging;

public partial class Map : System.Web.UI.Page
{
    string[] MapParameter = null;
    string inv = "0";
    string Out = "0";
    string[] Data = null;
    string[] Show = null;
    string[] Overlay = null;
    string[] Colors = null;
    string[] Style = null;
    string Grid = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        MapParameter = null;
        inv = "0";
        Out = "0";
        Data = null;
        Show = null;
        Overlay = null;
        Colors = null;
        Style = null;
        Grid = null;

        NameValueCollection coll;
        coll = Request.QueryString;
        if (coll.Count == 0)
        {
            Response.Redirect("help.htm");
        }
        else
            for (int i = 0; i < coll.Count; i++)
            {
                if (coll.Keys[i] != null)
                {
                    string Key = coll.Keys[i].ToLower(); if (Key == null) Key = "";
                    string Value = coll.GetValues(i)[0]; if (Value == null) Value = "";
                    Value = Server.HtmlDecode(Value).ToLower();
                    switch (Key)
                    {
                        case "map":
                            MapParameter = Value.Split(new char[] { '|' });
                            break;
                        case "inv":
                            inv = Value;
                            break;
                        case "out":
                            Out = Value;
                            break;
                        case "color":
                            Colors = Value.Split(new char[] { '|' });
                            break;
                        case "style":
                            Style = Value.Split(new char[] { '|' });
                            break;
                        case "show":
                            Show = Value.Split(new char[] { '|' });
                            break;
                        case "data":
                            Data = Value.Split(new char[] { '|' });
                            break;
                        case "ovl":
                            Overlay = Value.Split(new char[] { '|' });
                            break;
                        case "grid":
                            Grid = Value;
                            break;
                        default:
                            break;
                    }
                }
            }
        const string DefaultMap = "at-9";
        string ShowMap = "";
        string map = "";
        if (MapParameter == null)
        {
            ShowMap = DefaultMap;
            MapParameter = new string[] { ShowMap };
            map = ShowMap;
        }
        else
        {
            ShowMap = MapParameter[0];
            if (ShowMap == "")
                ShowMap = DefaultMap;
            if (ShowMap == null)
                ShowMap = DefaultMap;
        }
        int MapCount = GeoMaps.map.Length;
        int MapIndex = -1;
        for (int i = 0; i < MapCount; i++)
        {
            if (GeoMaps.map[i].MapDetails[1].ToLower() == ShowMap)
            {
                MapIndex = i;
                break;
            }
        }
        if (MapIndex == -1)
            Response.Redirect("help.htm");
        MemoryStream mem = GeoMaps.map[MapIndex].GetSvgMap(MapParameter, Show, Colors, inv, Data, Overlay, Style, Out == "data", Grid);
        byte[] buffer = mem.ToArray();
        Response.Clear();
        switch (Out)
        {
            case "0":
            case "svg":
                Response.ContentType = "image/svg+xml";
                Response.BinaryWrite(buffer);
                break;
            case "1":
            case "txt":
                Response.ContentType = "text/plain";
                Response.BinaryWrite(buffer);
                break;
            case "2":
            case "jpg":
                Response.ContentType = "image/jpeg";
                Svg.SvgDocument doc = new Svg.SvgDocument();
                Svg.SvgDocument.Open(mem).Draw().Save(Response.OutputStream, ImageFormat.Jpeg);
                break;
            case "3":
            case "data":
                Response.ContentType = "text/html";
                Response.BinaryWrite(buffer);
                break;
        }
        Response.End();
    }
}
