using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TwitterClient.ViewModel;
using System.ComponentModel;
using TwitterClient.Model;

namespace TwitterClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private TwitterViewModel viewModel;
        private TimeLines timeLine = TimeLines.UserTimeline;
        public MainWindow()
        {
            InitializeComponent();
            viewModel = (TwitterViewModel)this.DataContext;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            viewModel.loadUserTweets(timeLine);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            viewModel.updateTwitter(timeLine);
            textBox1.Text = null;
        }

        private void Data_checked(object sender, RoutedEventArgs e)
        {
            ICollectionView view =
            CollectionViewSource.GetDefaultView(viewModel.Tweets);
            view.SortDescriptions.Add(new SortDescription(
            "PostDate", ListSortDirection.Ascending));
        }

        private void Data_unchecked(object sender, RoutedEventArgs e)
        {
            ICollectionView view =
           CollectionViewSource.GetDefaultView(viewModel.Tweets);
            view.SortDescriptions.Remove(new SortDescription(
            "PostDate", ListSortDirection.Ascending));
        }

        private void user_Click(object sender, RoutedEventArgs e)
        {
            timeLine = TimeLines.UserTimeline;
            viewModel.loadUserTweets(timeLine);
        }

        private void home_Click(object sender, RoutedEventArgs e)
        {
            timeLine = TimeLines.HomeTimeline;
            viewModel.loadUserTweets(timeLine);
        }

        private void public_Click(object sender, RoutedEventArgs e)
        {
            timeLine = TimeLines.PublicTimeline;
            viewModel.loadUserTweets(timeLine);
        }

        private void mentions_Click(object sender, RoutedEventArgs e)
        {
            timeLine = TimeLines.Mentions;
            viewModel.loadUserTweets(timeLine);
        }

        private void retweetByMe_Click(object sender, RoutedEventArgs e)
        {
            timeLine = TimeLines.RetweetedByMe;
            viewModel.loadUserTweets(timeLine);
        }

        private void ScreenName_checked(object sender, RoutedEventArgs e)
        {
            ICollectionView view =
           CollectionViewSource.GetDefaultView(viewModel.Tweets);
            view.SortDescriptions.Add(new SortDescription(
            "ScreenName", ListSortDirection.Ascending));
        }

        private void ScreenName_unchecked(object sender, RoutedEventArgs e)
        {
            ICollectionView view =
           CollectionViewSource.GetDefaultView(viewModel.Tweets);
            view.SortDescriptions.Remove(new SortDescription(
            "ScreenName", ListSortDirection.Ascending));
        }

        private void Uzytkownik_checked(object sender, RoutedEventArgs e)
        {
            ICollectionView view =
            CollectionViewSource.GetDefaultView(viewModel.Tweets);
            view.GroupDescriptions.Add(new PropertyGroupDescription("ScreenName"));

        }

        private void Uzytkownik_unchecked(object sender, RoutedEventArgs e)
        {
            ICollectionView view =
           CollectionViewSource.GetDefaultView(viewModel.Tweets);
            view.GroupDescriptions.Clear();
        }

        private void textBox2_TextChanged(object sender, TextChangedEventArgs e)
        {
            ICollectionView view =
            CollectionViewSource.GetDefaultView(viewModel.Tweets);
            view.Filter = new Predicate<object>(TweetFilter);
        }
        private bool TweetFilter(object obj)
        {
        Tweet tweet = obj as Tweet;
        return (tweet.ScreenName.Contains(textBox2.Text)) || (tweet.Text.Contains(textBox2.Text));

        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            InfoDialogBox dialog = new InfoDialogBox();
            dialog.ShowDialog();
        }
    }
}
