using System.Linq;
using System.Windows;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;

namespace WPListDirectoriesAndFiles
{
    public partial class MainPage : PhoneApplicationPage
    {
        public IsolatedStorageFile file;

        public MainPage()
        {
            InitializeComponent();

            SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;

            try
            {
                file = IsolatedStorageFile.GetUserStoreForApplication();
                
                //Erstellen von leeren Órdnern und Dateien
                file.CreateDirectory("Subdirectory1");
                file.CreateDirectory("Subdirectory2");
                file.CreateDirectory("Subdirectory3");
                file.CreateDirectory("Subdirectory4");
                file.CreateDirectory("Subdirectory5");

                file.CreateDirectory("Ordner1");
                file.CreateDirectory("Ordner2");
                file.CreateDirectory("Ordner3");
                file.CreateDirectory("Ordner4");
                file.CreateDirectory("Ordner5");

                file.CreateDirectory("Ordner1/folder1");
                file.CreateDirectory("Ordner1/folder2");

                file.CreateFile("test1.txt").Close();
                file.CreateFile("test2.txt").Close();
                file.CreateFile("test3.txt").Close();
                file.CreateFile("test4.txt").Close();
                file.CreateFile("test5.txt").Close();

                file.CreateFile("leer1.txt").Close();
                file.CreateFile("leer2.txt").Close();
                file.CreateFile("leer3.txt").Close();
                file.CreateFile("leer4.txt").Close();
                file.CreateFile("leer5.txt").Close();

                file.CreateFile("Ordner1/unterordner1.txt").Close();
                file.CreateFile("Ordner1/unterordner2.txt").Close();
                file.CreateFile("Ordner1/unterordner3.txt").Close();
                file.CreateFile("Ordner1/unterordner4.txt").Close();
                file.CreateFile("Ordner1/unterordner5.txt").Close();

            }
            catch (IsolatedStorageException ios)
            {
            }
        }

        private void btnListAll_Click(object sender, RoutedEventArgs e)
        {
            string[] files = file.GetFileNames();
            string[] folders = file.GetDirectoryNames();

            lbEntries.Items.Clear();
            lbEntries.Items.Add("Dateien:");

            foreach (var item in files)
            {
                lbEntries.Items.Add("     "+item);
            }

            lbEntries.Items.Add("Ordner:");

            foreach (var item in folders)
            {
                lbEntries.Items.Add("     " + item);
            }
        }

        private void btnListFiltered_Click(object sender, RoutedEventArgs e)
        {
            //Schlägt aufgrund eines Bugs fehl
            //string[] fileNames = file.GetFileNames("test*.txt");
            //string[] folders = file.GetDirectoryNames("Ordner*");

            var fileNames = from filename in file.GetFileNames()
                            where 
                            System.IO.Path.GetFileName(filename).StartsWith("test")
                            &&
                            System.IO.Path.GetExtension(filename).Equals(".txt")
                            select filename;
            var folders = from foldername in file.GetDirectoryNames()
                            where
                            foldername.StartsWith("Ordner")
                            select foldername;
            lbEntries.Items.Clear();
            lbEntries.Items.Add("Dateien mit 'test*.txt':");

            foreach (var item in fileNames)
            {
                lbEntries.Items.Add("     " + item);
            }

            lbEntries.Items.Add("Ordner mit 'Ordner*':");

            foreach (var item in folders)
            {
                lbEntries.Items.Add("     " + item);
            }
        }

        private void btnSubFolderEntries_Click(object sender, RoutedEventArgs e)
        {
            string[] files = file.GetFileNames("Ordner1/");
            string[] folders = file.GetDirectoryNames("Ordner1/");

            lbEntries.Items.Clear();
            lbEntries.Items.Add("Dateien aus 'Ordner1':");

            foreach (var item in files)
            {
                lbEntries.Items.Add("     " + item);
            }

            lbEntries.Items.Add("Ordner in 'Ordner1':");

            foreach (var item in folders)
            {
                lbEntries.Items.Add("     " + item);
            }
        }
    }
}