using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Telerik;
using Telerik.Web.UI;

namespace Helper
{
    public class Common
    {
        public enum ERROR_MESSAGE_TYPE : byte
        {
            SUCCESS = 1,
            ERROR = 2,
            WARNING = 3
        }

        public static string GenerateRandomPassword(byte PasswordLength)
        {
            string _allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGH­JKLMNOPQRSTUVWXYZ123456789";
            Random randNum = new Random();
            char[] chars = new char[PasswordLength];
            int allowedCharCount = _allowedChars.Length;

            for (int i = 0; i < PasswordLength; i++)
            {
                chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())];
            }

            return new string(chars);

        }

        public static void SetDatePickerUICulture(RadDatePicker Datepicker)
        {
            GregorianCalendar calendar = new GregorianCalendar();
            CultureInfo cinfo = new CultureInfo("ar-SA");
            cinfo.DateTimeFormat.Calendar = calendar;
            Datepicker.Culture = cinfo;
        }

        public static string GetTagValue(string ConfigFile, string TagName)
        {
            string xmlFileName = string.Empty;
            System.Data.DataSet configdata = new System.Data.DataSet();
            if (System.Web.HttpContext.Current != null)
            {
                xmlFileName = System.Web.HttpContext.Current.Server.MapPath(ConfigFile);
                configdata.ReadXml(xmlFileName.Replace("\\Controls", ""));

                xmlFileName = configdata.Tables[0].Rows[0][TagName].ToString();
                configdata = null;
            }
            else
            {
                xmlFileName = AppDomain.CurrentDomain.BaseDirectory + ConfigFile;
                configdata.ReadXml(xmlFileName.Replace("~", ""));
                xmlFileName = configdata.Tables[0].Rows[0][TagName].ToString();
                configdata = null;
            }
            return xmlFileName;
        }

        public static void SendEmail(string EmailTo, string Subject, string EmailMessage)
        {
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            message.To.Add(EmailTo);
            message.Subject = Subject;
            message.From = new System.Net.Mail.MailAddress(GetTagValue("Config.xml", "fromemail"));
            message.Body = EmailMessage;
            message.IsBodyHtml = true;
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(GetTagValue("Config.xml", "smptserver"));
            smtp.UseDefaultCredentials = false;
            NetworkCredential basicAuthenticationInfo = new NetworkCredential(GetTagValue("Config.xml", "smptpuser"), GetTagValue("Config.xml", "smptppwd"));
            smtp.Credentials = basicAuthenticationInfo;
            smtp.Port = Convert.ToInt32(GetTagValue("Config.xml", "smptpPort"));
            smtp.EnableSsl = Convert.ToBoolean(GetTagValue("Config.xml", "smptpEnableSsl"));
            smtp.Send(message);
        }
    }
}
