using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using FoxTube.Classes;
using Windows.Storage;
using System.Xml;

namespace FoxTube.Pages.SettingsPages
{
    /// <summary>
    /// Page with changelogs and dev messages
    /// </summary>
    public sealed partial class Inbox : Page
    {
        List<InboxItem> items = new List<InboxItem>();

        string open;
        public Inbox()
        {
            InitializeComponent();
        }

        public async void LoadItems()
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                               
                StorageFile file = await (await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(@"Assets\Data")).GetFileAsync("Patchnotes.xml");
                doc.Load(await file.OpenStreamForReadAsync());
                foreach (XmlElement e in doc["items"].ChildNodes)
                    items.Add(new InboxItem(
                        e.GetAttribute("version"),
                        e["content"][SettingsStorage.Language].InnerText,
                        e.GetAttribute("time")));

                doc.Load("http://foxgame-studio.000webhostapp.com/foxtube-messages.xml");
                foreach (XmlElement e in doc["posts"].ChildNodes)
                    items.Add(new InboxItem(
                        e["header"][SettingsStorage.Language].InnerText,
                        e["content"][SettingsStorage.Language].InnerText,
                        DateTime.Parse(e.GetAttribute("time")),
                        e["id"].InnerText
                        ));

                items.OrderBy(item => item.TimeStamp);
            }
            catch { }

            items.ForEach(i => list.Items.Add(i));

            if (!string.IsNullOrWhiteSpace(open))
                Open(open);

            open = null;
        }

        private void filter_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                list.Items.Clear();

                switch (filter.SelectedIndex)
                {
                    case 0:
                        items.ForEach(i => list.Items.Add(i));
                        break;

                    case 1:
                        List<InboxItem> messages = items.FindAll(i => i.Type == InboxItemType.Default);
                        messages.ForEach(i => list.Items.Add(i));
                        break;

                    case 2:
                        List<InboxItem> notes = items.FindAll(i => i.Type == InboxItemType.PatchNote);
                        notes.ForEach(i => list.Items.Add(i));
                        break;
                }
            }
            catch(NullReferenceException) { }
        }

        private void list_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            InboxItem item = list.SelectedItem as InboxItem;
            if(list.SelectedItem != null)
            {
                OpenView(item.Title, item.Content);
                if (grid.ColumnDefinitions[1].Width.Value == 0)
                {
                    grid.ColumnDefinitions[0].Width = new GridLength(0);
                    grid.ColumnDefinitions[1].Width = new GridLength(1, GridUnitType.Star);
                }
            }
        }

        void CloseView()
        {
            content.Text = "";
            title.Text = "";
            list.SelectedItem = null;
            block.Visibility = Visibility.Visible;
        }

        void OpenView(string header, string body)
        {
            content.Text = body;
            title.Text = header;
            block.Visibility = Visibility.Collapsed;
        }

        private void close_Click(object sender, RoutedEventArgs e)
        {
            CloseView();
            if (grid.ColumnDefinitions[0].Width.Value == 0)
            {
                grid.ColumnDefinitions[0].Width = new GridLength(1, GridUnitType.Star);
                grid.ColumnDefinitions[1].Width = new GridLength(0);
            }
        }

        public void Open(string arg)
        {
            if(items.Count == 0)
            {
                open = arg;
                return;
            }

            InboxItem item = items.Find(i => i.Id == arg);
            if(item != null)
                list.SelectedItem = item;
        }
    }
}
