using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using XYY.Common.Standard;
using System.Linq;
namespace XYY.Authentication.Standard
{

    public static class UserContentCoreUnit
    {

        public static UserContent GetUserContent(this ClaimsPrincipal principal)
        {
            /*
             
                       new Claim(ClaimTypes.NameIdentifier,user.Id.ToString()),
                       new Claim(ClaimTypes.Name,user.NiceName),
                       new Claim(ClaimTypes.GroupSid,user.CustomerId.ToString()),
                       new Claim(ClaimTypes.Role,string.Join(",",user.Roles)),
                       new Claim(ClaimTypes.AuthorizationDecision,string.Join(",",user.PrivilegeUrl))
             */
            UserContent userContent = new UserContent();
            try
            {
                userContent.Id = int.Parse(principal.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value ?? "0");
                userContent.CustomerId = int.Parse(principal.Claims.FirstOrDefault(x => x.Type == ClaimTypes.GroupSid)?.Value ?? "0");
                userContent.NiceName = principal.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name)?.Value;
                userContent.IsAuthorization = true;
                userContent.Roles = principal.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Role)?.Value.Split(',').ToList();
                userContent.PrivilegeUrl = principal.Claims.FirstOrDefault(x => x.Type == ClaimTypes.AuthorizationDecision)?.Value.Split(',').ToList();
            }
            catch (Exception ex)
            {
                userContent.IsAuthorization = false;
            }
            return userContent;
        }
    }
}
