using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace UtmConvert {
    public class Printer {

		public delegate void updateErrorEventHandler(Exception error);
		public event updateErrorEventHandler updateErrorEvent;

        string _status = "ok";
        public string Status {
            get { return _status; }
        }

        string _output = "";
        public string Output {
            get { return _output; }
        }

        public string writeUtmXmlFile(List<UtmPointSet> lps) {
            _status = "ok";
            try {
                StreamWriter sWrite = new StreamWriter(lps[0]
                    .LocationName + ".xml", false, Encoding.UTF8);
                _output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n\r\n";
                foreach (UtmPointSet ps in lps) {
                    _output = _output + "<utm_point_set location=\"" 
                        + ps.LocationName + "\" datum=\"" + ps.Datum + "\">\r\n";
                    foreach (UtmPoint pt in ps.Points)
                        _output = _output + "\t<dd_dd easting=\"" + pt.Point.X 
                            + "\" northing=\"" + pt.Point.Y + "\" zone=\""
                            + pt.Zone + "\" note=\"" + pt.Note
                            + "\" handle=\"" + pt.Handle + "\" />\r\n";
                    _output = _output + "</utm_point_set>\r\n";
                }
                sWrite.Write(_output);
                sWrite.Close();
                return "utm_" + lps[0].LocationName + ".xml";
            } catch (Exception e) {
				if (updateErrorEvent != null)
					updateErrorEvent.Invoke(e);
                _status = e.Message;
                return _status;
            }
        }

        public string writeLatLonXmlFile(List<LatLonPointSet> lps) {
            _status = "ok";
            try {
                StreamWriter sWrite = new StreamWriter(lps[0]
                    .LocationName + ".xml", false, Encoding.UTF8);
                _output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n\r\n";
                foreach (LatLonPointSet ps in lps) {
                    _output = _output + "<lat_lon_point_set location=\"" + ps.LocationName
                        + "\" datum=\"" + ps.Datum + "\">\r\n";
                    foreach (LatLonPoint pt in ps.Points) {
                        if (pt.Format == "ddd_dd")
                            _output = _output + "\t<" + pt.Format + " " + "lat=\"" 
                                + ConvertDegRad.getDegrees(Math.Abs(pt.Lat)) 
                                + "\" ns=\"" + pt.Ns + "\" lon=\"" 
                                + ConvertDegRad.getDegrees(Math.Abs(pt.Lon))
                                + "\" ew=\"" + pt.Ew + "\" note=\"" + pt.Note
                                +"\" handle=\"" + pt.Handle + "\" />\r\n";
                        else if (pt.Format == "ddd_mm_mm")
                            _output = _output + "\t<" + pt.Format + " " + "lat=\"" 
                                + ConvertDegRad.getDegreesMin(Math.Abs(pt.Lat)) 
                                + "\" ns=\"" + pt.Ns + "\" lon=\"" 
                                + ConvertDegRad.getDegreesMin(Math.Abs(pt.Lon))
                                + "\" ew=\"" + pt.Ew + "\" note=\"" + pt.Note
                                +"\" handle=\"" + pt.Handle + "\" />\r\n";
                        else
                            _output = _output + "\t<" + pt.Format + " " + "lat=\""
                                + ConvertDegRad.getDegreesMinSec(Math.Abs(pt.Lat)) 
                                + "\" ns=\"" + pt.Ns + "\" lon=\"" 
                                + ConvertDegRad.getDegreesMinSec(Math.Abs(pt.Lon))
                                + "\" ew=\"" + pt.Ew + "\" note=\"" + pt.Note
                                +"\" handle=\"" + pt.Handle + "\" />\r\n";
                    }
                    _output = _output + "</lat_lon_point_set>\r\n";
                }
                sWrite.Write(_output);
                sWrite.Close();
                return "lat_lon_" + lps[0].LocationName + ".xml";
            } catch (Exception e) {
				if (updateErrorEvent != null)
					updateErrorEvent.Invoke(e);
                _status = e.Message;
                return _status;
            }
        }
    }


}
