using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text.RegularExpressions;

namespace CheckFirmwareUpgradeEligibility
{
	#region "Summary"

	/* Project-Firmware Upgrade
	 * Description:check user eligibility.
	 * Created By-Xavient Offshore
	 * Created Date-17 Dec 2013
     *
     * 
     * 
     * 
     * 
     * 
     * 
	*/
	#endregion

	#region "Interface declaration"

	interface ICheckLegacyWIFI
	{
		string checkEligibilityStatus(ClsCheckLegacyWIFI obj);
	}

	#endregion

	#region "Class declaration and Implements Interface"

	public class ClsCheckLegacyWIFI : ICheckLegacyWIFI
	{
		#region "Private Variable Declaration"

		private string _AccountNumber;
		private string _MacAddress;
		private string _PhoneNumber;
		private string _ZipCode;

		#endregion

		#region "Public Property Declaration"

		public string AccountNumber
		{
			get
			{
				return this._AccountNumber;
			}
			set
			{
				this._AccountNumber = value;
			}
		}

		public string MacAddress
		{
			get
			{
				return this._MacAddress;
			}
			set
			{
				this._MacAddress = value;
			}
		}

		public string PhoneNumber
		{
			get
			{
				return this._PhoneNumber;
			}
			set
			{
				this._PhoneNumber = value;
			}
		}

		public string ZipCode
		{
			get
			{
				return this._ZipCode;
			}
			set
			{
				this._ZipCode = value;
			}
		}

		#endregion

		#region "Public Methods"

		public string checkEligibilityStatus(ClsCheckLegacyWIFI objClsCheckLegacyWIFI)
		{
			string strReturnMessage = string.Empty;

			try
			{
				strReturnMessage = CheckInputValidation(objClsCheckLegacyWIFI);
				if (strReturnMessage == "")
				{
					if (objClsCheckLegacyWIFI.MacAddress != "")
					{
						strReturnMessage = GetEligibilityStatussMAC(objClsCheckLegacyWIFI);
					}
					else if (objClsCheckLegacyWIFI.ZipCode != "")
					{
                        bool val = Regex.IsMatch(objClsCheckLegacyWIFI.ZipCode, @"^\d+$");

                        if (val == false)
                        {
                            strReturnMessage = "The Account number or zip code entered is invalid. Try again by entering your 16 digit account and your 5 digit zipcode.";
                        }
                        else if (objClsCheckLegacyWIFI.ZipCode.Length != 5)
                        {
                            strReturnMessage = "The Account number or zip code entered is invalid. Try again by entering your 16 digit account and your 5 digit zipcode.";
                        }
                      
                        else if (objClsCheckLegacyWIFI.AccountNumber != "")
						{
							strReturnMessage = GetEligibilityStatussZIPAcc(objClsCheckLegacyWIFI);
						}
						else if (objClsCheckLegacyWIFI.PhoneNumber != "")
						{
                            bool valueNum = Regex.IsMatch(objClsCheckLegacyWIFI.PhoneNumber, @"^\d+$");

                            if (valueNum == false)
                            {
                                strReturnMessage = "The Phone number or zip code entered is invalid. Try again by entering your Charter phone number and your 5 digit zipcode.";
                            }
                            else if (objClsCheckLegacyWIFI.PhoneNumber.Length != 10)
                            {
                                strReturnMessage = "The Phone number or zip code entered is invalid. Try again by entering your Charter phone number and your 5 digit zipcode.";
                            }
                            else
                            {
							strReturnMessage = GetEligibilityStatussZipPhone(objClsCheckLegacyWIFI);
						}
                        }
						else
						{
                            strReturnMessage = "Enter your 16 digit account or Charter phone number and your 5 digit zipcode.";
						}
					}
					else if (objClsCheckLegacyWIFI.ZipCode == "" && objClsCheckLegacyWIFI.AccountNumber != "")
					{
                        strReturnMessage = "Enter your 16 digit account or Charter phone number and your 5 digit zipcode.";
					}
					else if (objClsCheckLegacyWIFI.ZipCode == "" && objClsCheckLegacyWIFI.PhoneNumber != "")
					{
                        strReturnMessage = "Enter your 16 digit account or Charter phone number and your 5 digit zipcode.";
					}
				}
			}
			catch (Exception ex)
			{
				strReturnMessage = ex.Message.ToString();
			}

               
                
               
			return strReturnMessage;
		}

		// Check Inputs validation entered by user for blank and digits
        
		private string CheckInputValidation(ClsCheckLegacyWIFI objClsCheckLegacyWIFI)
		{
			string strReturnMessage = string.Empty;
			if ((objClsCheckLegacyWIFI.AccountNumber == "") && (objClsCheckLegacyWIFI.ZipCode == "") && (objClsCheckLegacyWIFI.PhoneNumber == "") && (objClsCheckLegacyWIFI.MacAddress == ""))
			{
				strReturnMessage = "Form is missing required component, check your entries and try again.";
			}
			else if (objClsCheckLegacyWIFI.MacAddress != "")
			{
				string strSql = string.Empty;
				strSql = " select PATINDEX('%[^0-9A-Fa-f]%','" + objClsCheckLegacyWIFI.MacAddress + "')";
				int retval;
				SqlConnection SqlConn = GetConectionString();
				SqlCommand Sqlcmd = new SqlCommand(strSql, SqlConn);
				SqlConn.Open();
				retval = (int)Sqlcmd.ExecuteScalar();
				SqlConn.Close();

				if (retval > 0)
				{
					strReturnMessage = "The MAC address you entered is invalid. Check your address and try again.";
				}
				else if (objClsCheckLegacyWIFI.MacAddress.Length != 12)
				{
					strReturnMessage = "Your MAC address requires 12 characters, alpha and numeric.";
				}
			}
			else if (objClsCheckLegacyWIFI.AccountNumber != "")
			{
				bool val = Regex.IsMatch(objClsCheckLegacyWIFI.AccountNumber, @"^\d+$");

				if (val == false)
				{
                    strReturnMessage = "The Account number or zip code entered is invalid. Try again by entering your 16 digit account and your 5 digit zipcode.";
				}
				else if (objClsCheckLegacyWIFI.AccountNumber.Length != 16)
				{
                    strReturnMessage = "Enter your 16 digit account number. This can be found on your billing statement or at charter.com/myaccount.";
				}
			}
			else if (objClsCheckLegacyWIFI.ZipCode != "")
			{
				bool val = Regex.IsMatch(objClsCheckLegacyWIFI.ZipCode, @"^\d+$");

				if (val == false)
				{
                    strReturnMessage = "Enter your 5 digit zip code.";
				}
				else if (objClsCheckLegacyWIFI.ZipCode.Length != 5)
				{
                    strReturnMessage = "Enter your 5 digit zip code.";
				}
			}
			else if (objClsCheckLegacyWIFI.PhoneNumber != "")
			{
				bool val = Regex.IsMatch(objClsCheckLegacyWIFI.PhoneNumber, @"^\d+$");

				if (val == false)
				{
                    strReturnMessage = "Phone number requires 10 digits.";
				}
				else if (objClsCheckLegacyWIFI.PhoneNumber.Length != 10)
				{
					strReturnMessage = "Phone number requires 10 digits.";
				}
			}

            
			return strReturnMessage;
		}

		// Execute when user entered only Mac Address
		private string GetEligibilityStatussMAC(ClsCheckLegacyWIFI objClsCheckLegacyWIFI)
		{
			int rowcount = 0;
			string returnMsg = string.Empty;

			SqlConnection sqlConn = GetConectionString();

			try
			{
				SqlCommand sqlCmd = new SqlCommand();
				sqlCmd.CommandText = "PROC_GET_UPDATE_FIRMWARE_MACADDR";
				sqlCmd.CommandType = CommandType.StoredProcedure;
				sqlCmd.Connection = sqlConn;
				sqlCmd.Parameters.AddWithValue("@MAC_ADDR", objClsCheckLegacyWIFI.MacAddress.Trim().Replace("'", "''").ToString());
				sqlConn.Open();
				rowcount = (int)sqlCmd.ExecuteScalar();
				sqlConn.Close();
				if (rowcount > 0)
				{
					//returnMsg = "Your device is eligible for a firmware upgrade.";
				}
				else
				{
					returnMsg = "Your device does not require an update at this time.";
				}
			}
			catch (Exception ex)
			{
				returnMsg = ex.Message + ex.Source;
			}
			finally
			{
				if (sqlConn.State.Equals(ConnectionState.Open))
				{
					sqlConn.Close();
				}
			}

			return returnMsg;
		}

		// Execute when user entered only Zipcode or Account Number
		private string GetEligibilityStatussZIPAcc(ClsCheckLegacyWIFI objClsCheckLegacyWIFI)
		{
			int rowcount = 0;
			string returnMsg = string.Empty;
			SqlConnection sqlConn = new SqlConnection();
			sqlConn = GetConectionString();

			try
			{
				SqlCommand sqlCmd = new SqlCommand();
				sqlCmd.CommandText = "PROC_GET_UPDATE_FIRMWARE_ACC_ZIP";
				sqlCmd.CommandType = CommandType.StoredProcedure;
				sqlCmd.Connection = sqlConn;
				sqlCmd.Parameters.AddWithValue("@ACCOUNT_NUM", objClsCheckLegacyWIFI.AccountNumber.Trim().Replace("'", "''").ToString());
				sqlCmd.Parameters.AddWithValue("@ZIPCODE", objClsCheckLegacyWIFI.ZipCode.Trim().Replace("'", "''").ToString());
				sqlConn.Open();
				rowcount = (int)sqlCmd.ExecuteScalar();
				sqlConn.Close();
				if (rowcount > 0)
				{
					//returnMsg = "Your device is eligible for a firmware upgrade.";
				}
				else
				{
					returnMsg = "Your device does not require an update at this time.";
				}
			}
			catch (Exception ex)
			{
				returnMsg = ex.Message + ex.Source;
			}
			finally
			{
				if (sqlConn.State.Equals(ConnectionState.Open))
				{
					sqlConn.Close();
				}
			}

			return returnMsg;
		}

		// Execute when user entered only Zipcode or Phone Number
		private string GetEligibilityStatussZipPhone(ClsCheckLegacyWIFI objClsCheckLegacyWIFI)
		{
			int rowcount = 0;
			string returnMsg = string.Empty;

			SqlConnection sqlConn = GetConectionString();

			try
			{
				SqlCommand sqlCmd = new SqlCommand();
				sqlCmd.CommandText = "PROC_GET_UPDATE_FIRMWARE_ZIP_PHONE";
				sqlCmd.CommandType = CommandType.StoredProcedure;
				sqlCmd.Connection = sqlConn;
				sqlCmd.Parameters.AddWithValue("@ZIPCODE", objClsCheckLegacyWIFI.ZipCode.Trim().Replace("'", "''").ToString());
				sqlCmd.Parameters.AddWithValue("@PHONE_NUM", objClsCheckLegacyWIFI.PhoneNumber.Trim().Replace("'", "''").ToString());
				sqlConn.Open();
				rowcount = (int)sqlCmd.ExecuteScalar();
				sqlConn.Close();
				if (rowcount > 0)
				{
					//returnMsg = "Your device is eligible for a firmware upgrade.";
				}
				else
				{
					returnMsg = "Your device does not require an update at this time.";
				}
			}
			catch (Exception ex)
			{
				returnMsg = ex.Message + ex.Source;
			}
			finally
			{
				if (sqlConn.State.Equals(ConnectionState.Open))
				{
					sqlConn.Close();
				}
			}

			return returnMsg;
		}

		private SqlConnection GetConectionString()
		{
			// SqlConnection SqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["FirmwareDBConnection"].ConnectionString);
			// return SqlConn;
			// -----------------------------------------------------

			// string connectionString = "Server=ignatz;Database=CharterDotcom;user id=charter;password=charter;";
			string connectionString = @"Server=KSTLMSQL59;Database=CharterDotcom;user id=charterdotcomdbo;password=zYpYIpM0;";

			// SqlConnection sqlConn = new SqlConnection(ConfigurationSettings.AppSettings["FirmwareDBConnection"]);

			SqlConnection sqlConn = new SqlConnection(connectionString);
			return sqlConn;
		}

		/*
				private SqlConnection GetConnectionString()
				{
					// string connectionString = "Server=ignatz;Database=CharterDotcom;user id=charter;password=charter;";
					string connectionString = @"Server=KSTLMSQL59;Database=CharterDotcom;user id=charterdotcomdbo;password=zYpYIpM0;";

					// SqlConnection sqlConn = new SqlConnection(ConfigurationSettings.AppSettings["FirmwareDBConnection"]);

					SqlConnection sqlConn = new SqlConnection(connectionString);
					return sqlConn;
				}
		*/

		#endregion
	}
	#endregion
}
