using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using pr13.Models;
using Microsoft.EntityFrameworkCore;
using MsBox.Avalonia;
using ReactiveUI;

namespace pr13.ViewModels
{
    public class PersonalCabinetViewModel : ReactiveObject
    {
        PostgresContext DB = new PostgresContext();
        User? userData;
        public User? UserData { get => userData; set => userData = value; }
        public PersonalCabinetViewModel(int id)
        {
            UserData = DB.Users.Where(x => x.IdUser == id).Include(x => x.Teacher).ThenInclude(x => x.IdGenderNavigation).FirstOrDefault();
        }

        public string SelectedGender
        {
            get => UserData.Teacher.IdGenderNavigation.Gender1;
            set => UserData.Teacher.IdGender = DB.Genders.FirstOrDefault(x => x.Gender1 == value).Id;
        }

        public string DR
        {
            get => UserData.Teacher.Birthdate.ToString();
            set => UserData.Teacher.Birthdate = DateTime.Parse(value);
        }

        public List<string> Genders => DB.Genders.Select(x => x.Gender1).ToList();

        bool _enable = false;

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

        public void Checked()
        {
            if (!Enable)
            {
                Enable = true;
            }
            else
            {
                Enable = false;
            }
        }

        string _newPass;
        string _newPassRepeat;

        public string NewPass
        {
            get => _newPass;
            set => this.RaiseAndSetIfChanged(ref _newPass, value);
        }

        public string NewPassRepeat
        {
            get => _newPassRepeat;
            set => this.RaiseAndSetIfChanged(ref _newPassRepeat, value);
        }

        public void FixChanges()
        {
            if (!CheckOnEmpty() || !CheckPhone())
            {
                return;
            }
            if (NewPass == NewPassRepeat)
            {
                UserData.Password = NewPassRepeat;
                DB.SaveChanges();
                var box = MessageBoxManager.GetMessageBoxStandard("Изменения", "Изменения сохранены!");
                box.ShowAsync();
            }
            else
            {
                var box = MessageBoxManager.GetMessageBoxStandard("Изменения", "Пароль должен совпадать, изминения не сохранены!");
                box.ShowAsync();
            }
        }

        public bool CheckOnEmpty()
        {
            if (!Enable)
            {
                if (UserData.Teacher.Surname.ToString() == null || UserData.Teacher.Surname.ToString() == "" ||
                    UserData.Teacher.Name.ToString() == null || UserData.Teacher.Name.ToString() == "" ||
                    UserData.Teacher.Patronymic.ToString() == null || UserData.Teacher.Patronymic.ToString() == "" ||
                    UserData.Login.ToString() == null || UserData.Login.ToString() == "" ||
                    UserData.Teacher.Email.ToString() == null || UserData.Teacher.Email.ToString() == "" ||
                    UserData.Teacher.Phone.ToString() == null || UserData.Teacher.Phone.ToString() == "")
                {
                    var box = MessageBoxManager.GetMessageBoxStandard("Поля", "Поля не заполнены");
                    box.ShowAsync();
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                if (UserData.Teacher.Surname.ToString() == null || UserData.Teacher.Surname.ToString() == "" ||
                    UserData.Teacher.Name.ToString() == null || UserData.Teacher.Name.ToString() == "" ||
                    UserData.Teacher.Patronymic.ToString() == null || UserData.Teacher.Patronymic.ToString() == "" ||
                    UserData.Login.ToString() == null || UserData.Login.ToString() == "" ||
                    UserData.Teacher.Email.ToString() == null || UserData.Teacher.Email.ToString() == "" ||
                    UserData.Teacher.Phone.ToString() == null || UserData.Teacher.Phone.ToString() == "" ||
                    NewPass == null || NewPass == "" || NewPassRepeat == null || NewPassRepeat == "")
                {
                    var box = MessageBoxManager.GetMessageBoxStandard("Поля", "Поля не заполнены");
                    box.ShowAsync();
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }

        public bool CheckPhone()
        {
            if (UserData.Teacher.Phone.Length == 11)
            {
                Regex PhoneRegex = new Regex("^89\\d{9}$");
                if (PhoneRegex.IsMatch(UserData.Teacher.Phone))
                {
                    return true;
                }
                else
                {
                    var box = MessageBoxManager.GetMessageBoxStandard("Телефон", "Неверно введен телефон!");
                    box.ShowAsync();
                    return false;
                }
            }
            else if (UserData.Teacher.Phone.Length < 11)
            {
                var box = MessageBoxManager.GetMessageBoxStandard("Телефон", "Неверно введен телефон!");
                box.ShowAsync();
                return false;
            }
            else
            {
                string CorrectPhone = "";
                CorrectPhone = UserData.Teacher.Phone.Replace("(", "");
                CorrectPhone = CorrectPhone.Replace(")", "");
                CorrectPhone = CorrectPhone.Replace("-", "");
                CorrectPhone = CorrectPhone.Replace(" ", "");
                if (CorrectPhone.Length == 11)
                {
                    UserData.Teacher.Phone = CorrectPhone;
                    return true;
                }
                else
                {
                    var box = MessageBoxManager.GetMessageBoxStandard("Телефон", "Неверно введен телефон!");
                    box.ShowAsync();
                    return false;
                }
            }
        }
    }
}