using Model;
using Model.Serialization;
using Model.Stub;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics.Eventing.Reader;

namespace Linaris
{
    public partial class LocalFilesPage : ContentPage
    {
        private ObservableCollection<CustomTitle> customTitles = (Application.Current as App).Manager.GetCustomTitles();

        public ObservableCollection<CustomTitle> CustomTitles
        {
            get => customTitles;
        }

        private ObservableCollection<Playlist> playlists = (Application.Current as App).Manager.GetPlaylists();

        public ObservableCollection<Playlist> Playlists
        {
            get => playlists;
        }

        public LocalFilesPage()
        {
            InitializeComponent();
            BindingContext = this;
        }

        // Reset methods

        void ResetAll(object sender, EventArgs e)
        {
            ResetSubMenus(sender, e);
        }

        void ResetSubMenus(object sender, EventArgs e)
        {
            foreach (var CustomTitle in customTitles)
            {
                CustomTitle.IsSubMenuVisible = false;
            }
            ResetPlaylistMenu(sender, e);
        }
        
        void ResetPlaylistMenu(object sender, EventArgs e)
        {
            foreach (CustomTitle customTitle in CustomTitles)
            {
                customTitle.IsPlaylistMenuVisible = false;
                customTitle.IsNewPlaylistMenuVisible = false;
            }
        }


        // Add methods

        void AddCustomTitle(CustomTitle customTitle)
        {
            (Application.Current as App).Manager.AddCustomTitle(customTitle);
            customTitles.Add(customTitle);
            ResetAll(this, null);
        }

        private async void AddTitles(object sender, EventArgs e)
        {
            var results = await FilePicker.PickMultipleAsync(new PickOptions
            {
                PickerTitle = "Choisissez des nouveaux titres !",
                FileTypes = new FilePickerFileType(
                    new Dictionary<DevicePlatform, IEnumerable<string>>
                    {
                        { DevicePlatform.WinUI, new [] { "*.mp3", "*.m4a" } },
                        { DevicePlatform.Android, new [] { "*.mp3", ".3gp", ".mp4", ".m4a", ".aac", ".ts", ".amr", ".flac", ".mid", ".xmf", ".mxmf", ".rtttl", ".rtx", ".ota", ".imy", ".mkv", ".ogg", ".wav" } },
                        { DevicePlatform.iOS, new[] { "*.mp3", "*.aac", "*.aifc", "*.au", "*.aiff", "*.mp2", "*.3gp", "*.ac3" } }
                    })
            });

            if (results == null)
            {
                return;
            }

            if (sender is Button button)
            {
                foreach (var result in results)
                {
                    CustomTitle custom = new CustomTitle(result.FileName, "none.png", "", result.FullPath);
                    if (!IsCustomTitleInCollection(custom))
                    {
                        AddCustomTitle(custom);
                    }
                }
            }
        }

        void AddToPlaylist(object sender, EventArgs e)
        {
            if (sender is Button button)
            {
                if (button.BindingContext is Playlist playlist)
                {
                    if (button.Parent is StackLayout stack && stack.BindingContext is CustomTitle customTitle)
                    {
                        playlist.AddTitle(customTitle);
                        ResetAll(sender, e);
                    }
                }
            }
        }

        void AddPlaylist(object sender, EventArgs e)
        {
            if (sender is Entry entry)
            {
                Playlist playlist = new Playlist(entry.Text, "", "none.png");
                if (!IsInPlaylists(playlist))
                {
                    (Application.Current as App).Manager.AddPlaylist(playlist);
                    playlists.Add(playlist);
                }
                if(entry.BindingContext is CustomTitle customTitle)
                {
                    customTitle.IsNewPlaylistMenuVisible = false;
                    entry.Text = "";
                }
            }
        }


        // Remove methods

        void RemoveCustomTitle(object sender, EventArgs e)
        {
            if (sender is Button button)
            {
                if (button.BindingContext is CustomTitle titleToRemove)
                {
                    (Application.Current as App).Manager.RemoveCustomTitle(titleToRemove);
                    customTitles.Remove(titleToRemove);
                }
            }
        }


        // Show methods

        void ShowSubMenu(object sender, EventArgs e)
        {
            if (sender is Image image)
            {
                if (image.BindingContext is CustomTitle customTitle)
                {
                    if (!customTitle.IsSubMenuVisible)
                    {
                        ResetAll(sender, e);
                        customTitle.IsSubMenuVisible = true;
                    }
                    else
                    {
                        ResetSubMenus(sender, e);
                    }
                }
            }
        }

        void ShowPlaylistMenu(object sender, EventArgs e)
        {
            if (sender is Button button)
            {
                if (button.BindingContext is CustomTitle customTitle)
                {
                    if (!customTitle.IsPlaylistMenuVisible)
                    {
                        customTitle.IsPlaylistMenuVisible = true;
                    }
                }
            }
        }

        void ShowNewPlaylistMenu(object sender, EventArgs e)
        {
            if (sender is Button button)
            {
                if (button.BindingContext is CustomTitle customTitle)
                {
                    if (!customTitle.IsNewPlaylistMenuVisible)
                    {
                        customTitle.IsNewPlaylistMenuVisible = true;
                    }
                }
            }
        }


        // Change methods

        private async void ChangeImage(object sender, EventArgs e)
        {
            var result = await FilePicker.PickAsync(new PickOptions
            {
                PickerTitle = "Choisissez une nouvelle image !",
                FileTypes = FilePickerFileType.Images
            });

            if (result == null)
            {
                return;
            }

            if (sender is Button button)
            {
                if (button.BindingContext is CustomTitle customTitle)
                {
                    customTitle.ImageURL = result.FullPath;
                }
            }
        }

        // Search methods

        bool IsCustomTitleInCollection(CustomTitle customTitle)
        {
            foreach(var custom in customTitles)
            {
                if (customTitle.Equals(custom))
                {
                    return true;
                }
            }
            return false;
        }

        bool IsInPlaylists(Playlist playlist)
        {
            foreach (Playlist p in playlists)
            {
                if (p.Equals(playlist))
                {
                    return true;
                }
            }
            return false;
        }
    }
}