using System;

namespace Dish_Project
{
	/// <summary>
	/// Summary description for PersionCelender.
	/// </summary>
	public class PersionCelender
	{
		private int day,month,year;		
		public string Date;
		public int[] date=new int[3];		
		public PersionCelender()
		{
			
		}

		public PersionCelender(System.DateTime dateTime)
		{
			date=ConvertToPersia(dateTime);
		}

		public PersionCelender(int year,int month,int day)
		{
			ConvertToMiladi(year,month,day);
		}
		public int[] ConvertToPersia(System.DateTime dateTime)
		{
			int[] date=new int[3];		
			int deydiff;
			day=dateTime.DayOfYear;
			year=dateTime.Year;
			if(System.DateTime.IsLeapYear(System.DateTime.Today.Year-1))
				deydiff=11;
			else
				deydiff=10;
			if(day>79)
			{
				day-=79;
				if(day<=186)
				{
					month=day/31;											
					if(day%31==0)
						day=31;
					else 
					{
						month++;
						day%=31;
					}
					year-=621;
				}
				else
				{
					day-=186;
					month=(day/30)+6;
					if(day%30==0)
						day=30;
					else 
					{
						month++;
						day%=30;
					}
					year-=621;
				}
			}
			else
			{
				day+=deydiff;
				month=(day/30)+9;
				if(day%30==0)
					day=30;
				else
				{
					month++;
					day%=30;
				}
				year-=622;
			}
			date[0]=day;
			date[1]=month;
			date[2]=year;
			Date=date[2].ToString()+"-"+date[0].ToString()+"-"+date[1].ToString();
			return 	date;		
		}
//*****************************************************************************************
		public string ConvertToSqlDate(int DayOfYear,int Year)
		{
			int[] date=new int[3];		
			int deydiff;
			day=DayOfYear;
			year=Year;
			if(System.DateTime.IsLeapYear(System.DateTime.Today.Year-1))
				deydiff=11;
			else
				deydiff=10;
			if(day>79)
			{
				day-=79;
				if(day<=186)
				{
					month=day/31;											
					if(day%31==0)
						day=31;
					else 
					{
						month++;
						day%=31;
					}
					year-=621;
				}
				else
				{
					day-=186;
					month=(day/30)+6;
					if(day%30==0)
						day=30;
					else 
					{
						month++;
						day%=30;
					}
					year-=621;
				}
			}
			else
			{
				day+=deydiff;
				month=(day/30)+9;
				if(day%30==0)
					day=30;
				else
				{
					month++;
					day%=30;
				}
				year-=622;
			}
			date=ConvertToMiladi(year,month,day);
			Date=date[2].ToString()+"-"+date[0].ToString()+"-"+date[1].ToString();
			return 	Date;		
		}
//*****************************************************************************************
		public int[] ConvertToMiladi(int yaer,int month,int day)
		{	
			//******************* Leap year
			if (yaer == 1378)
			{
				if ((month == 12) && (day == 10))
				{
					date[2] = 2000;
					date[1] = 2;
					date[0] = 29;
					return date;
				}
				if (month == 12 && day > 10)
					day --;
			}
			else if( yaer == 1379)
			{
				day --;
				if (day == 0) 
				{
					month = month - 1;
					if (month > 0 && month < 7 ) 
						day = 31;
					if (month > 6) 
						day = 30;
					if (month == 0)
					{
						day = 29;
						month = 12;
						yaer --;
					}
				}
			}
			//'*******************
			if((month < 10) ||(month == 10 && day < 11)) 
				yaer+=621;
			else
				yaer+=622;
			switch (month)
			{
				case 1:
					if (day < 12)
					{
						month = 3;
						day +=20;
					}
					else
					{
						month = 4;
						day -= 11;
					}
					break;
				case 2:
					if(day < 11)
					{
						month = 4;
						day += 20;
					}
					else
					{
						month = 5;
						day-=10;
					}
					break;
				case 3:
					if (day < 11)
					{
						month = 5;
						day += 21;
					}
					else
					{
						month = 6;
						day-=10;
					}
					break;
				case 4:
					if (day < 10)
					{
						month = 6;
						day+= 21;
					}
					else
					{
						month = 7;
						day -= 9;
					}
					break;
				case 5:
				case 6:
				case 8:
					if (day < 10)
					{
						month += 2;
						day+= 22;
					}
					else
					{
						month+= 3;
						day-=9;
					}
					break;
				case 7:
					if (day < 9)
					{
						month = 9;
						day+=22;
					}
					else
					{
						month = 10;
						day-=8;
					}
					break;
				case 9:
					if (day < 10)
					{
						month = 11;
						day+= 21;
					}
					else
					{
						month = 12;
						day-= 9;
					}
					break;
				case 10:
					if (day < 11)
					{
						month = 12;
						day+= 21;
					}
					else
					{
						month = 1;
						day-= 10;
					}
					break;
				case 11:
					if (day < 12)
					{
						month = 1;
						day+= 20;
					}
					else
					{
						month = 2;
						day-=11;
					}
					break;
				case 12:
					if (day < 10)
					{
						month = 2;
						day+= 19;
					}
					else
					{
						month = 3;
						day-= 9;
					}
					break;
			}
			date[0]=day;
			date[1]=month;
			date[2]=yaer;
			Date=date[2].ToString()+"-"+date[1].ToString()+"-"+date[0].ToString();
			return date;
		}

		public string ConvertToSqlDate(System.DateTime dateTime)
		{
			date[0]=dateTime.Day;
			date[1]=dateTime.Month;
			date[2]=dateTime.Year;
			Date=date[2].ToString()+"-"+date[0].ToString()+"-"+date[1].ToString();
			return Date;
		}
	}
}
