using AipGateway.AIP;
using AipGateway.API.Application.Modules;
using AipGateway.API.Domain.IRepositories.IGenericRepositories;
using AipGateway.API.Domain.Models;
using AipGateway.API.Domain.Models.Request;
using AipGateway.API.Domain.Models.Response;
using AipGateway.API.Extensions;
using AipGateway.API.Services;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;

namespace AipGateway.API.Controllers
{
    [ApiController]
    [Route("v1/aip-api/stream")]
    [Produces("application/json")]
    public class ApiStreamController : BaseModule
    {
        private readonly ILogger<ApiStreamController> _log;
        private readonly IApiStreamService _apiStreamService;
        public ApiStreamController(
            ILogger<ApiStreamController> logger, 
            IUnitOfWork unitOfWork, 
            IWebHostEnvironment webHostEnvironment,
            IApiStreamService apiStreamService)
            : base(unitOfWork, webHostEnvironment)
        {
            _log = logger;
            _apiStreamService = apiStreamService;
            _log.LogError("ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo: ApiStreamController");
        }


        [HttpPost("info")]
        [SwaggerResponse(200, type: typeof(SuccessResponseModel<ResponseInfo>))]
        public async Task<IResult> GetInfo([FromBody] RequestStream req)
        {
            GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_INFO, req.apiKey);
            req.apiGuid = GlobalConstants.GetApiGuid(HttpContext);
            HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
            return await CreateResponseAsync(async () =>
            {
                int authError = ContainerService.ValidationApiAuthorization(HttpContext, req.apiKey);
                if (authError != 0)
                {
                    throw ContainerService.CreateValidationApiAuthorizationAsync(HttpContext, authError);
                }
                var response = await _apiStreamService.GetInfo(req);
                var data = new SuccessResponseModel<ResponseInfo>()
                {
                    Message = GlobalConstants.API_RESULT_SUCCESS,
                    Result = response,
                    StatusCode = System.Net.HttpStatusCode.OK,
                    Success = true
                };
                var result = Results.Ok(data);
                HttpContext.Items.Add(GlobalConstants.API_RESULT, (ResponseBase)data.Result);
                return result;
            });
        }

        [HttpPost("set-label")]
        [SwaggerResponse(200, type: typeof(SuccessResponseModel<ResponseStream>))]
        public async Task<IResult> SetLabel([FromBody] RequestStreamSet req)
        {
            GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_SET_LABEL, req.apiKey);
            req.apiGuid = GlobalConstants.GetApiGuid(HttpContext);
            HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
            return await CreateResponseAsync(async () =>
            {
                int authError = ContainerService.ValidationApiAuthorization(HttpContext, req.apiKey);
                if (authError != 0)
                {
                    throw ContainerService.CreateValidationApiAuthorizationAsync(HttpContext, authError);
                }
                var response = await _apiStreamService.SetLabel(req);
                var data = new SuccessResponseModel<ResponseStream>()
                {
                    Message = GlobalConstants.API_RESULT_SUCCESS,
                    Result = response,
                    StatusCode = System.Net.HttpStatusCode.OK,
                    Success = true
                };
                var result = Results.Ok(data);
                HttpContext.Items.Add(GlobalConstants.API_RESULT, (ResponseBase)data.Result);
                return result;
            });
        }

        [HttpPost("delete-label")]
        [SwaggerResponse(200, type: typeof(SuccessResponseModel<ResponseStream>))]
        public async Task<IResult> DelLabel([FromBody] RequestStreamDel req)
        {
            GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_DELETE_LABEL, req.apiKey);
            req.apiGuid = GlobalConstants.GetApiGuid(HttpContext);
            HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
            return await CreateResponseAsync(async () =>
            {
                int authError = ContainerService.ValidationApiAuthorization(HttpContext, req.apiKey);
                if (authError != 0)
                {
                    throw ContainerService.CreateValidationApiAuthorizationAsync(HttpContext, authError);
                }
                var response = await _apiStreamService.DelLabel(req);
                var data = new SuccessResponseModel<ResponseStream>()
                {
                    Message = GlobalConstants.API_RESULT_SUCCESS,
                    Result = response,
                    StatusCode = System.Net.HttpStatusCode.OK,
                    Success = true
                };
                var result = Results.Ok(data);
                HttpContext.Items.Add(GlobalConstants.API_RESULT, (ResponseBase)data.Result);
                return result;
            });
        }

        [HttpPost("set-protection")]
        [SwaggerResponse(200, type: typeof(SuccessResponseModel<ResponseStream>))]
        public async Task<IResult> SetProtection([FromBody] RequestStreamSet req)
        {
            GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_SET_PROTECTION, req.apiKey);
            req.apiGuid = GlobalConstants.GetApiGuid(HttpContext);
            HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
            return await CreateResponseAsync(async () =>
            {
                int authError = ContainerService.ValidationApiAuthorization(HttpContext, req.apiKey);
                if (authError != 0)
                {
                    throw ContainerService.CreateValidationApiAuthorizationAsync(HttpContext, authError);
                }
                var response = await _apiStreamService.SetProtection(req);
                var data = new SuccessResponseModel<ResponseStream>()
                {
                    Message = GlobalConstants.API_RESULT_SUCCESS,
                    Result = response,
                    StatusCode = System.Net.HttpStatusCode.OK,
                    Success = true
                };
                var result = Results.Ok(data);
                HttpContext.Items.Add(GlobalConstants.API_RESULT, (ResponseBase)data.Result);
                return result;
            });
        }

        [HttpPost("delete-protection")]
        [SwaggerResponse(200, type: typeof(SuccessResponseModel<ResponseStream>))]
        public async Task<IResult> DelProtection([FromBody] RequestStreamDel req)
        {
            GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_DELETE_PROTECTIN, req.apiKey);
            req.apiGuid = GlobalConstants.GetApiGuid(HttpContext);
            HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
            return await CreateResponseAsync(async () =>
            {
                int authError = ContainerService.ValidationApiAuthorization(HttpContext, req.apiKey);
                if (authError != 0)
                {
                    throw ContainerService.CreateValidationApiAuthorizationAsync(HttpContext, authError);
                }
                var response = await _apiStreamService.DelProtection(req);
                var data = new SuccessResponseModel<ResponseStream>()
                {
                    Message = GlobalConstants.API_RESULT_SUCCESS,
                    Result = response,
                    StatusCode = System.Net.HttpStatusCode.OK,
                    Success = true
                };
                var result = Results.Ok(data);
                HttpContext.Items.Add(GlobalConstants.API_RESULT, (ResponseBase)data.Result);
                return result;
            });
        }


        [HttpPost("set-label-protection")]
        [SwaggerResponse(200, type: typeof(SuccessResponseModel<ResponseStream>))]
        public async Task<IResult> SetLabelProtection([FromBody] RequestStreamAllSet req)
        {
            GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_SET_LABEL_PROTECTION, req.apiKey);
            req.apiGuid = GlobalConstants.GetApiGuid(HttpContext);
            HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
            return await CreateResponseAsync(async () =>
            {
                int authError = ContainerService.ValidationApiAuthorization(HttpContext, req.apiKey);
                if (authError != 0)
                {
                    throw ContainerService.CreateValidationApiAuthorizationAsync(HttpContext, authError);
                }
                var response = await _apiStreamService.SetLabelProtection(req);
                var data = new SuccessResponseModel<ResponseStream>()
                {
                    Message = GlobalConstants.API_RESULT_SUCCESS,
                    Result = response,
                    StatusCode = System.Net.HttpStatusCode.OK,
                    Success = true
                };
                var result = Results.Ok(data);
                HttpContext.Items.Add(GlobalConstants.API_RESULT, (ResponseBase)data.Result);
                return result;
            });
        }

        [HttpPost("delete-label-protection")]
        [SwaggerResponse(200, type: typeof(SuccessResponseModel<ResponseStream>))]
        public async Task<IResult> DelLabelProtection([FromBody] RequestStreamDel req)
        {
            GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_DELETE_LABEL_PROTECTION, req.apiKey);
            req.apiGuid = GlobalConstants.GetApiGuid(HttpContext);
            HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
            return await CreateResponseAsync(async () =>
            {
                int authError = ContainerService.ValidationApiAuthorization(HttpContext, req.apiKey);
                if (authError != 0)
                {
                    throw ContainerService.CreateValidationApiAuthorizationAsync(HttpContext, authError);
                }
                var response = await _apiStreamService.DelLabelProtection(req);
                var data = new SuccessResponseModel<ResponseStream>()
                {
                    Message = GlobalConstants.API_RESULT_SUCCESS,
                    Result = response,
                    StatusCode = System.Net.HttpStatusCode.OK,
                    Success = true
                };
                var result = Results.Ok(data);
                HttpContext.Items.Add(GlobalConstants.API_RESULT, (ResponseBase)data.Result);
                return result;
            });
        }

    }
}
