using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Syncfusion.XForms.PopupLayout;
using XF.Material.Forms.UI;

namespace InABox.Mobile
{
    
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MobileMenuButton
    {

        private MobileMenuButtonMenu _menu;

        public IList<MobileMenuEntry> Items => _menu.Items;
        
        public RelativePosition Position { get; set; }
        
        public event EventHandler Appearing;
        
        private ImageSource _source;
        
        public ImageSource Image
        {
            get => _image.Source;
            set => _image.Source = value;
        }
        
        public Size ImageSize
        {
            get => new Size(_image.WidthRequest,_image.HeightRequest);
            set
            {
                _image.HeightRequest = value.Height;
                _image.WidthRequest = value.Width;
            }
        }
        
        public bool ShowImages
        {
            get => _menu.ShowImages;
            set => _menu.ShowImages = value;
        }

        public MobileMenuButtonMenuImagePosition ImagePosition
        {
            get => _menu.ImagePosition;
            set => _menu.ImagePosition = value;
        }
        
        public event MobileMenuButtonClickedEvent Clicked;
        
        public MobileMenuButton()
        {
            _menu = new MobileMenuButtonMenu();
            _menu.ItemClicked += (sender,args) => PopupManager.DismissPopup();
            InitializeComponent();
            ShowImages = false;
            Position = RelativePosition.AlignToLeftOf;
        }

        private void _image_OnClicked(object sender, EventArgs e)
        {
            Appearing?.Invoke(this, EventArgs.Empty);
            if (_menu.Items.Any())
            {
                PopupManager.ShowPopup(
                    this,
                    () => _menu, 
                    new PopupManagerConfiguration()
                    {
                        SizeMode = AutoSizeMode.Both,
                        Padding = 5,
                        Position = this.Position
                    }
                );
            }
            else
                Clicked?.Invoke(this, new MobileMenuButtonClickedEventArgs(null));
        }

 
    }
    
}