using AI.Platform.Core;
using AI.Platform.Core.Entity.Media;
using AI.Platform.Page.Pages.Site.Model;
using Dm.util;
using Microsoft.AspNetCore.Components;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using System.Text.Json;

namespace AI.Platform.Page.Pages.Media.Model
{
    public class MediaModel
    {
    }

    /// <summary>
    /// 用于新增/修改广告时传递弹窗数据
    /// </summary>
    public class MediaDialogModel: IValidatableObject
    {
        /// <summary>
        /// 表id
        /// </summary>
        public long? Id { get; set; }

        /// <summary>
        /// 弹窗类型：1：新增；2：编辑；3：删除
        /// </summary>
        public int Type { get; set; }

        /// <summary>
        /// 集团ID
        /// </summary>
        public long GroupID { get; set; }

        /// <summary>
        /// 站点ID
        /// </summary>
        [Required(ErrorMessage = "请绑定油站")]
        public long BusinessUnitID { get; set; }

        /// <summary>
        /// 文件名
        /// </summary>
        [Required(ErrorMessage = "请上传广告以获取文件名")]
        public string FileName { set; get; }

        /// <summary>
        /// 文件后缀
        /// </summary>
        public string FileExtension { set; get; }

        /// <summary>
        /// guid文件名
        /// </summary>
        public string GuidFileName { set; get; }

        /// <summary>
        /// 保存到服务器的地址
        /// </summary>
        public string SavePath { set; get; }

        /// <summary>
        /// 文件开始播放时间
        /// </summary>
        [Required(ErrorMessage = "请设置广告播放时段")]
        public int? StartTime { set; get; }

        /// <summary>
        /// 文件结束播放时间
        /// </summary>
        [Required(ErrorMessage = "请设置广告播放时段")]
        public int? EndTime { set; get; }

        /// <summary>
        /// 文件生效时间
        /// </summary>
        [Required(ErrorMessage = "请设置广告策略有效时间段")]
        public DateTime? EffecitiveTime { set; get; }

        /// <summary>
        /// 文件失效时间
        /// </summary>
        public DateTime? FailureTime { set; get; }

        /// <summary>
        /// 文件油机状态
        /// </summary>
        [Required(ErrorMessage = "请选择播放该广告的油机状态")]
        public List<string> MachineStateList { set; get; } = new List<string>();

        /// <summary>
        /// 文件广告状态 0:禁用；1：可用
        /// </summary>
        [Required(ErrorMessage = "请设置广告状态")]
        public int MediaState { set; get; }

        /// <summary>
        /// 文件上传人
        /// </summary>
        public string MediaUploader { set; get; } = Global.CurrentUser.NickName;

        /// <summary>
        /// 备注
        /// </summary>
        public string Remark { set; get; }

        /// <summary>
        /// 可选择站点
        /// </summary>
        public List<SiteInfo> siteInfos { set; get; }

        

        public MediaEntity ToCompany()
        {
            var media = new MediaEntity()
            {
                LocalPath = SavePath,
                GroupId = this.GroupID,
                BusinessUnitId = this.BusinessUnitID,
                FileName = FileName,
                FileExtension = this.FileExtension,
                GuidFileName = this.GuidFileName,
                StartTime = this.StartTime ?? 0,
                EndTime = this.EndTime ?? 0,
                EffectiveTime = this.EffecitiveTime ?? DateTime.Now,
                FailureTime = this.FailureTime ?? DateTime.Now,
                MachineState = JsonSerializer.Serialize(getMachineStateList()),
                MediaState = this.MediaState,
                Uploader = this.MediaUploader ?? "",
                CreateTime = DateTime.Now,
                EditTime = DateTime.Now,
                Remark = this.Remark
            };
            if(this.Id != null) media.Id = (long)Id;
            return media;
        }

        /// <summary>
        /// 获取油机状态列表，为空则返回所有状态
        /// </summary>
        /// <returns></returns>
        public List<string> getMachineStateList()
        {
            if (MachineStateList.IsNullOrEmpty())
            {
                return getAllMachineStateList();
            }

            return MachineStateList;
        }

        /// <summary>
        /// 获取所有油枪状态
        /// </summary>
        /// <returns></returns>
        public List<string> getAllMachineStateList()
        {
            return new List<string>
                {
                    "idle","lock","offline","lift","authorised","start","fueling"
                };
        }

      
        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (MachineStateList.IsNullOrEmpty())
            {
                yield return new ValidationResult("请选择播放该广告的油机状态", new[] { nameof(MachineStateList) });
            }
            //if(MediaState == -1)
            //{
            //    yield return new ValidationResult("请设置广告状态", new[] { nameof(MediaState) });
            //}
            if (BusinessUnitID <= 0)
            {
                yield return new ValidationResult("请选择下发油站", new[] { nameof(BusinessUnitID) });
            }
        }
    }


    /// <summary>
    /// 用于显示广告列表数据
    /// </summary>
    public class MediaInfoModel
    {
        /// <summary>
        /// 表id
        /// </summary>
        public long? Id { get; set; }

        /// <summary>
        /// 集团ID
        /// </summary>
        public long GroupID { get; set; }

        /// <summary>
        /// 站点ID
        /// </summary>
        public long BusinessUnitID { get; set; }

        /// <summary>
        /// 站名
        /// </summary>
        public string SiteName { set; get; }

        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName { set; get; }

        /// <summary>
        /// 文件后缀
        /// </summary>
        public string FileExtension { set; get; }

        /// <summary>
        /// guid文件名
        /// </summary>
        public string GuidFileName { set; get; }

        /// <summary>
        /// 保存到服务器的地址
        /// </summary>
        public string SavePath { set; get; }

        /// <summary>
        /// 文件开始播放时间
        /// </summary>
        public int? StartTime { set; get; }

        /// <summary>
        /// 文件结束播放时间
        /// </summary>
        public int? EndTime { set; get; }

        /// <summary>
        /// 文件生效时间
        /// </summary>
        public DateTime EffecitiveTime { set; get; }

        /// <summary>
        /// 文件失效时间
        /// </summary>
        public DateTime FailureTime { set; get; }

        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime? EditTime { set; get; }
        /// <summary>
        /// 文件油机状态
        /// </summary>
        public string MachineStateList { set; get; }

        /// <summary>
        /// 文件广告状态 0:禁用；1：可用
        /// </summary>
        public int MediaState { set; get; }

        /// <summary>
        /// 文件上传人
        /// </summary>
        public string MediaUploader { set; get; } = Global.CurrentUser.NickName;

        /// <summary>
        /// 备注
        /// </summary>
        public string Remark { set; get; }

        public MediaType getType()
        {
            if(".mp4".equals(FileExtension) || ".MP4".equals(FileExtension))
            {
                return MediaType.VIDEO;
            } else
            {
                return MediaType.IMAGE;

            }
        }
    }

    public enum MediaType
    {
        VIDEO,IMAGE
    }

    /// <summary>
    /// 用于预览窗口
    /// </summary>
    public class PreviewInfo
    {
        /// <summary>
        /// 1：视频；2：图片
        /// </summary>
        public MediaType Type { set; get; }

        /// <summary>
        /// 地址
        /// </summary>
        public string Path { set; get; }
    }
}
