using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using System.Globalization;
using System.Drawing;

namespace Aram_CRM_Project.BLL
{
    //class Utility
    //{
    //}
    public class Utility
    {
        public void ClearControl(Control con)
        {
            foreach (Control x in con.Controls)
            {
                if (x is TextBox)
                {
                    TextBox txt = (TextBox)x;
                    txt.Text = string.Empty;
                }
                if (x is ComboBox)
                {
                    ComboBox cbo = (ComboBox)x;
                    cbo.SelectedIndex = -1;
                }
                if (x is PictureBox)
                {
                    PictureBox Pic = (PictureBox)x;
                    Pic.Image = null;
                }
                if (x is CheckBox)
                {
                    CheckBox chk = (CheckBox)x;
                    chk.Checked = false;
                }
                if (x is GroupBox)
                {
                    ClearControl(x);
                }
                if (x is Panel)
                {
                    ClearControl(x);
                }
                if (x is TabControl)
                {
                    ClearControl(x);
                }
            }
        }

        public void DisableControl(Control con)
        {
            foreach (Control x in con.Controls)
            {
                if (x is TextBox)
                {
                    TextBox txt = (TextBox)x;
                    txt.Enabled = false;
                }
                if (x is ComboBox)
                {
                    ComboBox cbo = (ComboBox)x;
                    cbo.Enabled = false;
                }
                if (x is PictureBox)
                {
                    PictureBox Pic = (PictureBox)x;
                    Pic.Enabled = false;
                }
                if (x is CheckBox)
                {
                    CheckBox chk = (CheckBox)x;
                    chk.Enabled = false;
                }
                if (x is Button)
                {
                    Button btn = (Button)x;
                    btn.Enabled = false;
                }
                if (x is GroupBox)
                {
                    DisableControl(x);
                }
                if (x is Panel)
                {
                    DisableControl(x);
                }
                if (x is TabControl)
                {
                    DisableControl(x);
                }
            }
        }

        public void EnableControl(Control con)
        {
            foreach (Control x in con.Controls)
            {
                if (x is TextBox)
                {
                    TextBox txt = (TextBox)x;
                    txt.Enabled = true;
                }
                if (x is ComboBox)
                {
                    ComboBox cbo = (ComboBox)x;
                    cbo.Enabled = true;
                }
                if (x is PictureBox)
                {
                    PictureBox Pic = (PictureBox)x;
                    Pic.Enabled = true;
                }
                if (x is CheckBox)
                {
                    CheckBox chk = (CheckBox)x;
                    chk.Enabled = true;
                }
                if (x is GroupBox)
                {
                    EnableControl(x);
                }
                if (x is Panel)
                {
                    EnableControl(x);
                }
                if (x is TabControl)
                {
                    EnableControl(x);
                }
            }
        }

        public string ChangeChar(string Name)
        {
            return Name.Replace("ي", "ی");
        }

        public string Encode(string str)
        {
            byte[] EncBuff = System.Text.Encoding.UTF8.GetBytes(str);
            return Convert.ToBase64String(EncBuff);
        }

        public string Decode(string str)
        {
            byte[] DecBuff = Convert.FromBase64String(str);
            return System.Text.Encoding.UTF8.GetString(DecBuff);
        }

        public string PersianDate()
        {
            PersianCalendar PersianDate = new PersianCalendar();
            DateTime Date = DateTime.Now;
            return PersianDate.GetYear(Date).ToString() + "/" + PersianDate.GetMonth(Date).ToString() + "/" + PersianDate.GetDayOfMonth(Date).ToString();
        }

        public string ThousandSeparator(string str)
        {
            string Num = str.Replace(",", "");
            if (Num.Length > 3)
            {
                int Length = Num.Length;
                int Result = Length / 3;
                int Remain = Length - (Result * 3);
                int Count;
                if (Remain == 0)
                    Count = Result - 1;
                else
                    Count = Result;

                for (int i = 0; i < Count; i++)
                {
                    Length = Length - 3;
                    Num = Num.Insert(Length, ",");
                }

                return Num;
            }
            else
            {
                if (str == ",")
                    str = "";
                return str;
            }
        }

        public byte[] ConvertImageToByteArray(System.Drawing.Image imageToConvert, ImageFormat formatOfImage)
        {
            byte[] Ret = null;

            try
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    if (imageToConvert != null)
                        imageToConvert.Save(ms, formatOfImage);
                    Ret = ms.ToArray();
                }
                return Ret;
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
                return Ret;
            }
        }
        public Image ConvertByteArrayToImage(byte[] byteArray)
        {
            try
            {
                if (byteArray != null && byteArray.Length != 0)
                {
                    MemoryStream ms = new MemoryStream(byteArray, 0, byteArray.Length);
                    ms.Write(byteArray, 0, byteArray.Length);
                    return Image.FromStream(ms, true);
                }
                return null;
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
                return null;
            }
        }
    }
}
