using Exam.Classes;
using Exam.Pages;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace Exam
{
    /// <summary>
    /// Класс личного рейтинга пользователя
    /// </summary>
    public partial class UserActivity : Page
    {
        List<string> users_string = new List<string>();
        List<usersmetrics> um;
        List<users> all;
        public UserActivity(users user)
        {
            InitializeComponent();
            if (DBContext.Connect.posts.FirstOrDefault(x => x.id == user.id_post).id_role == 1)
            {
                all = DBContext.Connect.users.Where(x => x.id_post != 5 && x.id_post != 7 && x.id_post != 8).ToList();
                users_string.AddRange(all.Select(x => x.surname + " " + x.firstname + " " + x.patronymic));
                CBUsers.Visibility = Visibility.Visible;
                CBUsers.ItemsSource = users_string;
            }
            um = DBContext.Connect.usersmetrics.Where(x => x.id_user == user.id).ToList();
            LVUsers.ItemsSource = um;
            TBAllPoints.Text = "Всего: " + FindAllPoints();
            TBPosition.Text = "Позиция в рейтинге: " + FindPosition(user);
        }

        public double FindAllPoints()
        {
            return Convert.ToDouble(um.Sum(x=>x.coefficient));
        }

        public int FindPosition(users current_user)
        {
            return Methods.FindRating().FirstOrDefault(x=>x.id_user == current_user.id).position;
        }

        private void User_Changed(object sender, SelectionChangedEventArgs e)
        {
            users selected_user = all[CBUsers.SelectedIndex];
            um = DBContext.Connect.usersmetrics.Where(x => x.id_user == selected_user.id).ToList();
            LVUsers.ItemsSource = um;
            TBAllPoints.Text = "Всего: " + FindAllPoints();
            TBPosition.Text = "Позиция в рейтинге: " + FindPosition(selected_user);
        }
    }
}
