
using System;
using System.Timers;
using iElection.Models.Entities;
using iElection.ViewModels;
using Sharpnado.MaterialFrame;

namespace iElection.Views
{
    
    public partial class MainPage
    {
        private int position = 0;
        public MainPage()
        {
            InitializeComponent();
            Timer timer = new Timer(4000);
            timer.Elapsed += ((sender, args) =>
            {
                position++;
                var count = (BindingContext as MainPageViewModel).Images.Count;
                if (position == count)
                    position = 0;
                carouselView.Position = position;
            });
            timer.Start();
        }

        private void AddNewContact_OnTapped(object sender, EventArgs e)
        {
            if(BindingContext is MainPageViewModel viewModel)
                viewModel.AddNewContactCommand.Execute(null);
        }

        private void Notification_OnTapped(object sender, EventArgs e)
        {
            if (sender is MaterialFrame frame && frame.BindingContext is Notification notification && BindingContext is MainPageViewModel viewModel)
                viewModel.SelectNotificationCommand.Execute(notification);
        }

        private void ContactSelect_OnTapped(object sender, EventArgs e)
        {

            if (sender is MaterialFrame frame && frame.BindingContext is Contact contact && BindingContext is MainPageViewModel viewModel)
                viewModel.SelectContactCommand.Execute(contact);
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();
            if (BindingContext is MainPageViewModel viewModel)
                viewModel.Initialize(null);
        }
    }
}
