using System.Data;
using System.Text.RegularExpressions;
using PowerEasy.WebFuture.DataMigrations.Foundation.Enum;
using PowerEasy.WebFuture.DataMigrations.Foundation.Service;
using PowerEasy.WebFuture.DataMigrations.Foundation.Utilities;
using PowerEasy.WebFuture.DataMigrations.MigrationProgramme.SiteFactoryProgramme.Model;

namespace PowerEasy.WebFuture.DataMigrations.MigrationProgramme.SiteFactoryProgramme
{
    /// <summary>
    /// 迁移视频。
    /// </summary>
    public class VideoMigration : BaseContentMoldDataMigrationItem
    {
        /// <summary>
        /// 模型名字。
        /// </summary>
        public override string ModelName { get; set; } = "视频";

        /// <summary>
        /// 迁移项目的排序，用于执行和工具显示的顺序，从小到大排序。
        /// </summary>
        public override int Order { get; set; } = 4;

        /// <summary>
        /// 迁移项目名称。
        /// </summary>
        public override string MigrationItemName { get; set; } = "迁移视频";

        /// <summary>
        /// 目标数据库表名。
        /// </summary>
        public override string TargetDatabaseTableName { get; set; } = "PE_ContentManage_Video";

        /// <summary>
        /// 来源数据库表的主键名称。
        /// </summary>
        public override string SourcePrimaryKeyName { get; set; } = "GeneralID";

        /// <summary>
        /// 来源数据库表的节点名称。
        /// </summary>
        public override string SourceNodeIdName { get; set; } = "NodeID";

        /// <summary>
        /// 上传目录。
        /// </summary>
        public override string UploadPath { get; set; } = "$/History";

        /// <summary>
        /// 目标站点id。
        /// </summary>
        public override int SiteId { get; set; } = 1;

        /// <summary>
        /// 获取来源要迁移的数据集合。
        /// </summary>
        /// <param name="newContentId">最新迁移的数据主键。</param>
        public override void LoadSourceDataTable(int newContentId)
        {
            // 获取源数据集合。
            var sql = $"select top 10000 * from PE_CommonModel as cm left join PE_U_Video as a on cm.ItemID=a.ID where GeneralID > {newContentId} and TableName='PE_U_Video' and Title!='' order by GeneralID";

            this.SourceDataTable = DatabaseService.GetSourceDatabaseProvider(this.MigrationProgrammeIdentifier).ExecuteDataTable(sql);
        }

        /// <summary>
        /// 字段映射。（没有赋值的属性都使用了Video实体中的默认值，如果需要，请在本方法重新赋值。）
        /// </summary>
        /// <param name="sourceDataRow">源数据表行数据。</param>
        /// <param name="targetDataRow">目标数据表行数据。</param>
        /// <returns>返回映射后的行数据。</returns>
        public override DataRow MapFields(DataRow sourceDataRow, DataRow targetDataRow)
        {
            var video = new Video();

            // MoldId具体值请去PE_ContentManage_ContentMold表查看具体MoldId值。
            video.MoldId = 3;
            video.BelongSiteId = this.SiteId;
            video.ExtendContent = "<ExtendContent />";
            video.Status = Status.PendingAudit;

            video.HistoryId = MigrateHelper.GetColumnValueInt(sourceDataRow, this.SourcePrimaryKeyName);

            video.Title = MigrateHelper.GetColumnValueString(sourceDataRow, "Title");

            video.Subheading = MigrateHelper.GetColumnValueString(sourceDataRow, "Subheading");

            video.TitleIntact = MigrateHelper.GetColumnValueString(sourceDataRow, "TitleIntact");

            video.Intro = MigrateHelper.GetColumnValueString(sourceDataRow, "Synopsis");

            video.Keyword = MigrateHelper.GetColumnValueString(sourceDataRow, "Keyword");

            video.CopyFrom = MigrateHelper.GetColumnValueString(sourceDataRow, "CopyFrom");

            video.FeaturedImage = MigrateHelper.AddUploadPathFlag(MigrateHelper.GetColumnValueString(sourceDataRow, "cover"), this.UploadPath);

            video.Hits = MigrateHelper.GetColumnValueInt(sourceDataRow, "Hits");

            video.PublishTime = MigrateHelper.GetColumnValueDateTime(sourceDataRow, "UpdateTime");

            video.CreateTime = MigrateHelper.GetColumnValueDateTime(sourceDataRow, "InputTime");

            video.Priority = MigrateHelper.GetColumnValueInt(sourceDataRow, "Priority");

            video.UpdateTime = MigrateHelper.GetColumnValueDateTime(sourceDataRow, "UpdateTime");

            video.StatusUpdateTime = MigrateHelper.GetColumnValueDateTime(sourceDataRow, "UpdateTime");

            video.Inputter = MigrateHelper.GetColumnValueString(sourceDataRow, "Inputer");

            var still = MigrateHelper.GetColumnValueString(sourceDataRow, "Still").Replace("|", string.Empty);
            still = MigrateHelper.AddUploadPathFlag(still, this.UploadPath);
            video.WaitPlayPhoto = still;

            var videourl = string.Empty;
            var videourls = MigrateHelper.GetColumnValueString(sourceDataRow, "DownloadUrl");
            string[] a = videourls.Split(new string[] { "$$$" }, StringSplitOptions.RemoveEmptyEntries);
            int count = a.Length;
            for (int i = 0; i < count; i++)
            {
                var videourlx = a[i];
                var str = videourlx.ToString().Substring(videourlx.ToString().IndexOf('|') + 1, videourlx.Length - (videourlx.ToString().IndexOf('|') + 1));

                videourl = videourl + '\n' + MigrateHelper.AddUploadPathFlag(str, this.UploadPath);
            }

            video.VideoUrl = videourl;

            targetDataRow = MigrateHelper.GetMapperDataRow(video, targetDataRow);
            return targetDataRow;
        }
    }
}
