using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MD.PersianDateTime.Standard;
using Rg.Plugins.Popup.Services;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace CafeTerrace.Views.DialogBox
{
    public class PersianDatePickerDateItem
    {
        public bool IsEnable { get; set; }
        public DateTime EnDateTime { get; set; }
        public PersianDateTime PrDateTime { get; set; }
        public string PrDateTimeDate { get; set; }
    }
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class PersianDatePicker : ContentView
    {
        public ObservableCollection<List<PersianDatePickerDateItem>> PersianDateTimes = new ObservableCollection<List<PersianDatePickerDateItem>>();
        public Button SelectedButton = null;
        public DateTime CurrentDateTime = DateTime.Today;
        public event EventHandler DateSelected;
        public PersianDatePicker()
        {
            InitializeComponent();
            init(CurrentDateTime);
            daysCollection.ItemsSource = PersianDateTimes;
        }

        private void init(DateTime dateTime)
        {
            PersianDateTimes.Clear();
            PersianCalendar pc = new PersianCalendar();
            PersianDateTime thisMonthPersianStart = new PersianDateTime(pc.GetYear(dateTime), pc.GetMonth(dateTime), 1);
            PersianDateTime thisMonthPersianEnd;
            if (pc.GetMonth(dateTime) > 11)
                thisMonthPersianEnd = new PersianDateTime(pc.GetYear(dateTime)+1, 1, 1);
            else
                thisMonthPersianEnd = new PersianDateTime(pc.GetYear(dateTime), pc.GetMonth(dateTime)+1, 1);
            monthLable.Text = thisMonthPersianStart.ToString("MMMM");
            yearLable.Text = thisMonthPersianStart.ToString("yyyy");


            DateTime startDate = new DateTime(thisMonthPersianStart.Year, thisMonthPersianStart.Month, thisMonthPersianStart.Day, pc);
            DateTime endDate = new DateTime(thisMonthPersianEnd.Year, thisMonthPersianEnd.Month, thisMonthPersianEnd.Day, pc);

            var numDays = (int)((endDate - startDate).TotalDays);
            List<DateTime> myDates = Enumerable
                .Range(0, numDays)
                .Select(x => startDate.AddDays(x))
                .ToList();
            int j = 0;
            PersianDateTimes.Add(new List<PersianDatePickerDateItem>());

            int countFree = 0;
            switch ((new PersianDateTime(myDates[0])).PersianDayOfWeek)
            {
                case PersianDayOfWeek.Sunday:
                    countFree = 1;
                    break;
                case PersianDayOfWeek.Monday:
                    countFree = 2;
                    break;
                case PersianDayOfWeek.Tuesday:
                    countFree = 3;
                    break;
                case PersianDayOfWeek.Wednesday:
                    countFree = 4;
                    break;
                case PersianDayOfWeek.Thursday:
                    countFree = 5;
                    break;
                case PersianDayOfWeek.Friday:
                    countFree = 6;
                    break;
            }

            for (int i = 0; i < countFree; i++)
            {
                PersianDateTimes[j].Add(new PersianDatePickerDateItem
                {
                    IsEnable = false,
                    EnDateTime = DateTime.Now,
                    PrDateTime = new PersianDateTime(DateTime.Now),
                    PrDateTimeDate = string.Empty
                });
            }

            for (int i = countFree; i < myDates.Count + countFree; i++)
            {
                if (i!=0 && i % 7 == 0)
                {
                    PersianDateTimes.Add(new List<PersianDatePickerDateItem>());
                    j++;
                }
                PersianDateTimes[j].Add(new PersianDatePickerDateItem
                {
                    IsEnable = true,
                    EnDateTime = myDates[i- countFree],
                    PrDateTime = new PersianDateTime(myDates[i-countFree]),
                    PrDateTimeDate = (new PersianDateTime(myDates[i- countFree])).ToString("dd")
                });
            }

        }
        private void YearLable_OnTextChanged(object sender, TextChangedEventArgs e)
        {
            if (yearLable.Text.Length >= 4)
            {
                int year = int.Parse(yearLable.Text);
                if (year > 1200 && year < 3000)
                {

                    PersianCalendar pc = new PersianCalendar();
                    DateTime enYear = new DateTime(year, 1, 1, pc);
                    CurrentDateTime = new DateTime(enYear.Year,CurrentDateTime.Month,CurrentDateTime.Day);
                    init(CurrentDateTime);
                }
            }
        }
        private void DatePickerButton_OnClicked(object sender, EventArgs e)
        {
            var btn = (Button) sender;
            if (SelectedButton != null)
            {
                SelectedButton.BackgroundColor = Color.Transparent;
                SelectedButton.TextColor = Color.DimGray;
            }

            btn.BackgroundColor = (Color.Accent);
            btn.TextColor = (Color) Application.Current.Resources["PrimaryColor"];
            CurrentDateTime = (btn.BindingContext as PersianDatePickerDateItem).EnDateTime;
            SelectedButton = btn;
        }

        private void NextMonthButton_OnClicked(object sender, EventArgs e)
        {
            CurrentDateTime = CurrentDateTime.AddMonths(+1);
            init(CurrentDateTime);
        }

        private void PervMonthButton_OnClicked(object sender, EventArgs e)
        {
            CurrentDateTime = CurrentDateTime.AddMonths(-1);
            init(CurrentDateTime);
        }

        private void PervYearButton_OnClicked(object sender, EventArgs e)
        {
            CurrentDateTime = CurrentDateTime.AddYears(-1);
            init(CurrentDateTime);
        }

        private void NextYearButton_OnClicked(object sender, EventArgs e)
        {
            CurrentDateTime = CurrentDateTime.AddYears(+1);
            init(CurrentDateTime);
        }

        private void SubmitButton_OnClicked(object sender, EventArgs e)
        {
            DateSelected?.Invoke(CurrentDateTime,new EventArgs());
            PopupNavigation.Instance.PopAsync();
        }

        private void CloseButton_OnClicked(object sender, EventArgs e)
        {
            PopupNavigation.Instance.PopAsync();
        }


        private void YearLable_OnFocused(object sender, FocusEventArgs e)
        {
            yearLable.Text = "";
        }
    }
}