using System;
using System.Collections.Generic;
using System.Linq;
using MsBox.Avalonia;
using MsBox.Avalonia.Enums;
using ReactiveUI;
using UPtur.Models;

namespace UPtur.ViewModels
{
    public class HotelsViewModel : ReactiveObject
    {
        UpPrytovContext DB = new UpPrytovContext();
        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 };//выбор кол-во страниц на экране
        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);
        }
        public HotelsViewModel()//вывод первых 10 записей
        {
            Hotels = DB.Hotels.ToList();
            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()//переход на самую 1 страницу
        {
            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;
        }

       
            

        
    }
}
