using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using CheckArticlesForSpam.Models;
using ReactiveUI;
using Tmds.DBus.Protocol;

namespace CheckArticlesForSpam.ViewModels
{
    public class ProfileUserViewModel : ReactiveObject
    {
        string phone;
        string login;
        string password;
        string userName;
        User currentUser;
        MlgTeamContext context;
        string message;
        char? passwordChar = '*';

        public ProfileUserViewModel(User currentUser, MlgTeamContext context)
        {
            this.context = context;
            this.currentUser = currentUser;
            this.phone = currentUser.Phone;
            this.login = currentUser.Login;
            this.password = currentUser.Password;
            this.UserName = currentUser.Username;
        }
        public string Message { get => message; set => this.RaiseAndSetIfChanged(ref message, value); }
        public User CurrentUser { get => currentUser; set => currentUser = value; }
        public string Phone { get => phone; set => this.RaiseAndSetIfChanged(ref phone, value); }
        public string Login { get => login; set => this.RaiseAndSetIfChanged(ref login, value); }
        public string Password { get => password; set => this.RaiseAndSetIfChanged(ref password, value); }
        public string UserName { get => userName; set => this.RaiseAndSetIfChanged(ref userName, value); }
        public char? PasswordChar { get => passwordChar; set => this.RaiseAndSetIfChanged(ref passwordChar, value); }


        public User SaveChanges()
        {
            if (string.IsNullOrEmpty(Login) || string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(Password) || string.IsNullOrEmpty(Phone))
            {
                Message = "Заполните все поля";
                return null;
            }
            else if (!Regex.IsMatch(Phone, @"^(?:\+7|8)\d{10}$"))
            {
                Message = "Введите корректный телефон";
                return null;
            }
            else if (!Regex.IsMatch(Password, @"\S{6,}"))
            {
                Message = "Ваш пароль не должен быть \nменьше 6 символов";
                return null;
            }
            else if (!Regex.IsMatch(Password, @"\w*[0-9]\w*"))
            {
                Message = "У вашего пароля нет цифры";
                return null;
            }
            else if (Login == CurrentUser.Login)
            {
                return EditUserData();
            }
            else if (context.Users.FirstOrDefault(x => x.Login == Login) != null)
            {
                Message = "Такой пользователь существует\nВведите другой логин";
                return null;
            }
            else
            {
                return EditUserData();
            }
        }
        private User EditUserData()
        {
            CurrentUser.Login = Login;
            CurrentUser.Password = Password;
            CurrentUser.Username = UserName;
            CurrentUser.Phone = Phone;
            context.SaveChanges();
            return CurrentUser;
        }
    }
}