using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Shapes; using Avalonia.Media; using Avalonia.Threading; using avaloniaPr.Models; using avaloniaPr.Views; using ReactiveUI; using System; using System.Threading; namespace avaloniaPr.ViewModels { public class MainWindowViewModel : ViewModelBase { UserControl us = new Autorization(); Canvas canvas; string login = ""; string password = ""; bool enbld = true; string check; bool vsbltButton = true; bool vsbltCaptcha = false; bool vsbltWaiting = false; string capcha = ""; string waiting = "Ошибка. Попробуйте снова через 10 секунд"; DispatcherTimer DT = new DispatcherTimer(); public UserControl US { get => us; set => this.RaiseAndSetIfChanged(ref us, value); } public AutorizationViewModel AutorizationVM { get => autorizationVM; set => autorizationVM = value; } AutorizationViewModel autorizationVM = new AutorizationViewModel(); public CalculatorViewModel CalculatorVM { get => calculatorVM; set => calculatorVM = value; } CalculatorViewModel calculatorVM = new CalculatorViewModel(); public string LoginUs { get => login; set => this.RaiseAndSetIfChanged(ref login, value); } public string PasswordUs { get => password; set => this.RaiseAndSetIfChanged(ref password, value); } public bool VisibilityButton { get => vsbltButton; set => this.RaiseAndSetIfChanged(ref vsbltButton, value); } public bool VisibilityCaptcha { get => vsbltCaptcha; set => this.RaiseAndSetIfChanged(ref vsbltCaptcha, value); } public bool VisibilityWaiting { get => vsbltWaiting; set => this.RaiseAndSetIfChanged(ref vsbltWaiting, value); } public string Capcha { get => capcha; set => this.RaiseAndSetIfChanged(ref capcha, value); } public string Waiting { get => waiting; set => this.RaiseAndSetIfChanged(ref waiting, value); } public void AutorizationButton() { if (LoginUs != Models.UserData.UserLogin || PasswordUs != Models.UserData.UserPassword) { VisibilityCaptcha = true; VisibilityButton = false; CreateCaptcha(); } else { US = new Calculator(); } } public Canvas CanvasText { get => canvas; set => this.RaiseAndSetIfChanged(ref canvas, value); } public void CreateCaptcha() // метод для создания Canvas, внутри которого рандомно генерируются линии { LoginUs = ""; PasswordUs = ""; SolidColorBrush color = new SolidColorBrush(Color.FromRgb(155, 155, 155)); Canvas canvas = new Canvas() { Width = 210, Height = 100, Background = color }; Random rnd = new Random(); TextBlock TB1 = new TextBlock() { Text = Convert.ToString(rnd.Next(0, 9)), FontSize = 30, Foreground = Brushes.Black, Padding = new Thickness(20) }; TextBlock TB2 = new TextBlock() { Text = Convert.ToString(Convert.ToChar(rnd.Next(97, 123))), FontSize = 30, Foreground = Brushes.Black, Padding = new Thickness(40) }; TextBlock TB3 = new TextBlock() { Text = Convert.ToString(rnd.Next(0, 20)), FontSize = 30, Foreground = Brushes.Black, Padding = new Thickness(60, 10), }; TextBlock TB4 = new TextBlock() { Text = Convert.ToString(Convert.ToChar(rnd.Next(97, 123))), FontSize = 30, Foreground = Brushes.Black, Padding = new Thickness(100, 2) }; TextBlock TB5 = new TextBlock() { Text = Convert.ToString(rnd.Next(0, 9)), FontSize = 30, Foreground = Brushes.Black, Padding = new Thickness(130, 10) }; TextBlock TB6 = new TextBlock() { Text = Convert.ToString(Convert.ToChar(rnd.Next(65, 91))), FontSize = 30, Foreground = Brushes.Black, Padding = new Thickness(150, 1) }; TextBlock TB7 = new TextBlock() { Text = Convert.ToString(Convert.ToChar(rnd.Next(97, 123))), FontSize = 30, Foreground = Brushes.Black, Padding = new Thickness(170, -3) }; canvas.Children.Add(TB1); canvas.Children.Add(TB2); canvas.Children.Add(TB3); canvas.Children.Add(TB4); canvas.Children.Add(TB5); canvas.Children.Add(TB6); canvas.Children.Add(TB7); check = TB1.Text + TB2.Text + TB3.Text + TB4.Text + TB5.Text + TB6.Text + TB7.Text; for (int i = 0; i <= 10; i++) { Line line = new Line() { StartPoint = new Point(rnd.Next(220), rnd.Next(100)), EndPoint = new Point(rnd.Next(220), rnd.Next(100)), Stroke = new SolidColorBrush(Color.FromRgb(Convert.ToByte(rnd.Next(0, 155)), Convert.ToByte(rnd.Next(0, 155)), Convert.ToByte(rnd.Next(0, 155)))), StrokeThickness = 2 }; canvas.Children.Add(line); } CanvasText = canvas; } public bool Enabled { get => enbld; set => this.RaiseAndSetIfChanged(ref enbld, value); } public void CommandCaptcha() { if (Capcha != check || LoginUs != Models.UserData.UserLogin || PasswordUs != Models.UserData.UserPassword) { Enabled = false; VisibilityCaptcha = false; VisibilityWaiting = true; LoginUs = ""; PasswordUs = ""; Capcha = ""; DT.Interval = new TimeSpan(0, 0, 10); DT.Tick += new EventHandler(TimerEnd); DT.Start(); CreateCaptcha(); } else { AutorizationButton(); } } public void TimerEnd(object sender, EventArgs e) { Enabled = true; VisibilityWaiting = false; CreateCaptcha(); VisibilityCaptcha = true; DT.Stop(); } } }