using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UtmConvert {
    public partial class UtmConvertMainForm : Form {

        ConvertLatLonUtm con;
        XmlParser _parser;
        internal XmlParser Parser {
            get { return _parser; }
        }

        public UtmConvertMainForm() {
            InitializeComponent();
            _parser = new XmlParser();
            con = new ConvertLatLonUtm();
            foreach (Datum d in Datums.datumList)
                datumsListBox.Items.Add(d.Name);
            datumsListBox.SelectedIndex = 0;
            northSouthComboBox.SelectedIndex = 0;
            eastWestComboBox.SelectedIndex = 1;
            latLonFormatToolStripComboBox.SelectedIndex = 0;
            convertLatLonToUtm();
        }

        private void datumsListBox_SelectedIndexChanged(object sender, EventArgs e) {
            foreach (Datum d in Datums.datumList) {
                if (d.Name == datumsListBox.SelectedItem.ToString()) {
                    datumPropertyGrid.SelectedObject = d;
                    con.Datum = d;
                }
            }
        }


        private void convertButton_Click(object sender, EventArgs e) {
            if (latLonToUtmRadioButton.Checked)
                convertLatLonToUtm();
            else
                convertUtmToLatLon();
         }

        private void convertLatLonToUtm() {
            double lat = 0, lon = 0;
            bool nS = (northSouthComboBox.Text != "N");
            bool eW = (eastWestComboBox.Text != "E");
            if (latDegTextBox.Text != "" && latMinTextBox.Text != "" && latSecTextBox.Text != "")
				lat = ConvertDegRad.getRadians(latDegTextBox.Text, latMinTextBox.Text
					, latSecTextBox.Text, northSouthComboBox.Text[0]);
            else if (latDegTextBox.Text != "" && latMinTextBox.Text != "")
				lat = ConvertDegRad.getRadians(latDegTextBox.Text
					, latMinTextBox.Text, northSouthComboBox.Text[0]);
            else if (latDegTextBox.Text != "")
				lat = ConvertDegRad.getRadians(latDegTextBox.Text, northSouthComboBox.Text[0]);
            latInRad.Text = Math.Round(lat, 4).ToString();
            if (lonDegTextBox.Text != "" && lonMinTextBox.Text != "" && lonSecTextBox.Text != "")
				lon = ConvertDegRad.getRadians(lonDegTextBox.Text, lonMinTextBox.Text
					, lonSecTextBox.Text, eastWestComboBox.Text[0]);
            else if (lonDegTextBox.Text != "" && lonMinTextBox.Text != "")
				lon = ConvertDegRad.getRadians(lonDegTextBox.Text
					, lonMinTextBox.Text, eastWestComboBox.Text[0]);
            else if (lonDegTextBox.Text != "")
				lon = ConvertDegRad.getRadians(lonDegTextBox.Text, eastWestComboBox.Text[0]);
            lonInRad.Text = Math.Round(lon, 4).ToString();
            con.convertLatLonToUtm(lat, lon);
            eastingTextBox.Text = Math.Round(con.Easting, 2).ToString();
            northingTextBox.Text = Math.Round(con.Northing, 2).ToString();
            zoneTextBox.Text = con.Zone;
            latToolStripStatusLabel.Text = ConvertDegRad.getDegrees(Math.Abs(lat)).ToString() 
                + " " + northSouthComboBox.Text + ", ";
            lonToolStripStatusLabel.Text = ConvertDegRad.getDegrees(Math.Abs(lon)).ToString()
                 + " " + eastWestComboBox.Text + ", ";
        }

        private void convertUtmToLatLon() {
            if (eastingTextBox.Text != "" || northingTextBox.Text != "") {
                double e = 0, n = 0;
                double.TryParse(eastingTextBox.Text, out e);
                double.TryParse(northingTextBox.Text, out n);
                con.convertUtmToLatLon(e, n, zoneTextBox.Text);
                if (con.Latitude > 0)
                    northSouthComboBox.SelectedIndex = 0;
                else
                    northSouthComboBox.SelectedIndex = 1;
                latDegTextBox.Text = ConvertDegRad.getDegrees(Math.Abs(con.Latitude)).ToString();
                latMinTextBox.Clear();
                latSecTextBox.Clear();
                if (con.Longitude > 0)
                    eastWestComboBox.SelectedIndex = 0;
                else
                    eastWestComboBox.SelectedIndex = 1;
                lonDegTextBox.Text = ConvertDegRad.getDegrees(Math.Abs(con.Longitude)).ToString();
                lonMinTextBox.Clear();
                lonSecTextBox.Clear();
            }
        }

        private void convertingUTMToLatitudeLongitudepdfToolStripMenuItem_Click(object sender, EventArgs e) {
            DocViewer dv = new DocViewer();
            dv.Show();
            ToolStripMenuItem t = (ToolStripMenuItem)sender;
            dv.viewDocument(t.Text);
        }

        private void theUniversalTransverseMercatorSystempdfToolStripMenuItem_Click(object sender, EventArgs e) {
            DocViewer dv = new DocViewer();
            dv.Show();
            ToolStripMenuItem t = (ToolStripMenuItem)sender;
            dv.viewDocument(t.Text);
        }

        private void utmzonesjpgToolStripMenuItem_Click(object sender, EventArgs e) {
            DocViewer dv = new DocViewer();
            dv.Show();
            ToolStripMenuItem t = (ToolStripMenuItem)sender;
            dv.viewDocument(t.Text);
        }

        private void uTMConversions1xlsToolStripMenuItem_Click(object sender, EventArgs e) {
            DocViewer dv = new DocViewer();
            dv.Show();
            ToolStripMenuItem t = (ToolStripMenuItem)sender;
            dv.viewDocument(t.Text);
        }

        private void zoneCentralMeridianspdfToolStripMenuItem_Click(object sender, EventArgs e) {
            DocViewer dv = new DocViewer();
            dv.Show();
            ToolStripMenuItem t = (ToolStripMenuItem)sender;
            dv.viewDocument(t.Text);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
            this.Dispose();
        }

        private void sourceCodeToolStripMenuItem_Click(object sender, EventArgs e) {
            DocViewer dv = new DocViewer();
            dv.Show();
            dv.viewDocument(".\\html\\index.html");
        }

        private void aboutUtmConvertToolStripMenuItem_Click(object sender, EventArgs e) {
            AboutBoxMainForm a = new AboutBoxMainForm();
            a.Show();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e) {
            xmlOpenFileDialog.ShowDialog();
        }

        private void xmlOpenFileDialog_FileOk(object sender, CancelEventArgs e) {
            _parser = new XmlParser();
            _parser.readFile(xmlOpenFileDialog.FileName);
            inputXmlTextBox.Text = _parser.FileString;
            _parser.Parse(latLonFormatToolStripComboBox.Text);
            parserToolStripStatusLabel.Text = "Read: " + _parser.Status + ", ";
            utmPointsTextBox.Clear();
			utmPointsTextBox.AppendText("x:\ty:\r\n");
            foreach (UtmPointSet ps in _parser.UtmPointSetList) {
                foreach(UtmPoint pt in ps.Points) 
                utmPointsTextBox.AppendText(pt.Point.X + "\t" + pt.Point.Y + "\r\n");
            }
            if (_parser.UtmPointSetList.Count > 0) {
                Printer print = new Printer();
                printerToolStripStatusLabel.Text =
                    print.writeUtmXmlFile(_parser.UtmPointSetList);
                outputXmlTextBox.Text = print.Output;
            }
            latLonPointsTextBox.Clear();
            foreach (LatLonPointSet ps in _parser.LatLonPointSetList) {
                foreach (LatLonPoint pt in ps.Points)
                    latLonPointsTextBox.AppendText("lat: " 
                        + pt.Lat + ", ns: " + pt.Ns
                        + ", lon: " + pt.Lon + ", ew: " 
                        + pt.Ew + ", n: " + pt.Note + "\r\n");
            }
            if (_parser.LatLonPointSetList.Count > 0) {
                Printer print = new Printer();
                printerToolStripStatusLabel.Text =
                    print.writeLatLonXmlFile(_parser.LatLonPointSetList);
                outputXmlTextBox.Text = print.Output;
            }
            utmPointsTextBox.AppendText("Status: " + _parser.Status);
            latLonPointsTextBox.AppendText("Status " + _parser.Status);
        }

    }
}
