using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using TEAMModelOS.Filter;
using TEAMModelOS.SDK.DI;

namespace TEAMModelAPI.Controllers
{
    [ApiController]
    [Route("weather")]
    public class WeatherForecastController : ControllerBase
    {
        private readonly DingDing _dingDing;
        private readonly AzureStorageFactory _azureStorage;
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(DingDing dingDing, AzureStorageFactory azureStorage, ILogger<WeatherForecastController> logger)
        {
            _dingDing = dingDing;
            _azureStorage = azureStorage;
            _logger = logger;
        }

        [HttpGet]
        [ApiToken]
        public IEnumerable<WeatherForecast> Get()
        {
            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }

        /// <summary>
        /// 删除prefix  不管是内容 模块还是其他试题试卷 评测 问卷投票等都在使用。
        /// 
        /// {"cntr":"","prefix":"res/test"}
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("delete-prefix")]
        public async Task<IActionResult> DeletePrefix(JsonElement json)
        {
            string blobContainerName = null;
            string prefix = null;
            if (json.TryGetProperty("cntr", out JsonElement cntr) && cntr.ValueKind.Equals(JsonValueKind.String))
            {
                blobContainerName = $"{cntr}";
            }
            if (json.TryGetProperty("prefix", out JsonElement prefixjson) && prefixjson.ValueKind.Equals(JsonValueKind.String))
            {
                prefix = prefixjson.GetString();
            }
            if (prefix != null && blobContainerName != null)
            {
                var list = await _azureStorage.GetBlobContainerClient($"{cntr}").List(prefix);
                var status = await _azureStorage.GetBlobServiceClient().DeleteBlobs(_dingDing, blobContainerName, new List<string> { prefix });
                string u = "";
                string[] uls = System.Web.HttpUtility.UrlDecode($"{prefixjson}", Encoding.UTF8).Split("/");
                if (uls != null)
                {
                    u = !string.IsNullOrEmpty(uls[0]) ? uls[0] : uls[1];
                }
                return Ok(new { list });
            }
            else
            {
                return BadRequest();
            }
        }
    }
}
