using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using Stone.Class;
using PDT;
namespace Stone
{
    public partial class SaleList : Form
    {
        public SaleList()
        {
            InitializeComponent();
        }

        private void SaleList_Load(object sender, EventArgs e)
        {
                comboBoxSelectQuery.SelectedIndex = 0;
                LoadSettingCalendar();
            
        }

      

        private void BtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBoxSelectQuery_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxSelectQuery.SelectedIndex == 3)
            {
                
                BeginDate.Enabled = true;
                EndDate.Enabled = true;

            }
            else{

             
                BeginDate.Enabled = false;
                EndDate.Enabled = false;
            }
        }

        private void BtnPreview_Click(object sender, EventArgs e)
        {
            DSForPrint.Tables["TPropertiesFactor"].Clear();
        
            object result = Jacksonsoft.WaitWindow.Show(DoWork,null,comboBoxSelectQuery.SelectedIndex.ToString());
            
            DSForPrint.Tables.Remove("Information");
            DSForPrint.Tables.Add(Program.FC.DSFactor.Tables["Information"].Copy());
            stiReport1.Load(Application.StartupPath + "\\SaleList.mrt");
            stiReport1.RegData(DSForPrint);
            stiReport1.Show();

        }


        private string[] Split_String(string strShamsi)
        {
            string[] split = strShamsi.Split(new Char[] { '/', ' ' });
            return split;
        }



        private bool Compare_Two_Date(string[] split , int selectIndex)
        {
            PersianCalendar p = new PersianCalendar();
            int Year =Int32.Parse(split[0].ToString());
            int Mon = Int32.Parse(split[1].ToString());
            int Day = Int32.Parse(split[2].ToString());


            string[] strmiladi =Split_String(PersianDate.Miladi2Shamsi(DateTime.Now));

            int YearToday = Int32.Parse(strmiladi[0].ToString());
            int MonToday = Int32.Parse(strmiladi[1].ToString());
            int DayToday = Int32.Parse(strmiladi[2].ToString());




            string[] Begin = Split_String(BeginDate.SelectedDateInStringPersian);
            string[] End = Split_String(EndDate.SelectedDateInStringPersian);

            int YearBegin = Int32.Parse(Begin[0].ToString());
            int MonBegin = Int32.Parse(Begin[1].ToString());
            int DayBegin = Int32.Parse(Begin[2].ToString());

            int YearEnd = Int32.Parse(End[0].ToString());
            int MonEnd = Int32.Parse(End[1].ToString());
            int DayEnd = Int32.Parse(End[2].ToString());

            DateTime DT = p.ToDateTime(Year, Mon, Day, 0, 0, 0, 0);
            DateTime DTBegin = p.ToDateTime(YearBegin, MonBegin, DayBegin, 0, 0, 0, 0);
            DateTime DTEnd = p.ToDateTime(YearEnd, MonEnd, DayEnd, 0, 0, 0, 0);

            if (selectIndex == 3)
            {
                if ((DateTime.Compare(DT, DTBegin) >= 0) && (DateTime.Compare(DT, DTEnd) <= 0))
                {
                    return true;
                }
                return false;
            }

            if (selectIndex == 0)
            {
                if ((DateTime.Compare(DT.Date,DateTime.Now.Date) == 0))
                {
                    return true;
                }
                return false;
            }

            if (selectIndex == 1)
            {
                if (YearToday == Year && MonToday == Mon )
                {
                    return true;
                }
                return false;
            }

            if (selectIndex == 2)
            {
                if (YearToday == Year)
                {
                    return true;
                }
                return false;
            }

            return false;
        }


        private void DoWork(object sender, Jacksonsoft.WaitWindowEventArgs e )
        {
            PersianCalendar p = new PersianCalendar();

            for (int i = 0; i <= Program.FC.DSFactor.Tables["TPropertiesFactor"].Rows.Count - 1; i++)
            {
                if (Compare_Two_Date(Split_String(Program.FC.DSFactor.Tables["TPropertiesFactor"].Rows[i]["Date"].ToString()), Int32.Parse(e.Arguments[0].ToString())))
                {
                    DSForPrint.Tables["TPropertiesFactor"].ImportRow(Program.FC.DSFactor.Tables["TPropertiesFactor"].Rows[i]);
                }
            }

        }

        private void LoadSettingCalendar()
        {

            BeginDate.CalendarDaysBackColor = Properties.Settings.Default.DaysBGColor;
            BeginDate.CalendarDaysForeColor = Properties.Settings.Default.DaysColor;
            BeginDate.CalendarHolidayForeColor = Properties.Settings.Default.HolidayDaysColor;
            BeginDate.CalendarTodayBackColor = Properties.Settings.Default.TodayDateBGColor;
            BeginDate.CalendarTitleBackColor = Properties.Settings.Default.YearMonBGColor;
            BeginDate.CalendarTodayForeColor = Properties.Settings.Default.TodayDateColor;
            BeginDate.CalendarWeekDaysForeColor = Properties.Settings.Default.WeekColor;
            BeginDate.CalendarWeekDaysBackColor = Properties.Settings.Default.WeekBGColor;
            BeginDate.CalendarTrailingForeColor = Properties.Settings.Default.BeforeAfterMonDayColor;
            BeginDate.CalendarBorderColor = Properties.Settings.Default.BorderColor;
            BeginDate.CalendarDaysFont = Properties.Settings.Default.DaysFont;
            BeginDate.CalendarTodayFont = Properties.Settings.Default.TodayDateFont;
            BeginDate.CalendarShowToday = Properties.Settings.Default.ShowTodayDate;
            BeginDate.CalendarShowTrailing = Properties.Settings.Default.ShowBeforeAfterMonDay;

            EndDate.CalendarDaysBackColor = Properties.Settings.Default.DaysBGColor;
            EndDate.CalendarDaysForeColor = Properties.Settings.Default.DaysColor;
            EndDate.CalendarHolidayForeColor = Properties.Settings.Default.HolidayDaysColor;
            EndDate.CalendarTodayBackColor = Properties.Settings.Default.TodayDateBGColor;
            EndDate.CalendarTitleBackColor = Properties.Settings.Default.YearMonBGColor;
            EndDate.CalendarTodayForeColor = Properties.Settings.Default.TodayDateColor;
            EndDate.CalendarWeekDaysForeColor = Properties.Settings.Default.WeekColor;
            EndDate.CalendarWeekDaysBackColor = Properties.Settings.Default.WeekBGColor;
            EndDate.CalendarTrailingForeColor = Properties.Settings.Default.BeforeAfterMonDayColor;
            EndDate.CalendarBorderColor = Properties.Settings.Default.BorderColor;
            EndDate.CalendarDaysFont = Properties.Settings.Default.DaysFont;
            EndDate.CalendarTodayFont = Properties.Settings.Default.TodayDateFont;
            EndDate.CalendarShowToday = Properties.Settings.Default.ShowTodayDate;
            EndDate.CalendarShowTrailing = Properties.Settings.Default.ShowBeforeAfterMonDay;


        }
    }
}
