using Azure;
using Azure.Cosmos;
using HTEXLib.COMM.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TEAMModelOS.SDK.DI;
using TEAMModelOS.SDK.Extension;
using TEAMModelOS.SDK.Models;
using TEAMModelOS.SDK.Models.Cosmos.Common;

namespace TEAMModelOS.Services.Common
{
    public static class SheetService
    {
        public static async Task<string> genSheetId(CosmosClient client , DingDing _dingDing, TEAMModelOS.Models. Option _option, string sheetCode, string tbname)
        {
            string _num09 = "0123456789";
            string id = $"{Utils.CreatSaltString(7, _num09)}";
            for (int i = 0; i < 10; i++)
            {
                Response response = await client.GetContainer(Constant.TEAMModelOS, tbname).ReadItemStreamAsync($"{id}", new PartitionKey(sheetCode)); 
                if (response.Status == 404)
                {
                    break;
                }
                else
                {
                    if (i == 9)
                    {
                        string msg = "common/SheetConfig/upsert()\n id生成异常，重复生成次数超过10次";
                        await _dingDing.SendBotMsg($"OS，{_option.Location}，{msg}", GroupNames.醍摩豆服務運維群組);
                        throw new Exception(msg);
                    }
                    else
                    {
                        id = $"{Utils.CreatSaltString(7, _num09)}";
                    }
                }
            }
            return id;
        }
        public static async Task<string> genSheetNo(CosmosClient client, DingDing _dingDing, TEAMModelOS.Models.Option _option, string sheetCode, string tbname,string  from)
        {
            string _num09 = "0123456789";
            string no = $"{Utils.CreatSaltString(7, _num09)}";
            for (int i = 0; i < 10; i++)
            {
                List<SheetConfig> sheets = new List<SheetConfig>();
                bool hasCurrFrom = false;
                await foreach (var item  in  client.GetContainer(Constant.TEAMModelOS, tbname).GetItemQueryIterator<SheetConfig>(
                    queryText: $"select value(c) from  c where c.no='{no}'",
                    requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey(sheetCode) })) 
                {
                    hasCurrFrom = true;
                    break;
                }
                if (hasCurrFrom)
                {
                    if (i == 9)
                    {
                        string msg = "common/SheetConfig/upsert()\n id生成异常，重复生成次数超过10次";
                        await _dingDing.SendBotMsg($"OS，{_option.Location}，{msg}", GroupNames.醍摩豆服務運維群組);
                        throw new Exception(msg);
                    }
                    else
                    {
                        no = $"{Utils.CreatSaltString(7, _num09)}";
                    }
                }
                else { break; }
            }
            return no;
        }
    }
}
