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;
using System.Globalization;
using System.Threading;

namespace OsaGateway
{
    public partial class NewSMS : Form
    {
        string[] items;
        string message;
        Thread thread;

        int smsCount = 1;
        int charCount = 0;

        public int SMSCount
        {
            get
            {
                return this.smsCount;
            }
            set
            {
                this.smsCount = value;
                labelSMS.Text = this.smsCount.ToString();
                if (this.smsCount <= 10)
                    progressBar1.Value = this.smsCount;
            }
        }
        public int CharCount
        {
            get
            {
                return this.charCount;
            }
            set
            {
                this.charCount = value;
                labelChar.Text = this.charCount.ToString();
                if (this.charCount <= 160)
                    progressBar2.Value = this.charCount;
            }
        }

        public NewSMS()
        {
            InitializeComponent();
            Contacts.Choosed += new Contacts.ContactsFormHandler(Contacts_Choosed);
        }
        public NewSMS(string phoneNumber)
        {
            InitializeComponent();
            Contacts.Choosed += new Contacts.ContactsFormHandler(Contacts_Choosed);
            this.mtbPhoneNumber.Text = phoneNumber + "; ";
            this.richTextBox1.TabIndex = 1;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Contacts c = new Contacts(true);
            c.Show();
        }
        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            if (richTextBox1.TextLength == 0)
            {
                SMSCount = 0;
                CharCount = richTextBox1.TextLength;
            }
            if (richTextBox1.TextLength >= 1 && richTextBox1.TextLength <= 160)
            {
                SMSCount = 1;
                CharCount = richTextBox1.TextLength;
            }
            if (richTextBox1.TextLength > 160)
            {
                if ((richTextBox1.TextLength % 160) == 0)
                {
                    CharCount = 160;
                    SMSCount = richTextBox1.TextLength / 160;
                }
                else
                {
                    CharCount = (richTextBox1.TextLength % 160);
                    SMSCount = (richTextBox1.TextLength / 160) + 1;
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (richTextBox1.TextLength > 0 && mtbPhoneNumber.TextLength > 0)
            {
                items = mtbPhoneNumber.Text.Replace(" ", string.Empty).Split(new string[] { ";", ",", ".", "/", "|" }, StringSplitOptions.RemoveEmptyEntries);
                message = this.RemoveDiacritics(richTextBox1.Text);
                string[] items2 = items;
                if (items.Count() <= 0)
                    mtbPhoneNumber.BackColor = Color.Orange;
                else
                {
                    thread = new Thread(new ThreadStart(RunSend));
                    thread.Start();
                    foreach (string item in items2)
                        Message.Add(item, message, TrafficType.Outgoing);
                    this.Hide();
                    this.Close();
                }
            }
            else
            {
                if (richTextBox1.TextLength <= 0)
                    richTextBox1.BackColor = Color.Orange;
                else
                    richTextBox1.BackColor = Color.White;
                if (mtbPhoneNumber.TextLength <= 0)
                    mtbPhoneNumber.BackColor = Color.Orange;
                else
                    mtbPhoneNumber.BackColor = Color.White;
            }
        }

        private void RunSend()
        {
            foreach (string item in items)
                Program.gateway.SendSMS(item, message, true);
        }

        private void Contacts_Choosed(List<Contact> contacts)
        {
            foreach (Contact item in contacts)
            {
                mtbPhoneNumber.Text += item.Phone + "; ";
            }
        }
        public string RemoveDiacritics(string s)
        {
            s = s.Normalize(NormalizationForm.FormD);
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < s.Length; i++)
            {
                if (CharUnicodeInfo.GetUnicodeCategory(s[i]) != UnicodeCategory.NonSpacingMark) sb.Append(s[i]);
            }

            return sb.ToString();
        }
    }
}
