using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows.Input;
using Moshaverino.Models;
using Moshaverino.Views.SignUp;
using Prism.Navigation;
using Prism.Services;
using RestSharp;
using Rg.Plugins.Popup.Pages;
using Rg.Plugins.Popup.Services;
using Xamarin.Forms;

namespace Moshaverino.ViewModels
{
    public class SignUpPageViewModel : ViewModelBase
    {
        public List<ContentView> SignUps { get; set; }
        private Student Student = null;
        public ICommand SubmitSexButtonCommand { get; set; }
        public ICommand SubmitInfoButtonCommand { get; set; }
        public ICommand SubmitTypeButtonCommand { get; set; }
        public ICommand CloseCommand { get; set; }
        private int _position;
        public int Position
        {
            get { return _position; }
            set { SetProperty(ref _position, value); }
        }

        public SignUpPageViewModel(INavigationService navigationService, IPageDialogService dialogService) : base(navigationService)
        {
            var sex = new SelectSex { BindingContext = this };
            var info = new EnterPersonalInfo { BindingContext = this };
            var type = new EnterTypeInfo { BindingContext = this };
            SubmitSexButtonCommand = new DelegateCommand<Student>((student) =>
            {
                if (student == null)
                {
                    dialogService.DisplayAlertAsync("خطا", "لطفا اطلاعات صحیح را وارد کنید", "بستن");

                }
                else
                {
                    Student.HumenSex = student.HumenSex;
                    Position++;
                }

            });
            SubmitInfoButtonCommand = new DelegateCommand<Student>((student) =>
            {
                if (student == null)
                {
                    dialogService.DisplayAlertAsync("خطا", "لطفا اطلاعات صحیح را وارد کنید", "بستن");

                }
                else
                {
                    Student.FullName = student.FullName;
                    Student.StudyField = student.StudyField;
                    Student.StudyGrade = student.StudyGrade;
                    Student.AverageNumber = student.AverageNumber;
                    Position++;
                }
            });
            SubmitTypeButtonCommand = new DelegateCommand<Student>(async (student) =>
            {
                if (student == null)
                {
                    dialogService.DisplayAlertAsync("خطا", "لطفا اطلاعات صحیح را وارد کنید", "بستن");

                }
                else
                {
                    RestClient restClient = new RestClient(App.BaseAddress);
                    Student.FavoriteLesson = student.FavoriteLesson;
                    Student.UnFavoriteLesson = student.UnFavoriteLesson;
                    Student.FavoriteExam = student.FavoriteExam;
                    Student.AverageTaraz = student.AverageTaraz;
                    PopupNavigation.Instance.PushAsync(new PopupPage
                    {
                        Content = new Frame
                        {
                            VerticalOptions = LayoutOptions.Center,
                            HorizontalOptions = LayoutOptions.Center,
                            Content = new ActivityIndicator
                            {
                                WidthRequest = 150,
                                HeightRequest = 150,
                                IsRunning = true
                            }
                        }
                    });
                    RestRequest restRequest = new RestRequest("Student/SetInfo", Method.POST);
                    restRequest.AddJsonBody( Student);
                    var respond = await restClient.ExecutePostAsync<bool>(restRequest);
                    if (respond.StatusCode==HttpStatusCode.OK)
                    {
                        Position++;
                    }
                    else
                    {
                        dialogService.DisplayAlertAsync("خطا", respond.Content, "بستن");
                    }

                    PopupNavigation.Instance.PopAllAsync();
                }
            });
            CloseCommand = new DelegateCommand(()=>
            {
                navigationService.NavigateAsync("IntroducePage");
            });
            SignUps = new List<ContentView>
            {
                sex,
                type,
                info,
                new SubmitSignUped(){BindingContext = this}
            };
        }

        public override void Initialize(INavigationParameters parameters)
        {
            Student = parameters.GetValue<Student>("Student");
            base.Initialize(parameters);
        }
    }
}
