using Furion.DataValidation;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Waste.Domain;

namespace Waste.Application
{

    public class DeviceList : W_Device
    {
        /// <summary>
        /// 商户名称
        /// </summary>
        public string BusinessName { get; set; }
        /// <summary>
        /// ICCID
        /// </summary>
        public string ICCID { get; set; } = "";
        /// <summary>
        /// IMEI
        /// </summary>
        public string IMEI { get; set; } = "";
        /// <summary>
        /// IMSI
        /// </summary>
        public string IMSI { get; set; } = "";
        /// <summary>
        /// 今日测量次数
        /// </summary>
        public int TodayCount { get; set; } = 0;
        /// <summary>
        /// 累计测量次数
        /// </summary>
        public int TotalCount { get; set; } = 0;
        /// <summary>
        /// 今日测量重量
        /// </summary>
        public decimal TodayWeight { get; set; } = 0;
        /// <summary>
        /// 累计测量重量
        /// </summary>
        public decimal TotalWeight { get; set; } = 0;
        /// <summary>
        /// 最近心跳时间
        /// </summary>
        public DateTime? LastBeatTime { get; set; }
        /// <summary>
        /// 网络状态,0-离线,1-在线,规则:最新测量时间、最近心跳时间在16分钟之内则是在线
        /// </summary>
        public int NetStatus { get; set; } = 0;
        /// <summary>
        /// 信号强度,分为5格信号,算法为:31/6
        /// </summary>
        public string sign { get; set; }
        /// <summary>
        /// 经度
        /// </summary>
        public string Longitude { get; set; } = "";

        /// <summary>
        /// 纬度
        /// </summary>
        public string Latitude { get; set; } = "";
    }
    /// <summary>
    /// 设备批量操作
    /// </summary>
    public class DeviceBatchModel
    {
        /// <summary>
        /// 服务商ID
        /// </summary>
        public Guid BusinessId { get; set; }
        /// <summary>
        /// 操作类型,1-分配,2-回收
        /// </summary>
        public int type { get; set; }
        /// <summary>
        /// 设备ID列表
        /// </summary>
        public List<Guid> codes { get; set; }
    }
    /// <summary>
    /// 设备信息提交
    /// </summary>
    public class DeviceSubmit : W_Device
    {
        /// <summary>
        /// 设备对应的SecretHash
        /// </summary>
        public string SecretHash { get; set; }
        /// <summary>
        /// 设备对应的DevId
        /// </summary>
        public string DevId { get; set; }
        /// <summary>
        /// 设备对应的Secret
        /// </summary>
        public string Secret { get; set; }
    }
    /// <summary>
    /// 设备详情
    /// </summary>
    public class DeviceDetailS2Dto : W_Device
    {
        /// <summary>
        /// 最近心跳时间
        /// </summary>
        public string LastBeatTime { get; set; }
        /// <summary>
        /// 最近开机时间
        /// </summary>
        public string LastStartTime { get; set; }
        /// <summary>
        /// 纬度
        /// </summary>
        public string Latitude { get; set; }
        /// <summary>
        /// 经度
        /// </summary>
        public string Longitude { get; set; }
        /// <summary>
        /// 使用的版本号
        /// </summary>
        public string version { get; set; }
    }
    /// <summary>
    /// 设备配置
    /// </summary>
    public class DeviceConfigC2SDto : IValidatableObject
    {
        /// <summary>
        /// 设备ID
        /// </summary>
        public Guid Id { get; set; }

        /// <summary>
        /// 推送地址,支持http/https
        /// </summary>
        [Required(ErrorMessage = "推送地址不可为空")]
        [MaxLength(200, ErrorMessage = "推送地址最多200个字")]
        public string Url { get; set; }

        /// <summary>
        /// 额外推送信息,推送时固定以body参数传递
        /// </summary>
        [DataValidation(AllowNullValue = true)]
        [MaxLength(100, ErrorMessage = "额外推送信息最多100个字")]
        public string Body { get; set; }
        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="validationContext"></param>
        /// <returns></returns>
        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (!string.IsNullOrEmpty(Body) && (Body.Contains("&") || Body.Contains("?")))
            {
                yield return new ValidationResult("额外推送信息不可包含特殊字符&、?", new[] { nameof(Body) });
            }
            if(!Regex.IsMatch(Url.ToLower(), @"[http][a-zA-z]+://[^\s]*"))
            {
                yield return new ValidationResult("推送地址格式不正确", new[] { nameof(Url) });
            }
        }
    }
}
