using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace DINO_AddIn
{
	/// <summary>
	/// DINO Web Service control form
	/// </summary>
	public class ServiceForm : System.Windows.Forms.Form
	{
        private Geo3DModelService DinoService = null;
        public event DinoCallFinishedEventHandler DinoCallFinishedEvent;
        Geo3DModel[] models;
        private HelpForm myHelpForm;

        private Label urlLabel;
        private Panel panel1;
        private Label urlErrorLabel;
        private Button testButton;
        private TextBox urlTextBox;
        private Label modelLabel;
        private Panel panel2;
        private ComboBox modelComboBox;
        private Label xLabel;
        private Panel panel3;
        private Label xErrorLabel;
        private TextBox xTextBox;
        private Label yLabel;
        private TextBox yTextBox;
        private Button RunButton;
        private Label errorLabel;
        private Label yErrorLabel;
        private Button helpButton;
        private Label versionLabel;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

        #region Windows Form Designer generated code

            public ServiceForm(string url)
		    {
			    //
			    // Required for Windows Form Designer support
			    //
			    InitializeComponent();
                urlTextBox.Text = url;
                modelComboBox.SelectedIndex = 0;
		    }

		    /// <summary>
		    /// Clean up any resources being used.
		    /// </summary>
		    protected override void Dispose( bool disposing )
		    {
			    if( disposing )
			    {
				    if(components != null)
				    {
					    components.Dispose();
				    }
			    }
			    base.Dispose( disposing );
		    }


		    /// <summary>
		    /// Required method for Designer support - do not modify
		    /// the contents of this method with the code editor.
		    /// </summary>
		    private void InitializeComponent()
		    {
                System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ServiceForm));
                this.urlLabel = new System.Windows.Forms.Label();
                this.panel1 = new System.Windows.Forms.Panel();
                this.urlErrorLabel = new System.Windows.Forms.Label();
                this.testButton = new System.Windows.Forms.Button();
                this.urlTextBox = new System.Windows.Forms.TextBox();
                this.modelLabel = new System.Windows.Forms.Label();
                this.panel2 = new System.Windows.Forms.Panel();
                this.helpButton = new System.Windows.Forms.Button();
                this.modelComboBox = new System.Windows.Forms.ComboBox();
                this.xLabel = new System.Windows.Forms.Label();
                this.panel3 = new System.Windows.Forms.Panel();
                this.yErrorLabel = new System.Windows.Forms.Label();
                this.xTextBox = new System.Windows.Forms.TextBox();
                this.yLabel = new System.Windows.Forms.Label();
                this.yTextBox = new System.Windows.Forms.TextBox();
                this.xErrorLabel = new System.Windows.Forms.Label();
                this.RunButton = new System.Windows.Forms.Button();
                this.errorLabel = new System.Windows.Forms.Label();
                this.versionLabel = new System.Windows.Forms.Label();
                this.panel1.SuspendLayout();
                this.panel2.SuspendLayout();
                this.panel3.SuspendLayout();
                this.SuspendLayout();
                // 
                // urlLabel
                // 
                this.urlLabel.AutoSize = true;
                this.urlLabel.Location = new System.Drawing.Point(13, 13);
                this.urlLabel.Name = "urlLabel";
                this.urlLabel.Size = new System.Drawing.Size(68, 13);
                this.urlLabel.TabIndex = 0;
                this.urlLabel.Text = "Service URL";
                // 
                // panel1
                // 
                this.panel1.Controls.Add(this.urlErrorLabel);
                this.panel1.Controls.Add(this.testButton);
                this.panel1.Controls.Add(this.urlTextBox);
                this.panel1.Location = new System.Drawing.Point(5, 5);
                this.panel1.Name = "panel1";
                this.panel1.Size = new System.Drawing.Size(482, 81);
                this.panel1.TabIndex = 1;
                // 
                // urlErrorLabel
                // 
                this.urlErrorLabel.AutoSize = true;
                this.urlErrorLabel.ForeColor = System.Drawing.Color.Red;
                this.urlErrorLabel.Location = new System.Drawing.Point(23, 59);
                this.urlErrorLabel.Name = "urlErrorLabel";
                this.urlErrorLabel.Size = new System.Drawing.Size(0, 13);
                this.urlErrorLabel.TabIndex = 2;
                // 
                // testButton
                // 
                this.testButton.BackColor = System.Drawing.SystemColors.ControlLight;
                this.testButton.Location = new System.Drawing.Point(389, 27);
                this.testButton.Name = "testButton";
                this.testButton.Size = new System.Drawing.Size(90, 21);
                this.testButton.TabIndex = 1;
                this.testButton.Text = "Test/Update";
                this.testButton.TextAlign = System.Drawing.ContentAlignment.TopCenter;
                this.testButton.TextImageRelation = System.Windows.Forms.TextImageRelation.TextAboveImage;
                this.testButton.UseVisualStyleBackColor = false;
                this.testButton.Click += new System.EventHandler(this.testButton_Click);
                // 
                // urlTextBox
                // 
                this.urlTextBox.Location = new System.Drawing.Point(11, 28);
                this.urlTextBox.Name = "urlTextBox";
                this.urlTextBox.Size = new System.Drawing.Size(362, 20);
                this.urlTextBox.TabIndex = 0;
                this.urlTextBox.TextChanged += new System.EventHandler(this.urlTextBox_TextChanged);
                // 
                // modelLabel
                // 
                this.modelLabel.AutoSize = true;
                this.modelLabel.Location = new System.Drawing.Point(13, 101);
                this.modelLabel.Name = "modelLabel";
                this.modelLabel.Size = new System.Drawing.Size(36, 13);
                this.modelLabel.TabIndex = 2;
                this.modelLabel.Text = "Model";
                // 
                // panel2
                // 
                this.panel2.Controls.Add(this.helpButton);
                this.panel2.Controls.Add(this.modelComboBox);
                this.panel2.Location = new System.Drawing.Point(5, 92);
                this.panel2.Name = "panel2";
                this.panel2.Size = new System.Drawing.Size(482, 69);
                this.panel2.TabIndex = 3;
                // 
                // helpButton
                // 
                this.helpButton.BackColor = System.Drawing.SystemColors.ControlLight;
                this.helpButton.Location = new System.Drawing.Point(389, 27);
                this.helpButton.Name = "helpButton";
                this.helpButton.Size = new System.Drawing.Size(90, 21);
                this.helpButton.TabIndex = 3;
                this.helpButton.Text = "Help";
                this.helpButton.TextAlign = System.Drawing.ContentAlignment.TopCenter;
                this.helpButton.TextImageRelation = System.Windows.Forms.TextImageRelation.TextAboveImage;
                this.helpButton.UseVisualStyleBackColor = false;
                this.helpButton.Click += new System.EventHandler(this.helpButton_Click);
                // 
                // modelComboBox
                // 
                this.modelComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
                this.modelComboBox.ForeColor = System.Drawing.Color.Gray;
                this.modelComboBox.Items.AddRange(new object[] {
            "(update to retrieve models)"});
                this.modelComboBox.Location = new System.Drawing.Point(11, 27);
                this.modelComboBox.Name = "modelComboBox";
                this.modelComboBox.Size = new System.Drawing.Size(181, 21);
                this.modelComboBox.TabIndex = 0;
                // 
                // xLabel
                // 
                this.xLabel.AutoSize = true;
                this.xLabel.Location = new System.Drawing.Point(9, 5);
                this.xLabel.Name = "xLabel";
                this.xLabel.Size = new System.Drawing.Size(14, 13);
                this.xLabel.TabIndex = 4;
                this.xLabel.Text = "X";
                // 
                // panel3
                // 
                this.panel3.Controls.Add(this.yErrorLabel);
                this.panel3.Controls.Add(this.xTextBox);
                this.panel3.Controls.Add(this.xLabel);
                this.panel3.Controls.Add(this.yLabel);
                this.panel3.Controls.Add(this.yTextBox);
                this.panel3.Controls.Add(this.xErrorLabel);
                this.panel3.Location = new System.Drawing.Point(5, 169);
                this.panel3.Name = "panel3";
                this.panel3.Size = new System.Drawing.Size(482, 103);
                this.panel3.TabIndex = 5;
                // 
                // yErrorLabel
                // 
                this.yErrorLabel.AutoSize = true;
                this.yErrorLabel.ForeColor = System.Drawing.Color.Red;
                this.yErrorLabel.Location = new System.Drawing.Point(90, 71);
                this.yErrorLabel.Name = "yErrorLabel";
                this.yErrorLabel.Size = new System.Drawing.Size(0, 13);
                this.yErrorLabel.TabIndex = 9;
                // 
                // xTextBox
                // 
                this.xTextBox.Location = new System.Drawing.Point(11, 23);
                this.xTextBox.Name = "xTextBox";
                this.xTextBox.Size = new System.Drawing.Size(63, 20);
                this.xTextBox.TabIndex = 0;
                this.xTextBox.TextChanged += new System.EventHandler(this.xTextBox_TextChanged);
                // 
                // yLabel
                // 
                this.yLabel.AutoSize = true;
                this.yLabel.Location = new System.Drawing.Point(9, 47);
                this.yLabel.Name = "yLabel";
                this.yLabel.Size = new System.Drawing.Size(14, 13);
                this.yLabel.TabIndex = 8;
                this.yLabel.Text = "Y";
                // 
                // yTextBox
                // 
                this.yTextBox.Location = new System.Drawing.Point(11, 64);
                this.yTextBox.Name = "yTextBox";
                this.yTextBox.Size = new System.Drawing.Size(63, 20);
                this.yTextBox.TabIndex = 6;
                this.yTextBox.TextChanged += new System.EventHandler(this.yTextBox_TextChanged);
                // 
                // xErrorLabel
                // 
                this.xErrorLabel.AutoSize = true;
                this.xErrorLabel.ForeColor = System.Drawing.Color.Red;
                this.xErrorLabel.Location = new System.Drawing.Point(90, 30);
                this.xErrorLabel.Name = "xErrorLabel";
                this.xErrorLabel.Size = new System.Drawing.Size(0, 13);
                this.xErrorLabel.TabIndex = 3;
                // 
                // RunButton
                // 
                this.RunButton.Enabled = false;
                this.RunButton.Location = new System.Drawing.Point(205, 278);
                this.RunButton.Name = "RunButton";
                this.RunButton.Size = new System.Drawing.Size(86, 22);
                this.RunButton.TabIndex = 3;
                this.RunButton.Text = "Haal Data Op";
                this.RunButton.UseVisualStyleBackColor = true;
                this.RunButton.Click += new System.EventHandler(this.RunButton_Click);
                // 
                // errorLabel
                // 
                this.errorLabel.AutoSize = true;
                this.errorLabel.Location = new System.Drawing.Point(12, 283);
                this.errorLabel.Name = "errorLabel";
                this.errorLabel.Size = new System.Drawing.Size(0, 13);
                this.errorLabel.TabIndex = 8;
                // 
                // versionLabel
                // 
                this.versionLabel.AutoSize = true;
                this.versionLabel.ForeColor = System.Drawing.SystemColors.ControlDarkDark;
                this.versionLabel.Location = new System.Drawing.Point(415, 287);
                this.versionLabel.Name = "versionLabel";
                this.versionLabel.Size = new System.Drawing.Size(69, 13);
                this.versionLabel.TabIndex = 9;
                this.versionLabel.Text = "Version 1.1.2";
                // 
                // ServiceForm
                // 
                this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                this.ClientSize = new System.Drawing.Size(489, 303);
                this.Controls.Add(this.versionLabel);
                this.Controls.Add(this.errorLabel);
                this.Controls.Add(this.RunButton);
                this.Controls.Add(this.panel3);
                this.Controls.Add(this.modelLabel);
                this.Controls.Add(this.panel2);
                this.Controls.Add(this.urlLabel);
                this.Controls.Add(this.panel1);
                this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
                this.MaximumSize = new System.Drawing.Size(505, 341);
                this.MinimumSize = new System.Drawing.Size(505, 341);
                this.Name = "ServiceForm";
                this.Text = "DINO WebService";
                this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
                this.panel1.ResumeLayout(false);
                this.panel1.PerformLayout();
                this.panel2.ResumeLayout(false);
                this.panel3.ResumeLayout(false);
                this.panel3.PerformLayout();
                this.ResumeLayout(false);
                this.PerformLayout();

            }

        #endregion

        //
        // Clear error labels and hide/close the form
        //
        private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            urlErrorLabel.Text = "";
            errorLabel.Text = "";
            this.Hide();
        }
        
        //
        // Performs a URL test and model/resolution pairs retrieval
        //
        private void testButton_Click(object sender, EventArgs e)
        {
            urlErrorLabel.Text = "";
            errorLabel.Text = "";
            testButton.Enabled = false;
            try{
                DinoService = new Geo3DModelService();
                DinoService.Url = urlTextBox.Text;

                //If there are coordinates, the model/resolutions available in the XY point are retrieved
                if ((xTextBox.Text != "") & (yTextBox.Text != ""))
                {
                    models = DinoService.listModels(double.Parse(xTextBox.Text), true, double.Parse(yTextBox.Text), true);
                }
                else //If no coordinates are present, general model/resolutions pairs are retrieved
                {
                    models = DinoService.listModels(0, false, 0, false);
                }
                //Model/resolution box is filled
                modelComboBox.Items.Clear();
                foreach (Geo3DModel model in models)
                {
                    modelComboBox.Items.Add(model.type + " - " + model.resolution);
                }
                modelComboBox.Text = "";
                if (modelComboBox.Items.Count > 0)
                {
                    modelComboBox.SelectedIndex = 0;
                    modelComboBox.ForeColor = Color.Black;
                    errorLabel.Text = "Service beschikbaar!";
                    RunButton.Enabled = true;
                }
                else{
                    errorLabel.Text = "Service beschikbaar, maar geen model gevonden!";                
                }
            }
            catch (Exception ee)
            {
                if (ee.Message.StartsWith("'Voor model '")) //No models available for the specific location
                {
                    modelComboBox.Text = "";
                    if (modelComboBox.Items.Count > 0)
                    {
                        modelComboBox.SelectedIndex = 0;
                        modelComboBox.ForeColor = Color.Black;
                    }
                    errorLabel.Text = "Service beschikbaar, maar geen model gevonden!";
                    RunButton.Enabled = true;
                }
                else
                    modelComboBox.ForeColor = Color.Gray;
                    urlErrorLabel.Text = "error: " + ee.Message;
                
            }
            testButton.Enabled = true;
        }

        //
        // Retrieves geo information from web service in the specified URL, model, resolution and XY coordinate
        //
        private void RunButton_Click(object sender, EventArgs e)
        {
            if (correctParameters())
            {
                RunButton.Enabled = false;
                DinoService = new Geo3DModelService();
                DinoService.Url = urlTextBox.Text;
                try
                {
                    GeoColumn response = DinoService.sampleColumn((string)models[modelComboBox.SelectedIndex].type, double.Parse(xTextBox.Text), double.Parse(yTextBox.Text), (int)models[modelComboBox.SelectedIndex].resolution);
                    DispatchDinoCallFinishedEvent(response);
                }
                catch (Exception ee)
                {
                    System.Windows.Forms.MessageBox.Show("Fout in de Web Service:\n" + ee.Message, "Foutmelding", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                RunButton.Enabled = true;
            }
        }

        //
        // Checks if all the parameters of the form are correctly formatted
        //
        private bool correctParameters()
        {
            if ((xErrorLabel.Text + yErrorLabel.Text != "") || (xTextBox.Text == "") || (yTextBox.Text == ""))
            {
                System.Windows.Forms.MessageBox.Show("Parameters (bijvoorbeeld coordinaten)zijn niet aanwezig of foutief", "Foutmelding", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }

        //
        // Launches the form "finished" event to be catched by the AddIn
        // It is a notification of "geo info ready"
        //
        private void DispatchDinoCallFinishedEvent(GeoColumn geoColumn)
        {
            // make sure the are some delegates to call
            // all delegates will be called in the order they were added
            // to the invocation list
            if (DinoCallFinishedEvent != null)
            {
                DinoCallFinishedEvent(new DinoCallFinishedEventArgs(geoColumn));
            }
        }

        //
        // On URL changed, service needs to be tested and model/resolution retrieved
        // To force it, the RUN (Haal Data Op) button is disabled until test is clicked (and successful)
        //
        private void urlTextBox_TextChanged(object sender, EventArgs e)
        {
            RunButton.Enabled = false;
        }

        //
        // X coordinate format check (done on X changed)
        //
        private void xTextBox_TextChanged(object sender, EventArgs e)
        {
            double dummy;
            if (double.TryParse(xTextBox.Text, out dummy))
                xErrorLabel.Text = "";
            else if (xTextBox.Text == "")
                xErrorLabel.Text = "";
            else
                xErrorLabel.Text = "X coordinaat moet een getal zijn";
        }

        //
        // Y coordinate format check (done on Y changed)
        //
        private void yTextBox_TextChanged(object sender, EventArgs e)
        {
            double dummy;
            if (double.TryParse(yTextBox.Text, out dummy))
                yErrorLabel.Text = "";
            else if (yTextBox.Text == "")
                yErrorLabel.Text = "";
            else
                yErrorLabel.Text = "Y coordinaat moet een getal zijn";
        }

        //
        // Opens the Help form
        //
        private void helpButton_Click(object sender, EventArgs e)
        {
            if (myHelpForm != null)
                myHelpForm.Dispose();
            myHelpForm = new HelpForm();
            myHelpForm.ShowDialog(this);
        }

	}

    //Class for the event arguments
    public class DinoCallFinishedEventArgs : EventArgs
    {
        // Include properties for the account number, current balance, required 
        // minimum balance, a message describing the event, and a transaction ID.  
        // You can include any information that would be useful for you application.
        private GeoColumn GC;
        public GeoColumn geoColumn
        {
            get
            {
                return GC;
            }
        }

        // AccountBalanceEventArgs constructor
        public DinoCallFinishedEventArgs(GeoColumn gc)
        {
            GC = gc;
        }
    }

    //Finished Event Delegate
    public delegate void DinoCallFinishedEventHandler(DinoCallFinishedEventArgs dinoCallFinishedEventArgs);
}
