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;
        public static readonly DependencyProperty TimeLineProperty =
        DependencyProperty.Register("TimeLine", typeof(TimeLines), typeof(MainWindow));

        public TimeLines TimeLine
        {
            get { return (TimeLines)GetValue(TimeLineProperty); }
            set { SetValue(TimeLineProperty, value); }
        }

        public MainWindow()
        {
            InitializeComponent();
            viewModel = (TwitterViewModel)this.DataContext;
            TimeLine = TimeLines.UserTimeline;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            viewModel.loadUserTweets(TimeLine);
            if (TimeLine == TimeLines.UserTimeline) this.taskBarItemInfo.Overlay = (DrawingImage)FindResource("URedOverlay");
            else if (TimeLine == TimeLines.HomeTimeline) this.taskBarItemInfo.Overlay = (DrawingImage)FindResource("HRedOverlay");
            else this.taskBarItemInfo.Overlay = null;
        }

        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)
        {
            load_UserTimeline();
        }

        private void home_Click(object sender, RoutedEventArgs e)
        {
            load_HomeTimeline();
        }

        private void public_Click(object sender, RoutedEventArgs e)
        {
            load_PublicTimeline();
        }

        private void mentions_Click(object sender, RoutedEventArgs e)
        {
            load_Mentions();
        }

        private void retweetByMe_Click(object sender, RoutedEventArgs e)
        {
            load_RetweetedByMe();
        }

        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();
        }

        private void ThumbButtonInfo_Click(object sender, EventArgs e)
        {
            load_UserTimeline();
        }
        private void load_UserTimeline()
        {
            TimeLine = TimeLines.UserTimeline;
            viewModel.loadUserTweets(TimeLine);
            this.taskBarItemInfo.Overlay = (DrawingImage)FindResource("URedOverlay");
        }
        private void load_HomeTimeline()
        {
            TimeLine = TimeLines.HomeTimeline;
            viewModel.loadUserTweets(TimeLine);
            this.taskBarItemInfo.Overlay = (DrawingImage)FindResource("HRedOverlay");
        }
        private void load_PublicTimeline()
        {
            TimeLine = TimeLines.PublicTimeline;
            viewModel.loadUserTweets(TimeLine);
            this.taskBarItemInfo.Overlay = null;
        }
        private void load_Mentions()
        {
            TimeLine = TimeLines.Mentions;
            viewModel.loadUserTweets(TimeLine);
            this.taskBarItemInfo.Overlay = null;
        }
        private void load_RetweetedByMe()
        {
            TimeLine = TimeLines.RetweetedByMe;
            viewModel.loadUserTweets(TimeLine);
            this.taskBarItemInfo.Overlay = null;
        }

        private void ThumbButtonInfo_Click_1(object sender, EventArgs e)
        {
            load_HomeTimeline();
        }

        private void ThumbButtonInfo_Click_2(object sender, EventArgs e)
        {
            load_PublicTimeline();
        }

        private void ThumbButtonInfo_Click_3(object sender, EventArgs e)
        {
            load_Mentions();
        }

        private void ThumbButtonInfo_Click_4(object sender, EventArgs e)
        {
            load_RetweetedByMe();
        }
    }
}
