<commit_before>﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Jirabox.Common;
using Jirabox.Core.Contracts;
using Jirabox.Core.ExceptionExtension;
using Jirabox.Resources;
using System;
using System.Threading;

namespace Jirabox.ViewModel
{
    public class LoginViewModel : ViewModelBase
    {
        private readonly INavigationService navigationService;
        private readonly IDialogService dialogService;
        private readonly IJiraService jiraService;

        private CancellationTokenSource cancellationTokenSource = null;
        private string serverUrl;
        private string userName;        
        private string password;
        private bool isDataLoaded;
        private bool isRememberMe;
        private bool loginButtonEnabled;

        public RelayCommand LoginCommand { get; private set; }
        public RelayCommand AboutCommand { get; private set; }   

        public string ServerUrl
        {
            get { return serverUrl; }
            set 
            {
                if (serverUrl != value)
                {
                    serverUrl = value;
                    App.ServerUrl = serverUrl;
                    RaisePropertyChanged(() => ServerUrl);
                }                
            }
        }

        public bool LoginButtonEnabled
        {
            get
            {
                return loginButtonEnabled;
            }
            set
            {
                loginButtonEnabled = value;
                RaisePropertyChanged(() => LoginButtonEnabled);
                LoginCommand.RaiseCanExecuteChanged();
            }
        }

        public string UserName
        {
            get { return userName; }
            set 
            {
                if (userName != value)
                {
                    userName = value;
                    App.UserName = userName;
                    RaisePropertyChanged(() => UserName);
                }
                
            }
        }

        public string Password
        {
            get { return password; }
            set
            {
                if (password != value)
                {
                    password = value;
                    App.Password = password;
                    RaisePropertyChanged(() => Password);
                }

            }
        }        

        public bool IsDataLoaded
        {
            get { return isDataLoaded; }
            set 
            {
                if (value != isDataLoaded)
                {
                    isDataLoaded = value;
                    RaisePropertyChanged(() => IsDataLoaded);
                }
            }
        }     
        
        public LoginViewModel(INavigationService navigationService, IDialogService dialogService, IJiraService jiraService)
        {            
            this.navigationService = navigationService;
            this.dialogService = dialogService;
            this.jiraService = jiraService;         
   
            InitializeData();
        }

        public async void Login()
        {
            IsDataLoaded = false;
            LoginButtonEnabled = false;
            if (!IsInputsValid())
            {
                IsDataLoaded = true;
                dialogService.ShowDialog(AppResources.UnauthorizedMessage, AppResources.LoginFailedMessage);
                return;
            }

            if (!ValidateUrl(ServerUrl))
            {
                IsDataLoaded = true;
                dialogService.ShowDialog(AppResources.InvalidServerUrlMessage, AppResources.LoginFailedMessage);                
                return;
            }
            try
            {
                //Create new cancellation token source
                cancellationTokenSource = new CancellationTokenSource();

                //Set base url for rest api
                App.BaseUrl = string.Format("{0}/rest/api/latest/", App.ServerUrl);
                var isLoginSuccess = await jiraService.LoginAsync(ServerUrl, UserName, Password, cancellationTokenSource);
                if (isLoginSuccess)
                {
                    App.IsLoggedIn = true;
                    StorageHelper.SaveUserCredential(ServerUrl, UserName, Password);                        
                    navigationService.Navigate<ProjectListViewModel>();                    
                }
            }         
            catch (HttpRequestStatusCodeException exception)
            {
                if (exception.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                {
                    dialogService.ShowDialog(AppResources.UnauthorizedMessage, AppResources.LoginFailedMessage);
                }
                else if (exception.StatusCode == System.Net.HttpStatusCode.Forbidden)
                {
                    dialogService.ShowDialog(AppResources.ForbiddenMessage, AppResources.LoginFailedMessage);
                }
                else<commit_msg>Login button enabling problem fixed.<commit_after>﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Jirabox.Common;
using Jirabox.Core.Contracts;
using Jirabox.Core.ExceptionExtension;
using Jirabox.Resources;
using System;
using System.Threading;

namespace Jirabox.ViewModel
{
    public class LoginViewModel : ViewModelBase
    {
        private readonly INavigationService navigationService;
        private readonly IDialogService dialogService;
        private readonly IJiraService jiraService;

        private CancellationTokenSource cancellationTokenSource = null;
        private string serverUrl;
        private string userName;        
        private string password;
        private bool isDataLoaded;
        private bool isRememberMe;
        private bool loginButtonEnabled;

        public RelayCommand LoginCommand { get; private set; }
        public RelayCommand AboutCommand { get; private set; }   

        public string ServerUrl
        {
            get { return serverUrl; }
            set 
            {
                if (serverUrl != value)
                {
                    serverUrl = value;
                    App.ServerUrl = serverUrl;
                    RaisePropertyChanged(() => ServerUrl);
                }                
            }
        }

        public bool LoginButtonEnabled
        {
            get
            {
                return loginButtonEnabled;
            }
            set
            {
                loginButtonEnabled = value;
                RaisePropertyChanged(() => LoginButtonEnabled);
                LoginCommand.RaiseCanExecuteChanged();
            }
        }

        public string UserName
        {
            get { return userName; }
            set 
            {
                if (userName != value)
                {
                    userName = value;
                    App.UserName = userName;
                    RaisePropertyChanged(() => UserName);
                }
                
            }
        }

        public string Password
        {
            get { return password; }
            set
            {
                if (password != value)
                {
                    password = <PASSWORD>;
                    App.Password = password;
                    RaisePropertyChanged(() => Password);
                }

            }
        }        

        public bool IsDataLoaded
        {
            get { return isDataLoaded; }
            set 
            {
                if (value != isDataLoaded)
                {
                    isDataLoaded = value;
                    RaisePropertyChanged(() => IsDataLoaded);
                }
            }
        }     
        
        public LoginViewModel(INavigationService navigationService, IDialogService dialogService, IJiraService jiraService)
        {            
            this.navigationService = navigationService;
            this.dialogService = dialogService;
            this.jiraService = jiraService;         
   
            InitializeData();
        }

        public async void Login()
        {
            IsDataLoaded = false;
            LoginButtonEnabled = false;
            if (!IsInputsValid())
            {
                IsDataLoaded = true;
                dialogService.ShowDialog(AppResources.UnauthorizedMessage, AppResources.LoginFailedMessage);
                LoginButtonEnabled = true;
                return;
            }

            if (!ValidateUrl(ServerUrl))
            {
                IsDataLoaded = true;
                dialogService.ShowDialog(AppResources.InvalidServerUrlMessage, AppResources.LoginFailedMessage);
                LoginButtonEnabled = true;
                return;
            }
            try
            {
                //Create new cancellation token source
                cancellationTokenSource = new CancellationTokenSource();

                //Set base url for rest api
                App.BaseUrl = string.Format("{0}/rest/api/latest/", App.ServerUrl);
                var isLoginSuccess = await jiraService.LoginAsync(ServerUrl, UserName, Password, cancellationTokenSource);
                if (isLoginSuccess)
                {
                    App.IsLoggedIn = true;
                    StorageHelper.SaveUserCredential(ServerUrl, UserName, Password);                        
                    navigationService.Navigate<ProjectListViewModel>();                    
                }
            }         
            catch (HttpRequestStatusCodeException exception)
            {
                if (exception.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                {
                    dialogService.ShowDialog(AppResources.UnauthorizedMessage, AppResources.LoginFailedMessage);
                }
                else if (exception.StatusCode == System.Net.HttpStatusCode.Forbidden)
                {
                    dialogService.ShowDialog(AppResources.ForbiddenMessage, AppResources.LoginFailedMessage);
                }
                else
{'fraction_non_alphanumeric': 0.06261198486959985, 'fraction_numerical': 0.00019908421262193908, 'mean_word_length': 1.2802995914661826, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4919824', 'n_tokens_mistral': 2264, 'n_tokens_neox': 2135, 'n_words': 459}