using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twitterizer;
using System.Windows;

namespace TwitterClient.Holders
{
    class TokenBuilder
    {
        private static OAuthTokens tokens = new OAuthTokens();
        private static string consumerKey;
        private static string consumerSecret;
        private static OAuthTokenResponse accessToken = new OAuthTokenResponse();
        static TokenBuilder()
        {
            tokens.ConsumerKey = Properties.Settings.Default.ConsumerKey;
            tokens.ConsumerSecret = Properties.Settings.Default.ConsumerSecret;
            tokens.AccessToken = Properties.Settings.Default.AccessToken;
            tokens.AccessTokenSecret = Properties.Settings.Default.AccessTokenSecret;
            consumerKey = Properties.Settings.Default.ConsumerKey;
            consumerSecret = Properties.Settings.Default.ConsumerSecret;
        }
        public static void buidTokens()
        {
            if (!tokens.HasBothTokens)
            {
                OAuthTokenResponse UriToken = new OAuthTokenResponse();
                UriToken = OAuthUtility.GetRequestToken(consumerKey, consumerSecret, "oob");
                Uri pinUri = OAuthUtility.BuildAuthorizationUri(UriToken.Token);

                PinDialogBox dialog = new PinDialogBox();
                try
                {
                    System.Diagnostics.Process.Start(pinUri.AbsoluteUri);
                }
                catch (System.ComponentModel.Win32Exception browserE)
                {
                    MessageBox.Show(browserE.Message);
                }
                catch (System.ObjectDisposedException dispE)
                {
                    MessageBox.Show(dispE.Message);
                }
                catch (System.Exception other)
                {
                    MessageBox.Show(other.Message);
                }
                if (dialog.ShowDialog() == true)
                {
                    string pin;
                    pin = dialog.Pin;
                    try
                    {
                        accessToken = OAuthUtility.GetAccessToken(consumerKey, consumerSecret, UriToken.Token, pin);
                    }
                    catch (ArgumentNullException argE)
                    {
                        throw argE;
                    }
                    // dwa brakujące elementy tokenów zostają dodane
                    tokens.AccessToken = accessToken.Token;
                    tokens.AccessTokenSecret = accessToken.TokenSecret;
                    // dane zostają zapisane w ustawieniach aplikacji
                    Properties.Settings.Default.AccessToken = accessToken.Token;
                    Properties.Settings.Default.AccessTokenSecret = accessToken.TokenSecret;
                    Properties.Settings.Default.Save();
                }
                else
                {
                    Environment.Exit(0);
                }
            }
        }
        public static OAuthTokens Tokens { get { return tokens; } }
    }
}
