using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using McKesson.PPS.Fusion.Authentication.Business;
using Pluto.Api;
using McKesson.PPS.Fusion.Business.Common.Context;

namespace PlutoServer.PracticeChoice.Security {
    public class Authentication {
        public UserInfo1 Login(string userName, string password, int siteId) {
            UserInfo1 userInfo = null;

            var identity = MagicIdentity.GetIdentity(userName, password, siteId, string.Empty);
            if(identity.IsAuthenticated) {
                //MagicPrincipal principal = new MagicPrincipal(identity);
                //Csla.ApplicationContext.User = principal;
                var userContext = UserContext.GetInstance();
                userInfo = new UserInfo1() {
                    AuthToken = identity.ToCookieString(),
                    FirstName = userContext.FirstName,
                    Id = userContext.PersonId.ToString(),
                    IsProvider = userContext.IsProvider,
                    LastName = userContext.LastName,
                    UserName = userName 
                };    
            } else {
                throw new Exception(string.Format("You are not authorized. Error{0}", identity.UserLoginError));
            }
            return userInfo;
        }
    }
}
