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

namespace iElection.ViewModels
{
    public class MainPageViewModel : ViewModelBase<MainPageDto>
    {
        public ICommand AddNewContactCommand { get; set; }
        public ICommand SelectContactCommand { get; set; }
        public ICommand SelectNotificationCommand { get; set; }
        public bool IsWorking { get; set; } = false;
        public string Text { get; set; }
        public ObservableCollection<string> Images { get; set; } = new ObservableCollection<string>();
        public MainPageViewModel(INavigationService navigationService , IRestWrapper restWrapper)  : base(navigationService,restWrapper)
        {
        }

        public override async void Initialize(INavigationParameters parameters)
        {
            base.Initialize(parameters);
            try
            {
                if (IsWorking)
                    return;
                PageDto.Contacts.Clear();
                PageDto.Notifications.Clear();
                Images.Clear();
                IsWorking = true;

                UtilitiesWrapper.Instance.PopUpUtilities.PushIndicator();
                var rest = await RestWrapper.CrudApiRest<Notification, int>(Address.NotificationController)
                    .ReadAll(UtilitiesWrapper.Instance.UserUtilities.UserToken);
                if (rest.IsSuccess)
                    rest.Data.ForEach(n => PageDto.Notifications.Add(n));
                var restContact = await RestWrapper.CrudApiRest<Contact, int>(Address.ContactController)
                    .ReadAll(UtilitiesWrapper.Instance.UserUtilities.UserToken);
                if (restContact.IsSuccess)
                    restContact.Data.ForEach(c => PageDto.Contacts.Add(c));
                var restCV = await RestWrapper.PageRestApi.GetInfoCv(UtilitiesWrapper.Instance.UserUtilities.UserToken);
                if (restCV.IsSuccess)
                {
                    Text = restCV.Data.Text;
                    restCV.Data.Images.ForEach(i=>Images.Add(i));
                    Title = restCV.Data.Title;
                }
                await UtilitiesWrapper.Instance.PopUpUtilities.PopAsync();

            }
            catch (ApiException e)
            {
                await UtilitiesWrapper.Instance.PopUpUtilities.PopAsync();
                RestWrapper.UnhandledExceptionRaiser(e);
            }
            catch (Exception e)
            {
                await UtilitiesWrapper.Instance.PopUpUtilities.PopAsync();
                UtilitiesWrapper.Instance.PopUpUtilities.PushError(e.Message);
            }
            finally
            {
                IsWorking = false;
            }
        }

        public override void InitializeCommand()
        {
            base.InitializeCommand();
            AddNewContactCommand = new DelegateCommand(() =>
            {
                NavigationService.NavigateAsync("AddContactPage");
            });
            SelectContactCommand = new DelegateCommand<Contact>(async (contact) =>
            {
                try
                {
                    UtilitiesWrapper.Instance.PopUpUtilities.PushIndicator();
                    var rest = await RestWrapper.CrudApiRest<Contact, int>(Address.ContactController)
                        .ReadOne(contact.Id, UtilitiesWrapper.Instance.UserUtilities.UserToken);
                    if (rest.IsSuccess)
                    {
                        await UtilitiesWrapper.Instance.PopUpUtilities.PopAsync();
                        var param = new NavigationParameters();
                        param.Add("Contact",new InviteContactDto
                        {
                            Age = rest.Data.Age,
                            District = rest.Data.District,
                            FirstName = rest.Data.FirstName,
                            LastName = rest.Data.LastName,
                            Job = rest.Data.Job,
                            Phone = rest.Data.Phone,
                            Gender = rest.Data.Gender!="مرد"?GenderType.Male:GenderType.Female
                        });
                        await NavigationService.NavigateAsync("AddContactPage", param);
                    }
                }
                catch (ApiException e)
                {
                    RestWrapper.UnhandledExceptionRaiser(e);
                }
                catch (Exception e)
                {
                    UtilitiesWrapper.Instance.PopUpUtilities.PushError(e.Message);
                }
            });
            SelectNotificationCommand = new DelegateCommand<Notification>(async (notify) =>
            {
                try
                {
                    UtilitiesWrapper.Instance.PopUpUtilities.PushIndicator();
                    var rest = await RestWrapper.CrudApiRest<Notification, int>(Address.NotificationController)
                        .ReadOne(notify.Id, UtilitiesWrapper.Instance.UserUtilities.UserToken);
                    if (rest.IsSuccess)
                    {
                        await UtilitiesWrapper.Instance.PopUpUtilities.PopAsync();
                        await UtilitiesWrapper.Instance.PopUpUtilities.PushAsync(new NotificationPopUp(rest.Data));
                    }
                }
                catch (ApiException e)
                {
                    RestWrapper.UnhandledExceptionRaiser(e);
                }
                catch (Exception e)
                {
                    UtilitiesWrapper.Instance.PopUpUtilities.PushError(e.Message);
                }
                
            });
        }
    }
}
