﻿using Facebook;
using NguyenAnNinhConfession.FacebookHelpers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Security.Authentication.Web;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556

namespace NguyenAnNinhConfession.Views
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class SettingsPage : Page
    {
        bool IsLoggedIn = false;

        public SettingsPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        FacebookHelper ObjFBHelper = new FacebookHelper();
        private void btnFBLogin_Click(object sender, RoutedEventArgs e)
        {
            if (IsLoggedIn == false)
                ObjFBHelper.LoginAndContinue();
            if (IsLoggedIn == true)
                btnFBLogout_Click();
        }

        FacebookClient fbclient = new FacebookClient();
        public async void ContinueWithWebAuthenticationBroker(WebAuthenticationBrokerContinuationEventArgs args)
        {
            ObjFBHelper.ContinueAuthentication(args);
            if (ObjFBHelper.AccessToken != null)
            {
                fbclient = new Facebook.FacebookClient(ObjFBHelper.AccessToken);

                //Fetch facebook UserProfile:
                dynamic result = await fbclient.GetTaskAsync("me");
                string id = result.id;
                string email = result.email;
                string FBName = result.name;
                
                IsLoggedIn = true;
                btnFBLogin.Content = "Log out";
                txtBlockContent.Text = "You're now logged in as " + FBName;
                
                var msgDialog = new MessageDialog("Hello, " + FBName, "Login successful");
                await msgDialog.ShowAsync();
            }
            else
            {
                var msgDialog = new MessageDialog("Check your credentials and try again", "Login failed");
                await msgDialog.ShowAsync();
            }
        }

        Uri _logoutUrl;
        private void btnFBLogout_Click()
        {
            _logoutUrl = fbclient.GetLogoutUrl(new
            {
                next = "https://www.facebook.com/connect/login_success.html",
                access_token = ObjFBHelper.AccessToken
            });
            WebAuthenticationBroker.AuthenticateAndContinue(_logoutUrl);
        }
    }
}
