using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using MsBox.Avalonia.Enums;
using MsBox.Avalonia;
using ReactiveUI;
using TourSK2.Models;

namespace TourSK2.ViewModels
{
	public class PageHotelsViewModel : ReactiveObject
	{
        UpSkakunovContext DB = new UpSkakunovContext();

        private List<Hotel>? _hotels;
        public List<Hotel>? Hotels { get => _hotels; set => this.RaiseAndSetIfChanged(ref _hotels, value); }

        private int countElements;
        public int CountElements
        {
            get => countElements;
            set
            {
                this.RaiseAndSetIfChanged(ref countElements, value);
                Hotels = DB.Hotels.ToList();
                AmountOfPages = Hotels.Count / countElements + 1;
                ToFirstPage();

            }
        }

        private int _amountOfPages;
        public int AmountOfPages
        {
            get => _amountOfPages;
            set
            {
                this.RaiseAndSetIfChanged(ref _amountOfPages, value);
            }
        }
        private int _page = 1;
        public int Page { get => _page; set => this.RaiseAndSetIfChanged(ref _page, value); }
        private bool _enableOrNotRight = true;
        public bool EnableOrNotRight { get => _enableOrNotRight; set => this.RaiseAndSetIfChanged(ref _enableOrNotRight, value); }
        private bool _enableOrNotLeft = false;
        public bool EnableOrNotLeft { get => _enableOrNotLeft; set => this.RaiseAndSetIfChanged(ref _enableOrNotLeft, value); }
        int flagElementCount = 0;

        private List<int> _selectCountOfElements = new List<int>() { 10, 15, 20, 25, 30 };
        public List<int> SelectedCountOfElements { get => _selectCountOfElements; set => this.RaiseAndSetIfChanged(ref _selectCountOfElements, value); }

        private List<Hotel> _selectedHotels;
        public List<Hotel> SelectedHotels
        {
            get => _selectedHotels;
            set => this.RaiseAndSetIfChanged(ref _selectedHotels, value);
        }
        /// <summary>
        /// по умолчанию выводятся первые 10 записей
        /// </summary>
        public PageHotelsViewModel()
        {
            Hotels = DB.Hotels.ToList();
            SelectedHotels = new List<Hotel>();
            CountElements = SelectedCountOfElements[0];
            Hotels = Hotels.Take(CountElements).ToList();

        }

        public void NextPage()
        {
            Page++;
            Hotels = DB.Hotels.ToList();
            Hotels = Hotels.Skip((Page - 1) * CountElements).ToList();
            flagElementCount = Hotels.Count();
            Hotels = Hotels.Take(CountElements).ToList();
            EnableOrNotLeft = true;
            if (flagElementCount <= CountElements)
            {
                EnableOrNotRight = false;
            }
            else
            {
                EnableOrNotRight = true;
            }
        }

        public void PrevPage()
        {
            Page--;
            Hotels = DB.Hotels.ToList();
            Hotels = Hotels.Skip((Page - 1) * CountElements).ToList();
            Hotels = Hotels.Take(CountElements).ToList();
            EnableOrNotRight = true;
            if (Page == 1)
            {
                EnableOrNotLeft = false;
            }
            else
            {
                EnableOrNotLeft = true;
            }
        }

        public void ToFirstPage()
        {
            Page = 1;
            Hotels = DB.Hotels.ToList();
            Hotels = Hotels.Take(CountElements).ToList();
            EnableOrNotLeft = false;
            EnableOrNotRight = true;
        }

        public void ToLastPage()
        {
            Hotels = DB.Hotels.ToList();
            Page = Hotels.Count / CountElements;
            Hotels = Hotels.Skip(Page * CountElements).ToList();
            Hotels = Hotels.Take(CountElements).ToList();
            Page++;
            EnableOrNotRight = false;
            EnableOrNotLeft = true;
        }
        public async void DeleteHotel()
        {
            if (SelectedHotels.Count > 0)
            {
                List<Hotel> forDelete = new List<Hotel>();
                List<Hotel> NotforDelete = new List<Hotel>();
                var box = MessageBoxManager.GetMessageBoxStandard("Удалить", "Вы уверены, что хотите удалить?", MsBox.Avalonia.Enums.ButtonEnum.YesNoCancel);
                var result = await box.ShowAsync();
                if (result == ButtonResult.Yes)
                {
                    List<HotelOfTour> ToursAndHotels;
                    foreach (Hotel h in SelectedHotels)
                    {
                        ToursAndHotels = DB.HotelOfTours.Where(x => x.HotelId == h.Id).ToList();
                        if (ToursAndHotels.Count > 0)
                        {
                            List<HotelOfTour> IsActual = ToursAndHotels.Where(x => x.Tour.IsActual[0] == true).ToList();
                            if (IsActual.Count > 0)
                            {
                                NotforDelete.Add(h);
                            }
                            else
                            {
                                forDelete.Add(h);
                            }
                        }
                        else
                        {
                            forDelete.Add(h);
                        }
                    }
                    if (NotforDelete.Count > 0)
                    {
                        box = MessageBoxManager.GetMessageBoxStandard("Ошибка", "Возникла ошибка при удалении, попробуйте снова");
                        box.ShowAsync();
                    }
                    DB.Hotels.RemoveRange(forDelete);
                    DB.SaveChanges();
                    ToFirstPage();
                }
            }
            else
            {
                var box = MessageBoxManager.GetMessageBoxStandard("Ошибка", "Чтобы удапить выберите один или несколько обьектов");
                box.ShowAsync();
            }
        }
    }
}
