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

namespace Quizzer.Pages
{
    public class StatisticsModel : PageModel
    {
        [BindProperty]
        public OpenIDUCNData ucnData { get; set; }

        [BindProperty]
        public string UserID { get; set; }

        [BindProperty]
        public List<QuizzDN> quizzList { get; set; }

        [BindProperty]
        public StatisticsMCDN PageStatistics { get; set; }

        [BindProperty]
        public string myFullName { get; set; }


        private bool isDebug;
        private string KCUserID { get; set; }

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


        public string statisticsCookie { get; set; }

        private const string cookieName = "StatisticsCookie";

        private void GetMyCookie()
        {
            string jsonString = HttpContext.Request.Cookies[cookieName];
            if (jsonString != null)
            {
                statisticsCookie = JsonConvert.DeserializeObject<string>(jsonString);
                return;
            }
            // Here if no cookie found
            statisticsCookie = "";
            // Save the new cookie
            SetMyCookie();
        }

        private void SetMyCookie()
        {
            string jsonResult = JsonConvert.SerializeObject(statisticsCookie);
            Response.Cookies.Append(cookieName, jsonResult, new Microsoft.AspNetCore.Http.CookieOptions
            {
                Secure = true,
                HttpOnly = true,
                SameSite = Microsoft.AspNetCore.Http.SameSiteMode.None,
            });
        }

        private void RemoveMyCookie()
        {
            Response.Cookies.Delete(cookieName);
        }


        //


        public void Common()
        {
            isDebug = MyConfiguration.Get()["DEBUG"] == "Y";
            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");
        }


        public IActionResult OnGet()
        {
            Common();
            if (ucnData.theRole != "teacher" && ucnData.theRole != "developer") return Redirect("/");
            GetMyCookie();
            quizzList = dbManager.GetQuizzesTeacher(UserID);
            if (statisticsCookie != "")
            {
                // traverse all statistics
                PageStatistics = dbManager.GetStatisticsMCDN(statisticsCookie);
            }
            else
            {
                PageStatistics = null;
            }
            return Page();
        }

        public IActionResult OnPost(string action = "na", string changeTo = "na")
        {
            Common();
            GetMyCookie();
            switch (action)
            {
                case "selectQuizz":
                    statisticsCookie = changeTo;
                    break;
                case "delete":
                    dbManager.DeleteStatistics(changeTo);
                    break;
                case "na":
                    break;
                default:
                    break;
            }
            SetMyCookie();
            return Redirect("/Statistics");
        }
    }
}
