using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WPFYifis2
{
    class Presenter
    {
        private static Presenter singletone;

        public static Presenter Singleton { get { if (singletone == null) { singletone = new Presenter(); return singletone; } else { return singletone; } } }

        private User loggedUser;

        public User LoggedUser { get => loggedUser; }

        private gr606_alvviContext model;

        public gr606_alvviContext Model { get => model; }

        public Presenter()
        {
            model = new gr606_alvviContext();

            MessageBox.Show(model.Lessons.First().ToString());
        }
        public bool Login(string login, string password)
        {
            User user = model.Users.Single(el => el.Login == login && el.Password == password);

            if (user == null)
            {
                return false;
            }

            loggedUser = user;
            return true;
        }
            
        public Parachute GetParachute()
        {
            try
            {
                return model.Parachutes.Single(el => el.IdParachute == loggedUser.IdParachute);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public Jump GetJump()
        {
            try
            {
                return model.Jumps.Single(el => el.IdJump == model.JumpUserLists.Single(el => el.IdUser == loggedUser.IdUser).IdJump);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public User GetInstructor() 
        {
            try
            {
                return model.Users.Single(el => el.IdUser == GetJump().IdInstructor);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public string GetParachuteType()
        {
            try
            {
                return model.TypeParachutes.Single(el => el.IdTypeParachute == GetParachute().IdTypeParachute).TypeParachute1;
            } 
            catch (Exception ex)
            {
                return "";
            }
        }
        public Lesson GetLesson()
        {
            try
            {
                return model.Lessons.Single(el => el.IdUserList == model.UserLists.Single(el => el.IdUser == loggedUser.IdUser).IdLesson);
            }
            catch(Exception ex)
            {
                MessageBox.Show("ex");
                return null;
            }
        }
        public TypeLesson GetTypeLesson()
        {
            try
            {
                return model.TypeLessons.Single(el => el.IdTypeLessons == GetLesson().IdTypeLessons);
            }
            catch(Exception ex)
            {
                return null;
            }
        }
    }
}
