using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Disposables;
using MsBox.Avalonia;
using ReactiveUI;
using TourSK2.Models;

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

        Hotel hotelData;
        public Hotel HotelData { get => hotelData; set => hotelData = value; }
        public bool flag = false;
        /// <summary>
		/// Конструктор для инициализации при редактировании существующей записи
		/// </summary>
        public PageAddRedactViewModel(int id)
        {
            HotelData = DB.Hotels.Find(id);
            CountriesList = DB.Countries.Select(x => x.Name).ToList();
            SelectedStars = HotelData.CountOfStars;
            SelectedCountry = HotelData.CountryName;
            EnteredName = HotelData.Name;
            EnteredDescription = HotelData.Description;
            flag = true;
        }
        /// <summary>
		/// Конструктор для инициализации при добавлении нового отеля
		/// </summary>
        public PageAddRedactViewModel()
        {
            SelectedStars = StarsHotel[0];
            CountriesList = DB.Countries.Select(x => x.Name).ToList();
            HotelData = new Hotel();
        }

        private List<int> _starsHotel = new List<int>() { 0, 1, 2, 3, 4, 5 };
        public List<int> StarsHotel { get => _starsHotel; set => this.RaiseAndSetIfChanged(ref _starsHotel, value); }
        private int _selectedStars;
        public int SelectedStars
        {
            get
            {
                return _selectedStars;
            }
            set
            {
                this.RaiseAndSetIfChanged(ref _selectedStars, value);
            }
        }

        private List<string> _countriesList;
        public List<string> CountriesList
        {
            get => _countriesList;
            set => this.RaiseAndSetIfChanged(ref _countriesList, value);

        }

        private string _selectedCountry;
        public string SelectedCountry
        {
            get
            {
                return _selectedCountry;
            }
            set
            {
                this.RaiseAndSetIfChanged(ref _selectedCountry, value);
            }
        }

        private string _enteredName = "";
        public string EnteredName
        {
            get
            {
                return _enteredName;

            }
            set => this.RaiseAndSetIfChanged(ref _enteredName, value);
        }

        private string? _enteredDesciption = "";
        public string? EnteredDescription
        {
            get
            {
                return _enteredDesciption;

            }
            set => this.RaiseAndSetIfChanged(ref _enteredDesciption, value);
        }

        public void FixChanges()
        {
            if (CheckOnEmpty())
            {
                HotelData.Name = EnteredName;
                HotelData.Description = EnteredDescription;
                HotelData.CountOfStars = SelectedStars;
                HotelData.CountryCode = DB.Countries.Where(x => x.Name == SelectedCountry).Select(x => x.Code).First();
                if (flag)
                {
                    DB.SaveChanges();
                }
                else
                {
                    DB.Hotels.Add(HotelData);
                    DB.SaveChanges();
                }
                var box = MessageBoxManager.GetMessageBoxStandard("Успех", "Отель или его изменения сохранены");
                box.ShowAsync();
            }
            else
            {
                var box = MessageBoxManager.GetMessageBoxStandard("Провал", "Заполните все поля");
                box.ShowAsync();
            }
        }

        public bool CheckOnEmpty()
        {
            if (EnteredName != null && EnteredName != "" && EnteredDescription != null && EnteredDescription != "" &&
                SelectedCountry != null && SelectedCountry != "")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}