using Avalonia.Threading;
using AvaloniaApplicationTest.Models;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AvaloniaApplicationTest.ViewModels
{
    public class ShowHotelsPageViewModel : ReactiveObject
    {
        public List<Hotel> LBHotels
        {
            get => lBHotels;
            set => this.RaiseAndSetIfChanged(ref lBHotels, value);
        }

        public string HowManyShow
        {
            get
            {
                return GetShowed();
            }
            set => this.RaiseAndSetIfChanged(ref text1, value);
        }

        public string PageNumber
        {
            get
            {
                return GetPages();
            }
            set => this.RaiseAndSetIfChanged(ref text2, value);
        }
        
        public int UserCount
        {
            get => user_count;
            set  { 

                this.RaiseAndSetIfChanged(ref user_count, value);
                try
                {
                    all_page_number = Convert.ToInt32(Math.Round((double)MainWindowViewModel.myConnection.Hotels.Count() / user_count, MidpointRounding.ToPositiveInfinity));
                    LBHotels = MainWindowViewModel.myConnection.Hotels.Take(value).ToList();
                }
                catch { }
            }
            }

        public bool CanBack
        {
            get => visibility_b;
            set => this.RaiseAndSetIfChanged(ref visibility_b, value);
        }

        public bool CanNext
        {
            get => visibility_n;
            set => this.RaiseAndSetIfChanged(ref visibility_n, value);
        }

        int page_number = 1;
        int all_page_number = Convert.ToInt32(Math.Round((double)MainWindowViewModel.myConnection.Hotels.Count()/ 10, MidpointRounding.ToPositiveInfinity));
        
        int all_count = MainWindowViewModel.myConnection.Hotels.Count();
        List<Hotel> lBHotels = MainWindowViewModel.myConnection.Hotels.Take(10).ToList();
        bool visibility_b = false;
        bool visibility_n = true;
        string text1 = "";
        string text2 = "";

        int user_count = 10;
        int count_showed = 10;

        string GetShowed()
        {
            string text = count_showed + " / " + all_count + " отелей";
            return text;
        }
        string GetPages()
        {
            all_page_number = Convert.ToInt32(Math.Round((double)MainWindowViewModel.myConnection.Hotels.Count() / user_count, MidpointRounding.ToPositiveInfinity));
            string text = page_number + " / " + all_page_number;
            return text;
        }

        public void Next()
        {
            CanBack = true;
            int count = (all_count - count_showed);
            if (count<= user_count)
            {
                CanNext = false;
            }
            else
            {
                count = user_count;
            }
            page_number++;
            LBHotels = MainWindowViewModel.myConnection.Hotels.Skip(count_showed).Take(count).ToList();
            count_showed += count;
            HowManyShow = GetShowed();
            PageNumber = GetPages();
        }

        public void Back()
        {
            CanNext = true;
            if (page_number == 2)
            {
                CanBack = false;
            }
            int count = user_count;
            if (page_number == all_page_number)
            {
                count = all_count - (page_number - 1)* user_count + user_count;
            }
            page_number--;
            count_showed = page_number * user_count;
            HowManyShow = GetShowed();
            PageNumber = GetPages();
            LBHotels = MainWindowViewModel.myConnection.Hotels.Skip(count_showed-count).Take(user_count).ToList();
        }
    }
}
