using CrazyStudio.Core.Common.Eitities.Page;
using CrazyStudio.Core.Common.JWT;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using XPrintServer.Business.Dto;
using XPrintServer.Business.Services;
using XPrintServer.DataModel.Models;

namespace XPrintServer.Admin.Api.Controllers
{

    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ModelPartController : ControllerBase
    {

        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly ModelPartService _modelPartService;
        private readonly ActionLogService _actionLogService;

        public ModelPartController(IHttpContextAccessor httpContextAccessor, ModelPartService modelPartService, ActionLogService actionLogService)
        {
            this._httpContextAccessor = httpContextAccessor;
            this._modelPartService = modelPartService;
            this._actionLogService = actionLogService;
        }

        [HttpPost]
        [Authorize]
        public async Task<ActionResult> AddOrUpdateModelParts([FromBody] List<ModelPartDto> modelParts)
        {
            try
            {
                List<ModelPart> submitParts = new List<ModelPart>();
                for (int i = 0; i < modelParts.Count; i++)
                {
                    float.TryParse(modelParts[i].AlphaStr, out float alpha);
                    float.TryParse(modelParts[i].BetaStr, out float beta);
                    float.TryParse(modelParts[i].RadiusStr, out float radius);
                    modelParts[i].Alpha = alpha;
                    modelParts[i].Beta = beta;
                    modelParts[i].Radius = radius;
                    submitParts.Add(modelParts[i]);
                }
                await _modelPartService.AddOrUpdateModelParts(submitParts);
                return Ok(true);
            }
            catch (Exception ex)
            {
                await _actionLogService.AddActionLog(new ActionLog { Title = "AddOrUpdateModelParts Error", Content = ex.Message + " " + ex.StackTrace, DateTime = DateTime.Now });
                return StatusCode(500, ex.Message);
            }
        }


        [HttpPost]
        [Authorize]
        public async Task<ActionResult<ModelPartDto>> ModePartsFormModelId([FromBody] Guid Id)
        {
            try
            {
                var modelPartList = await _modelPartService.GetModePartsFormModelId(Id);
                return Ok(modelPartList);
            }
            catch (Exception ex)
            {
                await _actionLogService.AddActionLog(new ActionLog { Title = "ModePartsFormModelId Error", Content = ex.Message + " " + ex.StackTrace, DateTime = DateTime.Now });
                return StatusCode(500, ex.Message);
            }
        }
    }
}
