using DataVAPI.Tool.IOT;
using System;
using System.Collections.Generic;
using Tea;
using static AlibabaCloud.SDK.Iot20180120.Models.QueryDeviceFileResponseBody;


namespace BPASmartClient.IoT.Model
{
    public class Sample
    {

        /**
 * 使用AK&SK初始化账号Client
 * @param accessKeyId
 * @param accessKeySecret
 * @return Client
 * @throws Exception
 */
        public static AlibabaCloud.SDK.Iot20180120.Client CreateClient(string accessKeyId, string accessKeySecret)
        {
            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
                // 您的AccessKey ID
                AccessKeyId = accessKeyId,
                // 您的AccessKey Secret
                AccessKeySecret = accessKeySecret,
            };
            // 访问的域名
            config.Endpoint = "iot.cn-shanghai.aliyuncs.com";
            return new AlibabaCloud.SDK.Iot20180120.Client(config);
        }

        public static List<QueryDeviceFileResponseBodyData> FileQueryALL(string DeviceName)
        {
            List<QueryDeviceFileResponseBodyData> retdata = new List<QueryDeviceFileResponseBodyData>();
            AlibabaCloud.SDK.Iot20180120.Client client = CreateClient("LTAI5tDbUDuCvqU5hP8ZpHHV", "SvNIRQuz3BQFEaC3PFSJdgpeOKWFRi");
            AlibabaCloud.SDK.Iot20180120.Models.QueryDeviceFileListRequest queryDeviceFileListRequest = new AlibabaCloud.SDK.Iot20180120.Models.QueryDeviceFileListRequest
            {
                IotInstanceId = "iot-06z00g9pf3kwtxp",
                DeviceName = DataVClient.GetInstance().DeviceName,
                ProductKey = DataVClient.GetInstance().ProductKey,
            };
            AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
            AlibabaCloud.SDK.Iot20180120.Models.QueryDeviceFileListResponse resp = client.QueryDeviceFileListWithOptions(queryDeviceFileListRequest, runtime);

            AlibabaCloudRes cloudRes = Tools.JsonToObjectTools<AlibabaCloudRes>(AlibabaCloud.TeaUtil.Common.ToJSONString(resp.ToMap()));
            cloudRes?.body?.Data?.FileSummary?.ForEach(fileSummary =>
            {
                QueryDeviceFileResponseBodyData _data = FileQuery(DeviceName, fileSummary.FileId);
                if (_data != null)
                {
                    _data.UtcCreatedOn = DateTime.Parse(_data.UtcCreatedOn).AddHours(8).ToString("yyyy-MM-dd HH:mm:ss");
                    _data.DownloadUrl = _data.DownloadUrl.Replace("http://", "https://");
                    retdata.Add(_data);
                }
            });
            return retdata;
        }
        /// <summary>  
        /// 替换字符串起始位置(开头)中指定的字符串  
        /// </summary>  
        /// <param name="s">源串</param>  
        /// <param name="searchStr">查找的串</param>  
        /// <param name="replaceStr">替换的目标串</param>  
        /// <returns></returns>  
        public static string CutStarStr(string s, string searchStr, string replaceStr)
        {
            var result = s;
            try
            {
                if (string.IsNullOrEmpty(result))
                {
                    return result;
                }
                if (s.Length < searchStr.Length)
                {
                    return result;
                }
                if (s.IndexOf(searchStr, 0, searchStr.Length, StringComparison.Ordinal) > -1)
                {
                    result = s.Substring(searchStr.Length);
                }
                return result;
            }
            catch (Exception e)
            {
                return result;
            }
        }
        public static QueryDeviceFileResponseBodyData FileQuery(string DeviceName, string FileId)
        {
            AlibabaCloud.SDK.Iot20180120.Client client = CreateClient("LTAI5tDbUDuCvqU5hP8ZpHHV", "SvNIRQuz3BQFEaC3PFSJdgpeOKWFRi");
            AlibabaCloud.SDK.Iot20180120.Models.QueryDeviceFileRequest queryDeviceFileRequest = new AlibabaCloud.SDK.Iot20180120.Models.QueryDeviceFileRequest
            {
                IotInstanceId = "iot-06z00g9pf3kwtxp",
                DeviceName = DataVClient.GetInstance().DeviceName,
                ProductKey = DataVClient.GetInstance().ProductKey,
                FileId = FileId,
            };
            AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
            // 复制代码运行请自行打印 API 的返回值
            QueryDeviceFileResponseBodyData queryDeviceFile = client.QueryDeviceFileWithOptions(queryDeviceFileRequest, runtime).Body.Data;
            return queryDeviceFile;
        }

        public static bool? DeleteFile(string DeviceName, string FileId)
        {
            AlibabaCloud.SDK.Iot20180120.Client client = CreateClient("LTAI5tDbUDuCvqU5hP8ZpHHV", "SvNIRQuz3BQFEaC3PFSJdgpeOKWFRi");
            AlibabaCloud.SDK.Iot20180120.Models.DeleteDeviceFileRequest deleteDeviceFileRequest = new AlibabaCloud.SDK.Iot20180120.Models.DeleteDeviceFileRequest
            {
                IotInstanceId = "iot-06z00g9pf3kwtxp",
                DeviceName = DataVClient.GetInstance().DeviceName,
                ProductKey = DataVClient.GetInstance().ProductKey,
                FileId = FileId,
            };
            AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
            // 复制代码运行请自行打印 API 的返回值
            return client.DeleteDeviceFileWithOptions(deleteDeviceFileRequest, runtime).Body.Success;
        }

    }

    public class AlibabaCloudRes
    {
        public object headers { get; set; }
        public StrRes body { get; set; }
    }
    public class StrRes
    {
        public string code { get; set; }
        public string CurrentPage { get; set; }
        public ResData Data { get; set; }
        public string ErrorMessage { get; set; }
        public int PageCount { get; set; }
        public int PageSize { get; set; }
        public string RequestId { get; set; }
        public bool Success { get; set; }
        public int Total { get; set; }
    }
    public class ResData
    {
        public List<summary> FileSummary { get; set; }
    }
    public class summary
    {
        public string UtcCreatedOn { get; set; }
        public string Size { get; set; }
        public string FileId { get; set; }
        public string Name { get; set; }
    }
}
