using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using CafeTerrace.Models;
using CafeTerrace.Models.Api;
using CafeTerrace.Models.DataBase;
using CafeTerrace.Services;
using CafeTerrace.Utilities;
using Prism.Navigation;
using Prism.Services;
using RestSharp;

namespace CafeTerrace.ViewModels
{
    public class LoginPageViewModel : ViewModelBase
    {
        public ICommand LoginButtonCommand { get; set; }
        public ICommand SignUpButtonCommand { get; set; }
        public User SignUpUser { get; set; } = new User();
        public string Gender { get; set; }
        public LoginPageViewModel(INavigationService navigationService, IPageDialogService dialogService) : base(navigationService, dialogService)
        {
           
        }

        public override void InitializeCommand()
        {
            base.InitializeCommand();
            LoginButtonCommand = new DelegateCommand(async() =>
            {
                try
                {
                    Rg.Plugins.Popup.Services.PopupNavigation.Instance.PushAsync(PopUpUtilites.IndicatorPage);
                    
                    RestService<LoginedUser> restService = new RestService<LoginedUser>(Address.BaseAddress);
                    var dics = new Dictionary<string, string>() {{"username", SignUpUser.Phone},{"password",SignUpUser.Password}};
                    var rest = await restService.RequestParameters(Address.Login, Method.POST, dics);
                    Rg.Plugins.Popup.Services.PopupNavigation.Instance.PopAsync();
                    if (rest.IsSuccess)
                    {
                        Instance.LoginedUser = rest.Data;
                        NavigationService.NavigateAsync("myapp:///NavigationPage/MainPage");
                    }
                    else
                    {
                        PageDialogService.DisplayAlertAsync("خطا", rest.Message, "بستن");
                    }
                }
                catch (Exception e)
                {
                    Rg.Plugins.Popup.Services.PopupNavigation.Instance.PopAsync();
                    PageDialogService.DisplayAlertAsync("خطا", e.Message, "بستن");
                }
            });
            SignUpButtonCommand = new DelegateCommand(async () =>
            {
                try
                {
                    Rg.Plugins.Popup.Services.PopupNavigation.Instance.PushAsync(PopUpUtilites.IndicatorPage);
                    SignUpUser.ConfirmPassword = SignUpUser.Password;
                    SignUpUser.UserName = SignUpUser.Phone;
                    SignUpUser.BirthDate = DateTime.Now;
                    if (Gender != null)
                    {
                        SignUpUser.Gender = Gender == "مرد" ? GenderType.Male : GenderType.Female;
                    }
                    RestService<string> restService = new RestService<string>(Address.BaseAddress);
                    var rest = await restService.RequestBody(Address.SignUp, Method.POST, SignUpUser);
                    if (rest.IsSuccess)
                    {
                        Rg.Plugins.Popup.Services.PopupNavigation.Instance.PopAsync();
                        PageDialogService.DisplayAlertAsync("موفقیت امیز", "ثبت نام شما با موفقیت انجام شد", "بستن");
                    }
                    else
                    {
                        Rg.Plugins.Popup.Services.PopupNavigation.Instance.PopAsync();
                        PageDialogService.DisplayAlertAsync("خطا", rest.Message, "بستن");
                    }
                }
                catch (Exception e)
                {
                    Rg.Plugins.Popup.Services.PopupNavigation.Instance.PopAsync();
                    PageDialogService.DisplayAlertAsync("خطا", e.Message, "بستن");
                }
            });
        }
    }
}
