using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using iElection.Models;
using iElection.Models.Dto;
using iElection.Models.Entities;
using iElection.Services.Contracts;
using iElection.Utilities;
using Prism.Commands;
using Prism.Navigation;
using Refit;

namespace iElection.ViewModels
{
    public class SignUpPageViewModel : ViewModelBase<RegisterDto>
    {
        public RegisterInfoDto InfoDto { get; set; } = new RegisterInfoDto();
        public ICommand SignUpCommand { get; set; }
        public ICommand LoginCommand { get; set; }
        public SignUpPageViewModel(INavigationService navigationService , IRestWrapper restWrapper):base(navigationService,restWrapper)
        {

        }

        public override async void Initialize(INavigationParameters parameters)
        {
            base.Initialize(parameters);
            try
            {
                UtilitiesWrapper.Instance.PopUpUtilities.PushIndicator();
                var rest = await RestWrapper.PageRestApi.GetRegisterInfo(UtilitiesWrapper.Instance.UserUtilities
                    .UserToken);
                if (rest.IsSuccess)
                {
                    rest.Data.Genders = new List<GenderItemModel>
                    {
                        new GenderItemModel
                        {
                            Name = "مرد",
                            GenderType = GenderType.Male
                        },
                        new GenderItemModel
                        {
                            Name = "زن",
                            GenderType = GenderType.Female
                        }
                    };
                    InfoDto = rest.Data;
                    UtilitiesWrapper.Instance.PopUpUtilities.PopAsync();
                }
            }
            catch (ApiException e)
            {
                RestWrapper.UnhandledExceptionRaiser(e);
            }
            catch (Exception e)
            {
                UtilitiesWrapper.Instance.PopUpUtilities.PushError(e.Message);
            }
        }

        public override void InitializeCommand()
        {
            base.InitializeCommand();
            try
            {
                LoginCommand = new DelegateCommand(() =>
                {
                    NavigationService.NavigateAsync("LoginPage");
                });
                SignUpCommand = new DelegateCommand(async () =>
                {
                    try
                    {
                        UtilitiesWrapper.Instance.PopUpUtilities.PushIndicator();
                        if (string.IsNullOrEmpty(PageDto.FirstName))
                            throw new Exception("نام را وارد کنید");
                        if (string.IsNullOrEmpty(PageDto.LastName))
                            throw new Exception("نام خانوادگی را وارد کنید");
                        if (string.IsNullOrEmpty(PageDto.Phone))
                            throw new Exception("شماره تلفن را وارد کنید");
                        if (PageDto.JobId <= 0)
                            throw new Exception("شغل را وارد کنید");
                        if (PageDto.DistrictId <= 0)
                            throw new Exception("محله را وارد کنید");
                        if (string.IsNullOrEmpty(PageDto.Password) || string.IsNullOrEmpty(PageDto.RePassword))
                            throw new Exception("رمز عبور ها را وارد کنید");
                        if (PageDto.Password != PageDto.RePassword)
                            throw new Exception("تایید رمز عبور یکسان نیست");
                        var rest = await RestWrapper.AccountApiRest.RegisterUser(PageDto);
                        if (rest.IsSuccess)
                        {

                            UtilitiesWrapper.Instance.UserUtilities.UserToken = "Bearer " + rest.Data.jwt;
                            await NavigationService.NavigateAsync("MainPage");
                            UtilitiesWrapper.Instance.PopUpUtilities.PushSuccess("ثبت نام با موفقیت انجام شد");
                        }

                    }
                    catch (ApiException e)
                    {
                        RestWrapper.UnhandledExceptionRaiser(e);
                    }
                    catch (Exception e)
                    {
                        UtilitiesWrapper.Instance.PopUpUtilities.PushError(e.Message);
                    }
                });
            }
            catch (Exception e)
            {
                UtilitiesWrapper.Instance.PopUpUtilities.PushError(e.Message);
            }
        }
    }
}
