using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using TEAMModelOS.SDK.Extension.JwtAuth.JwtHelper;
using TEAMModelOS.SDK.Extension.JwtAuth.Models;
using TEAMModelOS.SDK.Context.Constant.Common;
using System.Security.Claims;

namespace TEAMModelOS.Controllers
{
    public class BaseController : Controller
    {
        public List<string> GetLoginUser(string claimType)
        {
            var tokenHeader = "";
            HttpRequest request = HttpContext.Request;
            if (request.Headers.ContainsKey(Constants.AUTHORIZATION))
            {
                tokenHeader = request.Headers[Constants.AUTHORIZATION];
                //tokenHeader = tokenHeader.ToString().Substring("Bearer ".Length).Trim();
                tokenHeader = tokenHeader.Replace("Bearer ", "");
            }
            if (request.Query.ContainsKey(Constants.ACCESS_TOKEN))
            {
                tokenHeader = request.Query[Constants.ACCESS_TOKEN];
                tokenHeader = tokenHeader.Trim();
            }
            if (string.IsNullOrEmpty(tokenHeader))
            {
                return null;
            }
            ClaimModel claimModel = JwtHelper.SerializeJWT(tokenHeader);
            claimModel.Claim.TryGetValue(claimType, out var claimValue);
            List<string> claimValues = new List<string>();
            foreach (Claim claim in claimModel.Claims)
            {
                if (claim.Type.Equals(claimType))
                {
                    claimValues.Add(claim.Value);
                }
            }
            return claimValues;
        }
    }
}
