using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

namespace Morakhassi
{
    class clsShamsi
    {
        private String dtShamsi;
        private DateTime dtMiladi;
        private int IsShamsiDT;

        public clsShamsi() { }

        public clsShamsi(int _Year, int _Month, int _Day)
        {
            PersianCalendar Per = new PersianCalendar();
            dtMiladi = Per.ToDateTime(_Year, _Month, _Day, 0, 0, 0, 0);
        }

        public clsShamsi(DateTime MiladiDate)
        {
            PersianCalendar calFar1 = new PersianCalendar();
            dtShamsi = calFar1.GetYear(MiladiDate).ToString() + "/" + calFar1.GetMonth(MiladiDate).ToString("00") + "/" + calFar1.GetDayOfMonth(MiladiDate).ToString("00");
        }

        public DateTime ShamsiToMiladi(String ShamsiDate)
        {
            DateTime MyMilDate;
            String p1, p2, p3;
            p1 = ShamsiDate.Substring(0, 4);
            p2 = ShamsiDate.Substring(5, 2);
            p3 = ShamsiDate.Substring(8, 2);
            PersianCalendar pers = new PersianCalendar();
            MyMilDate = pers.ToDateTime(int.Parse(p1), int.Parse(p2), int.Parse(p3), 0, 0, 0, 0);
            return MyMilDate;
        }

        public clsShamsi(String shamsiDate)
        {
            int dtCHK = 0;
            if (shamsiDate.Length == 10)
            {
                for (int i = 0; i <= 9; i++)
                {

                    Char aa = Convert.ToChar(shamsiDate.Substring(i, 1));
                    Int16 bb = Convert.ToInt16(aa);
                    if (i != 4 && i != 7)
                    {
                        if (bb >= 48 && bb <= 57)
                        { }
                        else
                        {
                            dtCHK = 1;
                        }
                    }
                    else
                    {
                        if (bb != 47)
                        {
                            dtCHK = 1;
                        }
                    }
                }
                try
                {
                    if (int.Parse(shamsiDate.Substring(5, 2)) < 1 || int.Parse(shamsiDate.Substring(5, 2)) > 12)
                    {
                        dtCHK = 1;
                    }
                    else
                    {
                        if (int.Parse(shamsiDate.Substring(5, 2)) < 7)
                        {
                            if (int.Parse(shamsiDate.Substring(8, 2)) < 1 || int.Parse(shamsiDate.Substring(8, 2)) > 31)
                            {
                                dtCHK = 1;
                            }
                        }
                        else if (int.Parse(shamsiDate.Substring(5, 2)) > 6 && int.Parse(shamsiDate.Substring(5, 2)) < 12)
                        {
                            if (int.Parse(shamsiDate.Substring(8, 2)) < 1 || int.Parse(shamsiDate.Substring(8, 2)) > 30)
                            {
                                dtCHK = 1;
                            }
                        }
                        else if (int.Parse(shamsiDate.Substring(5, 2)) == 12)
                        {
                            if (isKabiseh(shamsiDate.Substring(0, 4)) == 0)//Year Is Kabiseh
                            {
                                if (int.Parse(shamsiDate.Substring(8, 2)) < 1 || int.Parse(shamsiDate.Substring(8, 2)) > 30)
                                {
                                    dtCHK = 1;
                                }
                            }
                            else //Year Is Not Kabiseh
                            {
                                if (int.Parse(shamsiDate.Substring(8, 2)) < 1 || int.Parse(shamsiDate.Substring(8, 2)) > 29)
                                {
                                    dtCHK = 1;
                                }
                            }
                        }
                    }
                }
                catch
                {
                    dtCHK = 1;
                }
            }
            else
            {
                dtCHK = 1;
            }
            IsShamsiDT = dtCHK; // 1 = Not Shamsi Date , 0 = Shamsi Date
        }

        private int isKabiseh(String shamsiYaer)
        {
            int isKabise = 0;
            if ((int.Parse(shamsiYaer) + 1) % 100 == 0 && (int.Parse(shamsiYaer) + 1) % 400 == 0)
            {
                isKabise = 0;
            }
            else if ((int.Parse(shamsiYaer) + 1) % 100 != 0 && (int.Parse(shamsiYaer) + 1) % 4 == 0)
            {
                isKabise = 0;
            }
            else
            {
                isKabise = 1;
            }
            return isKabise;
        }

        public String ShamsiDate
        {
            get
            {
                return dtShamsi;
            }
            set
            {
                dtShamsi = value;
            }
        }

        public DateTime MiladiDate
        {
            get
            {
                return dtMiladi;
            }
            set
            {
                dtMiladi = value;
            }
        }

        public int ShamsiCheck
        {
            get
            {
                return IsShamsiDT;
            }
            set
            {
                IsShamsiDT = value;
            }
        }
    }
}
