using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Facebook.Rest;
using Facebook.Schema;
using Facebook.Session;
using Facebook.Utility;

namespace FacebookMemoryPartI
{
    public partial class MainPage : UserControl
    {
        #region Constants

        private const string APPLICATION_KEY = "YOUR_APPLICATION_KEY";

        #endregion

        #region Private

        private Api _facebookApi;
        private user _currentUser;
        private readonly BrowserSession _browserSession;

        #endregion

        public MainPage()
        {
            InitializeComponent();

            _browserSession = new BrowserSession(APPLICATION_KEY);
            _browserSession.LoginCompleted += new EventHandler<AsyncCompletedEventArgs>(LoginCompleted);

            this.btnLogin.Click += new RoutedEventHandler(btnLogin_Click);
        }

        void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            _browserSession.Login();
            this.txtStatus.Text = "Logging in...";
        }

        void GetUserInfoCompleted(IList<user> userDetails, object state, FacebookException e)
        {
            if (e == null)
            {
                Dispatcher.BeginInvoke(() =>
                {
                    // set our user object
                    this._currentUser = userDetails[0];

                    // set user information data context
                    this.ucUserInformation.DataContext = _currentUser.current_location;
                    this.ucUserInformation.txtWelcome.Text = "Welcome " + _currentUser.name + "!";
                    this.ucUserInformation.imgProfile.Source = new BitmapImage(new Uri(_currentUser.pic_square));

                    this.txtStatus.Text = "";
                });
            }
            else
            {
                // if any errors occurred, invoke a call back to the UI thread and show the error message
                Dispatcher.BeginInvoke(() => MessageBox.Show("Error getting user info:\n\n" + e.Message));
            }
        }

        void GetLoggedInUserCompleted(long userId, object state, FacebookException e)
        {
            // check that no errors have occurred
            if (e == null)
            {
                Dispatcher.BeginInvoke(() => this.txtStatus.Text = "Get loggin in user complete. Getting user information...");

                // retrieve user information
                _facebookApi.Users.GetInfoAsync(userId, GetUserInfoCompleted, null);
            }
            else
            {
                // if any errors occurred, invoke a call back to the UI thread and show the error message
                Dispatcher.BeginInvoke(() => MessageBox.Show("Error getting logged in user:\n\n" + e.InnerException.Message));
            }
        }

        void LoginCompleted(object sender, AsyncCompletedEventArgs e)
        {
            // create new instance of facebook api
            _facebookApi = new Api(_browserSession);

            // retrieve the logged in user
            _facebookApi.Users.GetLoggedInUserAsync(GetLoggedInUserCompleted, null);
        }
    }
}
