using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using Quizzer.Extensions;
using Quizzer.Implementations;
using Quizzer.Models;
using Quizzer.OpenIDConnect;
using Quizzer.Persistens;
using System.Diagnostics;
using System.IdentityModel.Tokens.Jwt;

namespace Quizzer.Pages
{
    public class CoverageModel : PageModel
    {
        [BindProperty] 
        public List<Coverage>? registrations { get; set; }
        [BindProperty]
        public List<string>? dates { get; set; }
        [BindProperty]
        public List<Coverage>? persons { get; set; }
        [BindProperty]
        public List<string>? theClasses { get; set; }
        [BindProperty]
        public string? currentClass { get; set; }
        [BindProperty]
        public string myFullName { get; set; }

        public OpenIDUCNData ucnData { get; set; }
        private bool isDebug;
        public string UserID { get; set; }
        private string KCUserID { get; set; }



        private DbOps dbMgr = new();
        private OpenIDConnectUtils OpenIDUtils = new();

        public void Common()
        {
            string? ucnDataJson = OpenIDUtils.GetJwtClaim(HttpContext, "ucn");

            KCUserID = OpenIDUtils.GetJwtClaim(HttpContext, "sub");
            ucnData = JsonConvert.DeserializeObject<OpenIDUCNData>(ucnDataJson);
            UserID = OpenIDUtils.GetJwtClaim(HttpContext, "email").HashSHA256();
            myFullName = OpenIDUtils.GetJwtClaim(HttpContext, "name");
        }

        private void GetMyCookie()
        {
            currentClass = HttpContext.Request.Cookies["CoverageCookie"];
            SetMyCookie();
        }

        private void SetMyCookie()
        {
            Response.Cookies.Append("CoverageCookie", currentClass, new Microsoft.AspNetCore.Http.CookieOptions
            {
                Secure = true,
                HttpOnly = true,
                SameSite = Microsoft.AspNetCore.Http.SameSiteMode.None,
            });
        }

        public IActionResult OnGet()
        {
            Common();
            if (ucnData.theRole != "teacher" && ucnData.theRole != "developer") return Redirect("/");
            currentClass = "";
            // List of classes
            KeyCloak KCApi = new KeyCloak();
            List<KCGroups> myGroups = KCApi.KeyCloakGetUsersGroups(KCUserID);
            theClasses = new();
            foreach (var group in myGroups)
            {
                if (group.name.Contains("-CSD-")) theClasses.Add(group.name);
            }
            SetMyCookie();
            return Page();
        }

        public IActionResult OnPost(string action = "na", string selectedClass = "")
        {
            Common();
            if (ucnData.theRole != "teacher" && ucnData.theRole != "developer") return Redirect("/");
            if ( selectedClass != "")
            {
                currentClass= selectedClass;
                SetMyCookie();
                registrations = dbMgr.GetCoverage(currentClass);
                // Ascending list of dates
                dates = dbMgr.GetCoverageDates(currentClass);
                // Ascending combined list of persons attending
                persons = dbMgr.GetCoveragePersons(currentClass);
                // List of classes
                KeyCloak KCApi = new KeyCloak();
                List<KCGroups> myGroups = KCApi.KeyCloakGetUsersGroups(KCUserID);
                theClasses = new();
                foreach (var group in myGroups)
                {
                    if (group.name.Contains("-CSD-")) theClasses.Add(group.name);
                }
                return Page();
            }
            else return Redirect("/");
        }
    }
}
