using System;
using System.Collections.ObjectModel;
using TwitterClient.Model;
using Twitterizer;
using System.Windows;

public enum TimeLines
{
    UserTimeline,
    HomeTimeline,
    PublicTimeline,
    Mentions,
    RetweetedByMe
}

namespace TwitterClient.ViewModel
{
    class TwitterViewModel
    {
        private ObservableCollection<Tweet> tweets = new ObservableCollection<Tweet>();
        TwitterResponse<TwitterStatusCollection> twitterStatuses = new TwitterResponse<TwitterStatusCollection>();
        public String TweetText {get;set;}
        public void loadUserTweets(TimeLines timeLine)
        {
            MessageBoxResult result;
            try
            {
                Holders.TokenBuilder.buidTokens();
                switch(timeLine)
                {
                    case TimeLines.UserTimeline: twitterStatuses = TwitterTimeline.UserTimeline(Holders.TokenBuilder.Tokens);
                        break;
                    case TimeLines.HomeTimeline: twitterStatuses = TwitterTimeline.HomeTimeline(Holders.TokenBuilder.Tokens);
                        break;
                    case TimeLines.PublicTimeline: twitterStatuses = TwitterTimeline.PublicTimeline(Holders.TokenBuilder.Tokens);
                        break;
                    case TimeLines.Mentions: twitterStatuses = TwitterTimeline.Mentions(Holders.TokenBuilder.Tokens);
                        break;
                    case TimeLines.RetweetedByMe: twitterStatuses = TwitterTimeline.RetweetedByMe(Holders.TokenBuilder.Tokens);
                        break;
                }
            }
            catch (ArgumentNullException)
            {
                result = MessageBox.Show("Podałeś błędy PIN, zalogowanie nie jest możliwe. Czy wczytać publiczne statusy?", "Błąd logowania", MessageBoxButton.YesNo, MessageBoxImage.Warning);
                if (result.Equals(MessageBoxResult.Yes))
                {
                    twitterStatuses = TwitterTimeline.PublicTimeline();
                }
                else
                {
                    return;
                }
            }
            showTweets(twitterStatuses);
        }
        public void updateTwitter(TimeLines timeLine)
        {

            try
            {
                Holders.TokenBuilder.buidTokens();
                TwitterStatus.Update(
                Holders.TokenBuilder.Tokens,
                TweetText
                );
                switch (timeLine)
                {
                    case TimeLines.UserTimeline: twitterStatuses = TwitterTimeline.UserTimeline(Holders.TokenBuilder.Tokens);
                        break;
                    case TimeLines.HomeTimeline: twitterStatuses = TwitterTimeline.HomeTimeline(Holders.TokenBuilder.Tokens);
                        break;
                    case TimeLines.PublicTimeline: twitterStatuses = TwitterTimeline.PublicTimeline(Holders.TokenBuilder.Tokens);
                        break;
                    case TimeLines.Mentions: twitterStatuses = TwitterTimeline.Mentions(Holders.TokenBuilder.Tokens);
                        break;
                    case TimeLines.RetweetedByMe: twitterStatuses = TwitterTimeline.RetweetedByMe(Holders.TokenBuilder.Tokens);
                        break;
                }
                showTweets(twitterStatuses);
            }
            catch (ArgumentNullException)
            {
                MessageBox.Show("Podałeś błędy PIN, zalogowanie nie jest możliwe. Nie możesz wysłać wiadomości", "Błąd logowania", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
        }
        private void showTweets(TwitterResponse<TwitterStatusCollection> responce)
        {
            tweets.Clear();
            foreach (var tweet in responce.ResponseObject)
            {
                tweets.Add(new Tweet
                {
                    ProfileImage = new Uri(tweet.User.ProfileImageLocation),
                    ScreenName = tweet.User.ScreenName,
                    Text = tweet.Text,
                    PostDate = tweet.CreatedDate
                });
            }
        }
        public ObservableCollection<Tweet> Tweets { get { return tweets; } }
    }
}
