using System;
using System.Collections.Generic;
using System.Linq;
using pr13.Models;
using Microsoft.EntityFrameworkCore;
using ReactiveUI;

namespace pr13.ViewModels
{
    public class CoursesViewModel : ReactiveObject
    {
        public static PostgresContext DB = new PostgresContext();
        private List<Course>? courses;
        public List<Course>? Courses { get => courses; set => this.RaiseAndSetIfChanged(ref courses, value); }

        public CoursesViewModel()
        {
            Courses = DB.Courses.Include(x => x.TeachersCourses).ToList();
        }



        public static List<string> OrderNameOrHours => new List<string>() { "Без сортировки", "По имени А-Я", "По имени Я-А", "По количеству часов (по возрастанию)", "По количеству часов (по убыванию)" };

        private string selectedNameOrHour = OrderNameOrHours[0];

        public string SelectedNameOrHour
        {
            get => selectedNameOrHour;
            set
            {
                selectedNameOrHour = value;
                Filter();
            }
        }

        public static List<string> FilterByHours => new List<string>() { "Без фильтра", "50 часов или меньше", "от 50 до 100 часов", "более 100 часов" };

        private string selectedFilter = FilterByHours[0];

        public string SelectedFilter
        {
            get => selectedFilter;
            set
            {
                selectedFilter = value;
                Filter();
            }
        }

        private string nameOrHoursFind;

        public string NameOrHoursFind
        {
            get => nameOrHoursFind;
            set
            {
                nameOrHoursFind = value;
                Filter();
            }
        }

        public void Filter()
        {
            Courses = DB.Courses.Include(x => x.TeachersCourses).ToList();

            if (SelectedNameOrHour != "Без сортировки")
            {
                switch (SelectedNameOrHour)
                {
                    case "По имени А-Я":
                        Courses = Courses.OrderBy(x => x.Course1).ToList();
                        break;
                    case "По имени Я-А":
                        Courses = Courses.OrderByDescending(x => x.Course1).ToList();
                        break;
                    case "По количеству часов (по возрастанию)":
                        Courses = Courses.OrderBy(x => x.HoursCount).ToList();
                        break;
                    case "По количеству часов (по убыванию)":
                        Courses = Courses.OrderByDescending(x => x.HoursCount).ToList();
                        break;
                }
            }

            if (SelectedFilter != "Без фильтра")
            {
                switch (SelectedNameOrHour)
                {
                    case "50 часов или меньше":
                        Courses = Courses.Where(x => x.HoursCount <= 50).ToList();
                        break;
                    case "от 50 до 100 часов":
                        Courses = Courses.Where(x => x.HoursCount > 50 && x.HoursCount <= 100).ToList();
                        break;
                    case "более 100 часов":
                        Courses = Courses.Where(x => x.HoursCount > 100).ToList();
                        break;
                }
            }

            if (NameOrHoursFind != null && NameOrHoursFind != "")
            {
                Courses = Courses.Where(x => x.Course1.ToLower().Contains(NameOrHoursFind.ToLower()) || NameOrHoursFind == Convert.ToString(x.HoursCount)).ToList();
            }
        }
    }
}