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 OsaGateway
{
    public partial class NewContact : Form
    {
        Contact contact = null;

        public NewContact()
        {
            InitializeComponent();
        }
        public NewContact(Guid contactGuid)
        {
            InitializeComponent();
            contact = Contact.Get(contactGuid);
            button1.Text = "Uložit";

            textBox1.Text = contact.Name;
            textBox2.Text = contact.Phone;
        }
        public NewContact(Contact newContact)
        {
            InitializeComponent();
            contact = newContact;
            button1.Text = "Uložit";

            textBox1.Text = contact.Name;
            textBox2.Text = contact.Phone;
        }
        private void SubmitContact(object sender, EventArgs e)
        {
            long phoneNumber;
            if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))
            {
                if (string.IsNullOrEmpty(textBox1.Text))
                    textBox1.BackColor = Color.Orange;
                else
                    textBox1.BackColor = Color.White;
                if (string.IsNullOrEmpty(textBox2.Text))
                    textBox2.BackColor = Color.Orange;
                else
                    textBox2.BackColor = Color.White;
            }
            else if(!long.TryParse(textBox2.Text.Replace("+",string.Empty), out phoneNumber))
            {
                textBox2.BackColor = Color.Orange;
            }
            else
            {
                if (contact == null)
                {
                    Contact _c = new Contact(textBox1.Text, textBox2.Text);
                    _c.Save();
                }
                else
                {
                    contact.Name = textBox1.Text;
                    contact.Phone = textBox2.Text;
                    contact.Save();
                }
                this.Hide();
                this.Close();
            }

        }
    }
}
