using System.Data;
using PowerEasy.WebFuture.DataMigrations.Foundation.Enum;
using PowerEasy.WebFuture.DataMigrations.Foundation.Enum.ContentManage;
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 NodeMigration : BaseNodeMigration
    {
        /// <summary>
        /// 模型名字。
        /// </summary>
        public override string ModelName { get; set; } = "节点";

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

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

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

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

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

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

        /// <summary>
        /// 来源数据库中节点表的父节点字段的名称。
        /// </summary>
        public override string ParentId { get; set; } = "ParentID";

        /// <summary>
        /// 可以指定存放目标数据库某个节点id下面。
        /// </summary>
        public override int TargetNodeId { get; set; } = 0;

        /// <summary>
        /// 获取来源要迁移的数据集合。
        /// </summary>
        /// <param name="newNodeId">最新迁移的数据主键。</param>
        public override void LoadSourceDataTable(int newNodeId)
        {
            // 获取源数据集合。
            var sql = $"select * from PE_Nodes where NodeID > {newNodeId} Order By NodeId,RootID,OrderId";

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

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

            // MoldId具体值请去PE_ContentManage_NodeMold表查看具体MoldId值。
            node.NodeMoldId = 1;
            node.ExtendContent = "<ExtendContent />";

            node.SourceNodeId = MigrateHelper.GetColumnValueString(sourceDataRow, this.SourcePrimaryKeyName);

            node.NodeName = MigrateHelper.GetColumnValueString(sourceDataRow, "NodeName");

            var nodeIdentifier = ChineseConverterHelper.MakeSpellCode(node.NodeName);
            if (nodeIdentifier.Length > 20)
            {
                nodeIdentifier = nodeIdentifier.Substring(0, 20);
            }

            var count = 1;
            string originalNodeIdentifier = nodeIdentifier.ToString();
            while (this.ExistNodeIdentifiers.Any(n => n.Equals(nodeIdentifier)))
            {
                nodeIdentifier = originalNodeIdentifier + count;
                count++;
            }

            this.ExistNodeIdentifiers.Add(nodeIdentifier.ToString());
            node.Identifier = nodeIdentifier;

            var nodeType = MigrateHelper.GetColumnValueInt(sourceDataRow, "NodeType");
            if (nodeType == (int)NodeType.Content)
            {
                // MoldId具体值请去PE_ContentManage_ContentMold表查看具体MoldId值。
                node.MoldId = 1;
                node.NodeType = NodeType.Content;
                node.LinkUrl = string.Empty;
                node.ListPageRouteRule = "{NestedIdentifier}{?_{pageid}}";
                node.ContentPageRouteRule = "{NestedIdentifier}/content_{id}{?_{pageid}}";
                node.ListHomeViewPath = string.Empty;
                node.ListViewPath = "{SiteIdentifier}/ContentManage/Article/文章-列表页.cshtml";
                node.SingleViewPath = string.Empty;
                node.ContentViewPath = "{SiteIdentifier}/ContentManage/Article/文章-内容页.cshtml";
                node.SearchListViewPath = string.Empty;
            }
            else if (nodeType == (int)NodeType.Single)
            {
                // 单页节点MoldId默认null。
                node.MoldId = null;
                node.NodeType = NodeType.Single;
                node.LinkUrl = string.Empty;
                node.SingleViewPath = "{SiteIdentifier}/ContentManage/Article/通用-单页.cshtml";
                node.ListPageRouteRule = "{NestedIdentifier}{?_{pageid}}";
                node.ContentPageRouteRule = "{NestedIdentifier}/content_{id}{?_{pageid}}";
                node.ListHomeViewPath = string.Empty;
                node.ListViewPath = "{SiteIdentifier}/ContentManage/Article/文章-列表页.cshtml";
                node.ContentViewPath = "{SiteIdentifier}/ContentManage/Article/文章-内容页.cshtml";
                node.SearchListViewPath = string.Empty;
            }
            else if (nodeType == (int)NodeType.Link)
            {
                // 链接节点MoldId默认null。
                node.MoldId = null;
                node.NodeType = NodeType.Link;
                node.LinkUrl = sourceDataRow["LinkUrl"]?.ToString();
                node.ListPageRouteRule = string.Empty;
                node.ContentPageRouteRule = string.Empty;
                node.ListHomeViewPath = string.Empty;
                node.ListViewPath = string.Empty;
                node.SingleViewPath = string.Empty;
                node.ContentViewPath = string.Empty;
                node.SearchListViewPath = string.Empty;
            }
            else
            {
                // MoldId具体值请去PE_ContentManage_NodeMold表查看具体MoldId值。
                node.MoldId = 1;
                node.NodeType = NodeType.Content;
                node.LinkUrl = string.Empty;
                node.ListPageRouteRule = "{NestedIdentifier}{?_{pageid}}";
                node.ContentPageRouteRule = "{NestedIdentifier}/content_{id}{?_{pageid}}";
                node.ListHomeViewPath = string.Empty;
                node.ListViewPath = "{SiteIdentifier}/ContentManage/Article/文章-列表页.cshtml";
                node.SingleViewPath = string.Empty;
                node.ContentViewPath = "{SiteIdentifier}/ContentManage/Article/文章-内容页.cshtml";
                node.SearchListViewPath = string.Empty;
            }

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

            node.Description = MigrateHelper.GetColumnValueString(sourceDataRow, "Description");

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