﻿// ***********************************************************************
// Assembly         : CoBuilder.Common
// Author           : Alex Bradley
// Created          : 04-19-2016
//
// Last Modified By : Alex Bradley
// Last Modified On : 07-20-2016
// ***********************************************************************
// <copyright file="SignInManager.cs" company="AB Consulting">
//     Copyright © AB Consulting 2016
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Diagnostics;
using CoBuilder.Common.Client;
using CoBuilder.Common.Interfaces;

namespace CoBuilder.Common.Logic
{
    /// <summary>
    /// Class SignInManager.
    /// </summary>
    public class SignInManager
    {
        /// <summary>
        /// The _active session
        /// </summary>
        private readonly ISession _activeSession;
        /// <summary>
        /// The _client
        /// </summary>
        private readonly CoBuilderClient _client;

        /// <summary>
        /// Initializes a new instance of the <see cref="SignInManager"/> class.
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="activeSession">The active session.</param>
        public SignInManager(CoBuilderClient client, ISession activeSession)
        {
            _client = client;
            _activeSession = activeSession;
        }

        /// <summary>
        /// Signs the in.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <returns>SignInResult.</returns>
        public SignInResult SignIn(string username, string password)
        {
            _activeSession.CurrentUser.Username = username;
            _activeSession.CurrentUser.Password = password;

            return SignIn();
        }

        /// <summary>
        /// Signs the in.
        /// </summary>
        /// <returns>SignInResult.</returns>
        public SignInResult SignIn()
        {
            try
            {
                var loginResult = _client.Login();

                if (loginResult.Status != AuthenticationRequestStatus.Ok)
                {
                    Debug.WriteLine(loginResult.Status.ToString());
                    return loginResult.Status == AuthenticationRequestStatus.Error
                        ? SignInResult.Error
                        : SignInResult.Failed;
                }

                var user = _activeSession.CurrentUser;

                if (user == null)
                {
                    Debug.WriteLine("Error Casting user");
                    return SignInResult.Error;
                }

                user.AccessToken = loginResult.TokenData;
                user.AdminName = loginResult.AdminName;
                user.CanWrite = loginResult.CanWrite;
                user.CompanyName = loginResult.CompanyName;
                user.ContactId = loginResult.ContactId;
                return SignInResult.Succeeded;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                return SignInResult.Error;
            }
        }

        /// <summary>
        /// Represents an event that is raised when the sign-out operation is complete.
        /// </summary>
        public void SignOut()
        {
            CoBuilderService.Close();
        }
    }
}