﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using SklAnalysis.TemplateXml;

namespace SklAnalysis.Data
{
    /// <summary>
    /// 预先存储好的全局数据 
    /// </summary>
    public static class GlobalValues
    {
        private static Dictionary<string, string> dict = new Dictionary<string, string>();

        public static Dictionary<string, string> Add(string Key, string Value)
        {
            try
            {
                dict.Add(Key, Value);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message + ".Key:" + Key);
            }
            return dict;

        }

        /// <summary>
        /// 参数列表 
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> List()
        {
            return dict;
        }

        //初始化数据 ,加GlobalValueType 参数，先后加载。
        public static void SaveInital(Config xmlConfig, TemplateXml.GlobalValueType vt)
        {
            foreach (TemplateXml.GlobalValue gv in xmlConfig.GlobalValues.GlobalValueS)
            {
                if (gv.ValueType == vt)
                {
                    Bookmark bm = new Bookmark();
                    bm.Name = "BM__________________________";
                    bm.ValueType = (BookmarkValueType)Convert.ToInt16(gv.ValueType);
                    bm.ShowType = BookmarkShowType.Text;
                    bm.BookmarkText = gv.BookmarkText;
                    bm.BookmarkParameter = gv.BookmarkParameter;
                    bm.BookmarkDatasource = gv.BookmarkDatasource;
                    bm.BookmarkComputings = gv.BookmarkComputings;
                    bm.BookmarkDataformat = gv.Dataformat;
                    bm.ConditionAtts = gv.ConditionAtts;
                    bm.IsUserPlugin = gv.IsUserPlugin;
                    bm.PluginName = gv.PluginName;

                    string newValue = Engine.FillTexts.newValue(bm, true);
                    if (dict.ContainsKey(gv.Name) == false)
                    {
                        dict.Add(gv.Name, newValue);
                        SklLogger.Logger.WriteLog("全局值:" + gv.Name + "\r\n" + newValue, "分析报告Sql");
                    }
                }
            }
        }

        public static List<string> RegValues(string text)
        {
            List<string> list = new List<string>();
            Regex re = new Regex(@"\#\{.*?\}");
            MatchCollection mList = re.Matches(text);
            for (int j = 0; j < mList.Count; j++)
            {
                //显示每个分组，可以看到每个分组都只有组号为1的项，反向回查和反向预查没有被捕获
                for (int i = 0; i < mList[j].Groups.Count; i++)
                {
                    list.Add(mList[j].Groups[i].Value.Replace("#{", "").Replace("}", ""));
                }
            }

            return list;
        }
    }
}
