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 PhotoMigration : BaseContentMoldDataMigrationItem
    {
        /// <summary>
        /// 模型名字。
        /// </summary>
        public override string ModelName { get; set; } = "图集";

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

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

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

        /// <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_Photo as a on cm.ItemID=a.ID where GeneralID > {newContentId} and TableName='PE_U_Photo' and Title!='' order by GeneralID";

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

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

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

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

            photo.Title = MigrateHelper.GetColumnValueString(sourceDataRow, "title");

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

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

            var content = MigrateHelper.GetColumnValueString(sourceDataRow, "Content");
            content = content.Replace("{PE.SiteConfig.ApplicationPath/}", string.Empty);
            content = content.Replace("{PE.SiteConfig.ApplicationPath/}{PE.SiteConfig.uploaddir/}", string.Empty);
            content = content.Replace("{PE.SiteConfig.uploaddir/}", string.Empty);
            content = MigrateHelper.ReplaceContentUploadPath(content, this.UploadPath);
            var intro = MigrateHelper.FilterHTMLTag(content);
            photo.Intro = intro;

            // 图集。
            var photoUrl = MigrateHelper.GetColumnValueString(sourceDataRow, "PhotoUrl");
            photoUrl = photoUrl.Replace("$$$", "GZLZ");
            string[] sArray = Regex.Split(photoUrl, "GZLZ", RegexOptions.IgnoreCase);
            var photoIntro = MigrateHelper.GetColumnValueString(sourceDataRow, "PhotoIntro");
            var photoStr = string.Empty;
            photoStr = "[";
            int photoCount = 1;
            foreach (string i in sArray)
            {
                var str = i.ToString().Substring(i.ToString().IndexOf('|') + 1, i.Length - (i.ToString().IndexOf('|') + 1));
                str = MigrateHelper.AddUploadPathFlag(str, this.UploadPath);
                photoStr = photoStr + "{\"PhotoUrl\":\"" + str.ToString() + "\", ";
                photoStr = photoStr + "\"PhotoTitle\":\"" + photoCount + ".jpg\",";
                photoStr = photoStr + " \"PhotoIntro\":\"\",";
                photoStr = photoStr + " \"fileName\":\"" + photoCount + ".jpg\",";
                photoStr = photoStr + "\"fileSize\":0},";
                photoCount++;
            }

            photoStr = photoStr.Remove(photoStr.Length - 1, 1);
            photoStr = photoStr + "]";
            photo.Photos = photoStr;

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

            photo.Author = MigrateHelper.GetColumnValueString(sourceDataRow, "Author");

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

            photo.FeaturedImage = MigrateHelper.AddUploadPathFlag(MigrateHelper.GetColumnValueString(sourceDataRow, "DefaultPicUrl"), this.UploadPath);

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

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

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

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

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

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

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

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