using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using WK_KLI_LAPS_Service.App_Data;
using WK_KLI_LAPS_Service.Models;

namespace WK_KLI_LAPS_Service.Controllers.UserManagement.StageMapping
{
    [Route("api/[controller]")]
    [ApiController]
    public class TopBarMenuApiController : ControllerBase
    {
        CommonRepository oCommonRepository = new CommonRepository();
        private ITopBarMenu _topbarmenuservice;
        private readonly IConfiguration _config;
        public TopBarMenuApiController(ITopBarMenu topbarmenuservice, IConfiguration config)
        {
            _topbarmenuservice = topbarmenuservice;
            _config = config;
        }

        [Authorize]
        [HttpPost("GetUserPreferences")]
        public IActionResult GetUserPreferences(userpreferenceModel userpreference)
        {
            try
            {
                var result = _topbarmenuservice.GetUserPreferences(userpreference);
               return Ok(result);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
                //throw;
            }
          

        }
        [Authorize]
        [HttpPost("AddUserPreferences")]
        public IActionResult AddUserPreferences(userpreferenceModel userpreference)
        {
            try
            {
                var result = _topbarmenuservice.AddUserPreferences(userpreference);
                return Ok(result);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
                //throw;
            }


        }
        [Authorize]
        [HttpPost("UpadateUserPreferences/{updatetype}")]
        public IActionResult UpadateUserPreferences(string updatetype, userpreferenceModel userpreference)
        {
            try
            {
                var result = _topbarmenuservice.UpadateUserPreferences(userpreference, updatetype);
                return Ok(result);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
                //throw;
            }


        }

        [Authorize]
        [HttpGet("GetOptionsList")]
        public IActionResult GetOptionsList()
        {
            try
            {

                var array = _config.GetSection("Options").GetChildren().ToList();
                //.Select(configSection => configSection.Value);

                List<object> options = new List<object>();

                for (int i = 0; i < array.Count; i++)
                {
                    var id = _config.GetValue<int>(array[i].Path + ":optionid");
                    var option = _config.GetValue<string>(array[i].Path + ":option");
                    options.Add(new { optionid = id, option = option });
                }

                //_config.GetValue<string>("AppSettings:Secret");

                return Ok(options);
            }
            catch (Exception ex)
            {
                return BadRequest(new { message = ex.Message });
            }

        }



    }
}