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

namespace GSMNetDemo
{
	public partial class Form1 : Form
	{
		int modemStatus;
		Encode_Msg recMsg = new Encode_Msg();
		Encode_Msg readMsg = new Encode_Msg();

		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			// Get version and Date
			label2.Text = GSM.GM_SYS_GetLibVersion();
			label4.Text = GSM.GM_SYS_GetLibDate();

			comboBox1.SelectedIndex = 0;
		}

		private void button1_Click(object sender, EventArgs e)
		{
			int error_code;
			int modem_port = Convert.ToInt32(textBox1.Text);
			error_code = GSM.GM_SYS_InitModem("0000", modem_port);
			if (error_code == GSM.GM_NOERROR)
			{
				timer1.Enabled = true;
			}else
			{
				label6.Text = "Modem init. fail : " + error_code;
			}
		}

		private void timer1_Tick(object sender, EventArgs e)
		{
			modemStatus = GSM.GM_SYS_CheckModemStatus();

			if (modemStatus == GSM.GM_NOERROR)
			{
				label6.Text = "Initializing success...";
			}else
				label6.Text = "Initializing...";

			int gmReg = GSM.GM_SYS_CheckReg();
			switch (gmReg)
			{
				case 1:
					label11.Text = "Registered";
					break;
				case 3:
					label11.Text = "Registration denied";
					break;
				case 5:
					label11.Text = "Registered, roaming";
					break;
				default:
					label11.Text = "Not registered";
					break;

			}

			int gmSignal = GSM.GM_SYS_CheckSignal();
			label9.Text = gmSignal.ToString();

			//Timer1 status light
			if (label12.BackColor == Color.Green)
				label12.BackColor = Color.White;
			else
				label12.BackColor = Color.Green;

			//check any new SMS coming
			if (GSM.GM_SMS_GetNewMsg(ref recMsg) != 0)
			{
				if (recMsg.mode == GSM.GSM_UCS2)
					label19.Text = "UCS2";
				else
					label19.Text = "7Bits";
				textBox4.Text = recMsg.phoneNumber;
				label21.Text = recMsg.time;
				textBox5.Text = recMsg.msg;
				label25.Text = recMsg.msg.Length.ToString();
			}
		}

		private void button2_Click(object sender, EventArgs e)
		{
			int result;

			GSMNet.Encode_Msg sendMsg = new GSMNet.Encode_Msg();

			if (comboBox1.SelectedIndex == 0)
				sendMsg.mode = GSM.GSM_UCS2;
			else
				sendMsg.mode = GSM.GSM_7BIT;
			sendMsg.phoneNumber = textBox2.Text;
			sendMsg.msg = textBox3.Text;

			result = GSM.GM_SMS_SendMsg(sendMsg);

			if (result == GSM.GM_NOERROR)
				timer2.Enabled = true;
			else
			{
				label16.BackColor = Color.Red;
				label16.Text = "can't send";
			}
		}

		//check status of Sending
		private void timer2_Tick(object sender, EventArgs e)
		{
			int sendStatus;
			sendStatus = GSM.GM_SYS_CheckCmdStatus();
			switch (sendStatus)
			{
				case GSM.GM_NOERROR:	//success, and send next one
					label16.BackColor = Color.Green;
					label16.Text = "Success";
					timer2.Enabled = false;
					break;
				case GSM.GM_BUSY:	//sending, and waiting replay
					if (label16.BackColor == Color.Red)
						label16.BackColor = Color.White;
					else
						label16.BackColor = Color.Red;
					label16.Text = "Sending";
					break;
				case GSM.GM_TIMEOUT:
					label16.BackColor = Color.Red;
					label16.Text = "TimeOut";
					break;
				default:
					label16.BackColor = Color.Red;
					label16.Text = "Error Code=" + sendStatus;
					break;
			}
		}
	}//class Form1
}