﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;
using System.Text;
using Npgsql;
using System.Data;

/// <summary>
/// Summary description for geoneer
/// </summary>
public class Geoserver
{

    public void ProcessInGeoserver(string Label1)
    {
        try
        {
            //process the new created table in Geoserver            
            string url = "http://localhost:8080/geoserver/rest/workspaces/geoneer/datastores/public/featuretypes";
            StringBuilder sb = featuretype_create(Label1);
            REST(url, "POST", sb);

            //create style for the featuretype
            url = "http://localhost:8080/geoserver/rest/layers/geoneer:" + Label1;
            string type = DetectType();
            sb = create_style(type);
            REST(url, "PUT", sb);
        }
        catch (Exception ex)
        { throw ex; }
    }


    public Geoserver()
    {
        //
        // TODO: Add constructor logic here
        //
    }   



    public void REST(String url, String method, StringBuilder xml)
    {
        try
        {
            WebRequest request = WebRequest.Create(url);

            request.ContentType = "text/xml";
            request.Method = method;
            request.Credentials = new NetworkCredential("admin", "geoserver");

            byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(xml.ToString().Trim());
            Stream reqstr = request.GetRequestStream();
            reqstr.Write(buffer, 0, buffer.Length);
            reqstr.Close();

            WebResponse response = request.GetResponse();
        }
        catch (Exception ex)
        {
            string e = ex.StackTrace.ToString();
            e = e.Length.ToString();
        }

    }

    public StringBuilder workspace_create(string workspace_name)
    {        
        FileInfo xmlfile = new FileInfo(HttpContext.Current.Server.MapPath("~/App_Data/workspace.xml"));        
        return new StringBuilder(xmlfile.OpenText().ReadToEnd().Replace("@name@",workspace_name));
    }

    public StringBuilder datastore_create(string datastore_name, string host, string port, string database, string user)
    {
        FileInfo xmlfile = new FileInfo(HttpContext.Current.Server.MapPath("~/App_Data/datastore.xml"));
        StringBuilder sb = new StringBuilder(xmlfile.OpenText().ReadToEnd());
        sb.Replace("@name@", datastore_name);
        sb.Replace("@host@", host);
        sb.Replace("@port@", port);
        sb.Replace("@database@", database);
        sb.Replace("@user@", user);
        
        return sb;
    }




    public StringBuilder featuretype_create(string featuretype_name)
    {
        FileInfo xmlfile = new FileInfo(HttpContext.Current.Server.MapPath("~/App_Data/featuretype.xml"));
        StringBuilder sb = new StringBuilder(xmlfile.OpenText().ReadToEnd());
        sb.Replace("@name@", featuretype_name);
        return sb;
    }



    public StringBuilder create_style(String type)
    {
        FileInfo xmlfile = new FileInfo(HttpContext.Current.Server.MapPath("~/App_Data/Styles/style.xml"));
        StringBuilder sb = new StringBuilder(xmlfile.OpenText().ReadToEnd());
        sb.Replace("@name@", type);
        return sb;    
    }

    public string DetectType()
    {
        Npgsql_Class npg = new Npgsql_Class();
        string sqlstring = "select ST_GeometryType(the_geom) from data_geoms limit 1;";
        DataTable dt = Data_Import_Transpose.CreateDataTableSelectStatement(sqlstring, npg);
        string t = dt.Rows[0].ItemArray.GetValue(0).ToString();

        if (t.Contains("Polygon")) { t = "polygon"; }
        if (t.Contains("Line")) { t = "line"; }
        if (t.Contains("Point")) { t = "point"; }
        

        return t;
    }
}