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

namespace OsaGateway
{
    static public class Connection
    {
        static private string xmlFile = Application.StartupPath + @"\" + @"connection.xml";
        static public bool SavePort(string port)
        {
            XDocument xdoc;
            try
            {
                if (!File.Exists(xmlFile))
                {
                    XElement newXdoc = new XElement("Connect");
                    newXdoc.Add(new XElement("Port"));
                    newXdoc.Add(new XElement("ServiceURL"));
                    newXdoc.Save(xmlFile);
                }
                xdoc = XDocument.Load(xmlFile);
                xdoc.Element("Connect").SetElementValue("Port", port);
                xdoc.Save(xmlFile);
                return true;

            }
            catch (Exception)
            {
                return false;
            }
        }
        static public string LoadPort()
        {
            XDocument xdoc;
            try
            {
                if (!File.Exists(xmlFile))
                    return string.Empty;
                xdoc = XDocument.Load(xmlFile);
                return xdoc.Element("Connect").Element("Port").Value.ToString();
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }
        static public bool SaveServiceUrl(string url)
        {
            XDocument xdoc;
            try
            {
                if (!File.Exists(xmlFile))
                {
                    XElement newXdoc = new XElement("Connect");
                    newXdoc.Add(new XElement("Port"));
                    newXdoc.Add(new XElement("ServiceURL"));
                    newXdoc.Save(xmlFile);
                }
                xdoc = XDocument.Load(xmlFile);
                xdoc.Element("Connect").SetElementValue("ServiceURL", url);
                xdoc.Save(xmlFile);
                return true;

            }
            catch (Exception)
            {
                return false;
            }
        }
        static public string LoadServiceUrl()
        {
            XDocument xdoc;
            try
            {
                if (!File.Exists(xmlFile))
                    return string.Empty;
                xdoc = XDocument.Load(xmlFile);
                return xdoc.Element("Connect").Element("ServiceURL").Value.ToString();
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }
    }
}
