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.ActivePhone;
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 ActivePhonePageViewModel : ViewModelBase
    {
        public List<ContentView> ActivePhones { get; set; }
        private int _position = 0;
        public int Position
        {
            get { return _position; }
            set { SetProperty(ref _position, value); }
        }

        private RestClient restClient = new RestClient(App.BaseAddress);
        private Student Student = null;
        private string PhoneNumber = string.Empty;
        public ActivePhonePageViewModel(INavigationService navigationService,IPageDialogService dialogService ):base(navigationService)
        {
            var enterCode = new EnterCode();
            enterCode.SubmitCodeClicked += (async (code, e) =>
            {
                var phoneCode = code as string;
                if (string.IsNullOrEmpty(phoneCode))
                {
                    dialogService.DisplayAlertAsync("خطا", "لطفا کد صحیح را وارد کنید", "بستن");
                }
                else
                {
                    PopupNavigation.Instance.PushAsync(new PopupPage
                    {
                        Content = new Frame
                        {
                            VerticalOptions = LayoutOptions.Center,
                            HorizontalOptions = LayoutOptions.Center,
                            Content = new ActivityIndicator
                            {
                                WidthRequest = 100,
                                HeightRequest = 100,
                                IsRunning = true
                            }
                        }
                    });
                    RestRequest restRequest = new RestRequest("Student/Code", Method.GET);
                    restRequest.AddQueryParameter("code", phoneCode);
                    var respond = await restClient.ExecuteGetAsync<bool>(restRequest);
                    if (respond.StatusCode==HttpStatusCode.OK)
                    {
                        NavigationParameters navigationParameters = new NavigationParameters();
                        navigationParameters.Add("Student",Student);
                        navigationService.NavigateAsync("SignUpPage",navigationParameters);
                    }
                    else
                    {
                        dialogService.DisplayAlertAsync("خطا", respond.Content, "بستن");
                    }

                    PopupNavigation.Instance.PopAllAsync();
                }
            });
            var enterPhone = new EnterPhoneNumber();
            enterPhone.SubmitPhoneCliced += (async (phone, e) =>
            {
                var phoneNumber = phone as string;
                if (phoneNumber == null)
                {
                    dialogService.DisplayAlertAsync("خطا", "لطفا شماره تلفن خود را وارد کنید", "بستن");
                }
                else
                {
                    await PopupNavigation.Instance.PushAsync(new PopupPage
                    {
                        Content = new Frame
                        {
                            VerticalOptions = LayoutOptions.Center,
                            HorizontalOptions = LayoutOptions.Center,
                            Content = new ActivityIndicator
                            {
                                WidthRequest = 100,
                                HeightRequest = 100,
                                IsRunning = true
                            }
                        }
                    });
                    RestRequest restRequest = new RestRequest("Student/Code",Method.POST);
                    restRequest.AddQueryParameter("phoneNumber", phoneNumber);
                    var respond = await restClient.ExecutePostAsync<Student>(restRequest);
                    if (respond.IsSuccessful && respond.Data!=null)
                    {
                        PhoneNumber = phoneNumber;
                        Student = respond.Data;
                        Position++;
                    }
                    else
                    {
                        dialogService.DisplayAlertAsync("خطا", respond.Content, "بستن");
                    }

                    PopupNavigation.Instance.PopAllAsync();
                }
            });
            ActivePhones = new List<ContentView>
            {
                enterPhone,
                enterCode
            };
        }
    }
}
