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.Collections;

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

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

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

        [BindProperty]
        public Cat CatSelected { get; set; }

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

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

        private DbOps cManager = new();

        private OpenIDConnectUtils OpenIDUtils = new();

        private Cat categoryCookie;


        private const string cookieName = "CategoriesCookie";

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

        private void SetMyCookie()
        {
            string jsonResult = JsonConvert.SerializeObject(categoryCookie);
            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);
        }

        //



        private void DumpEnvironment()
        {
            Console.WriteLine("HEADER DUMP START");
            foreach (var e in HttpContext.Request.Headers)
            {
                Console.WriteLine(e.Key + ":" + e.Value);
            }
            Console.WriteLine("HEADER DUMP END");
            Console.WriteLine("ENVIRONMENT DUMP START");
            foreach (DictionaryEntry e in System.Environment.GetEnvironmentVariables())
            {
                Console.WriteLine(e.Key + ":" + e.Value);
            }
            Console.WriteLine("ENVIRONMENT DUMP END");
        }

        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");
        }

        public void Always()
        {
            Common();
            GetMyCookie();
            CatSelectList = cManager.MakeCList(UserID, "radioBtn", categoryCookie.CId);
            if (categoryCookie.CId != "")
            {
                CatSelected = categoryCookie;
            }
        }

        public IActionResult OnGet()
        {
            Always();
            if (ucnData.theRole != "teacher" && ucnData.theRole != "developer") return Redirect("/");
            return Page();
        }

        public IActionResult OnPost(string action = "na", string categoryParent = "/", string newCategory = "na", string target="na")
        {
            //tagPath = JsonConvert.DeserializeObject<List<Tags>>(theTagPath);
            Common();
            switch (action)
            {
                case "newCategory":
                    categoryCookie = cManager.SetCat(UserID, categoryParent, newCategory);
                    break;
                case "editCategory":
                    cManager.UpdateCatdescr(categoryParent, CatSelected.Descr);
                    break;
                case "removeCategory":
                    cManager.DeleteCategory(target);
                    break;
                default:
                    categoryCookie = cManager.GetCat(categoryParent);
                    break;
            }
            SetMyCookie();
            return Redirect("/Categories");
        }
    }
}
