﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nane.Helpers;
using Nane.Models;
using Nane.Views;
using Xamarin.Forms;

namespace Nane.ViewModels
{
    public class ArtistRegisterPageViewModel : BaseViewModel
    {
        #region Properties
        private string _usernameText;

        public string UsernameText
        {
            get { return _usernameText; }
            set { _usernameText = value; OnPropertyChanged(); }
        }

        private string _passwordText;

        public string PasswordText
        {
            get { return _passwordText; }
            set { _passwordText = value; OnPropertyChanged(); }
        }

        private string _artistNameText;

        public string ArtistNameText
        {
            get { return _artistNameText; }
            set { _artistNameText = value; OnPropertyChanged(); }
        }


        #endregion

        #region Commands

        public DelegateCommand RegisterCommand { get; set; }
        #endregion

        private INavigation _navigationService;
        public ArtistRegisterPageViewModel(INavigation navigationService)
        {
            _navigationService = navigationService;
            RegisterCommand = new DelegateCommand(Register);
        }

        private async void Register()
        {
            var registerResult =
                await
                    _naneService.PostUser(new ArtistLoginModel()
                    {
                        Mail = UsernameText,
                        Name = ArtistNameText,
                        Password = PasswordText
                    });

            if (registerResult != null)
            { // Success
                LoginHelper.EMail = UsernameText;
                LoginHelper.Password = PasswordText;
                LoginHelper.UserInfo = registerResult;
                await _navigationService.PushModalAsync(new ArtistProfilePage());
            }
            else
            {
                OnPropertyChanged("_This user is already exists.");
            }
        }
    }
}
