using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Text.RegularExpressions;
using JuSNS.Common;
using System.Web;
using System.Xml;

namespace JuSNS.Home
{
    /// <summary>
    /// 模板元素替换类
    /// </summary>
    public class ParseTemplate
    {
        private string _tag;

        private string _Result;

        private int _UserID;
        /// <summary>
        /// 结果
        /// </summary>
        public string Result
        {
            get { return _Result; }
        }
        /// <summary>
        /// 替换整个模板
        /// </summary>
        /// <param name="tempname"></param>
        /// <returns>替换后的模板</returns>
        static public string replaceTemplate(string tempname)
        {
            JuSNS.Config.SiteTemplateConfig sConfig = new JuSNS.Config.SiteTemplateConfig(tempname);
            string returnStr = sConfig.HTML;
            string s = string.Empty;
            Match match = Regex.Match(returnStr, "{[^\\}{><,/]*}", RegexOptions.IgnoreCase | RegexOptions.RightToLeft);
            while (match.Success)
            {
                s = match.Value;
                ParseTemplate pt = new ParseTemplate(s);
                returnStr = returnStr.Replace(s, pt.Result);
                match = match.NextMatch();
            }
            return returnStr;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="tag">标记</param>
        public ParseTemplate(string tag)
        {
            _tag = tag;
            _tag = _tag.Replace("{", "");
            _tag = _tag.Replace("}", "");
            _UserID = Public.GetUserID();
            MethodInfo method = this.GetType().GetMethod(_tag, BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.NonPublic);
            object str = string.Empty;
            try
            {
                str = method.Invoke(this, null);
            }
            catch { }
            _Result = str.ToString();
        }

        protected string RootDir()
        {
            return Public.rootDir;
        }

        protected string UserSpace()
        {
            return Public.GetSpaceLink(_UserID);
        }

        protected string ReURL()
        {
            return HttpContext.Current.Request.Url.ToString().Replace("&", "-----");
        }

        protected string MySpaceLink()
        {
            return Public.GetSpaceLink(_UserID);
        }

        protected string UserID()
        {
            string s = _UserID.ToString();
            return s;
        }

        protected string gName()
        {
            string s = JuSNS.Config.UiConfig.gName;
            return s;
        }

        protected string APPServer()
        {
            string s = string.Empty;
            StringBuilder sb = new StringBuilder();
            List<JuSNS.Model.ApiInterface.AppInfo> infolist = JuSNS.ApiInterface.UtilUser.GetUserApp(_UserID);
            foreach (JuSNS.Model.ApiInterface.AppInfo info in infolist)
            {
                string url = HttpUtility.UrlEncode(info.Url + "?u=" + JuSNS.ApiInterface.SecretUser.EncryptUserID(_UserID.ToString()));
                sb.Append("<li><span style=\"float:left;\"><a href=\"");
                sb.Append(Public.rootDir + "/app/frame?id=" + info.AppID);
                sb.Append("\"><img src=\"" + JuSNS.Common.Public.GetAppIcon(info.Icon) + "\" style=\"width:16px;height:16px;\" align=\"absmiddle\" /> ");
                sb.Append(info.AppName);
                sb.Append("</a></span></li>");
            }
            s = sb.ToString();
            return s;
        }

        protected string MenuAppList()
        {
            string s = string.Empty;
            StringBuilder sb = new StringBuilder();
            List<JuSNS.Model.ApiInterface.AppInfo> infolist = JuSNS.ApiInterface.UtilUser.GetUserApp(_UserID);
            foreach (JuSNS.Model.ApiInterface.AppInfo info in infolist)
            {
                string url = HttpUtility.UrlEncode(info.Url + "?u=" + JuSNS.ApiInterface.SecretUser.EncryptUserID(_UserID.ToString()));
                sb.Append("<li><span><img src=\"" + JuSNS.Common.Public.GetAppIcon(info.Icon) + "\" style=\"width:16px;height:16px;\" align=\"absmiddle\" /> <a href=\"");
                sb.Append(Public.rootDir + "/app/frame?id=" + info.AppID);
                sb.Append("\"> ");
                sb.Append(info.AppName);
                sb.Append("</a></span></li>");
            }
            s = sb.ToString();
            return s;
        }

        protected string SkinStyle()
        {
            string s = JuSNS.Config.UiConfig.SkinStyle;
            return s;
        }

        protected string SNSVersion()
        {
            string s = JuSNS.Config.UiConfig.version;
            return s;
        }

        protected string SiteName()
        {
            string s = JuSNS.Config.UiConfig.SiteName;
            return s;
        }

        protected string loginName()
        {
            if (JuSNS.Common.Public.GetXMLValue("isRegMobile") == "1")
            {
                return  "手机或邮件";
            }
            else
            {
                return  "电子邮件";
            }
        }


        protected string loginCode()
        {
            string s = string.Empty;
            if (JuSNS.Common.Public.GetXMLValue("loginCode") == "1")
            {
                s += "<div> 请输入下面4位随机字符:</div>";
                string ScriptCode = "<script type=\"text/javascript\" language=\"JavaScript\">var numkey = Math.random();numkey = Math.round(numkey*10000);document.write(\"<img src='{RootDir}/inc/code/image.aspx?k='+ numkey +'' width='70' onClick='this.src+=Math.random()' alt='图片看不清？点击重新得到验证码' style='cursor:pointer;' height='23' hspace='4' align='absmiddle' />\");</script>";
                s += "<div><input type=\"text\" name=\"TxtLogCode\" id=\"TxtLogCode\" class=\"f-text s30\" maxlength=\"6\" /> " + ScriptCode + "</div>";
            }
            return s;
        }

        protected string URL()
        {
            string s = JuSNS.Config.UiConfig.URL;
            return s;
        }

        protected string JusnsMenu()
        {
            string s = string.Empty;
            XmlDocument doc = new XmlDocument();
            doc.Load(HttpContext.Current.Server.MapPath("~/config/base/url.config"));
            XmlNodeList nodelist = doc.SelectNodes("/config/add");
            bool isValue = false;
            string listSTR = string.Empty;
            string SiteName = string.Empty;
            int i = 0;
            foreach (XmlNode n in nodelist)
            {
                XmlAttributeCollection xac = n.Attributes;
                try
                {
                    int key = int.Parse(xac["key"].Value);
                    string value = xac["value"].Value;
                    string more = xac["more"].Value;
                    if (key == 0)
                    {
                        SiteName = value;
                        continue;
                    }
                    listSTR += "<li><a href=\"" + RootDir() + "/home/url.aspx?url=" + more + "\">" + value + "</a></li>";
                }
                catch { }
                isValue = true;
            }
            if (isValue)
            {
                s += "<span onmouseover=\"clearhidemenu()\" onmouseout=\"delayhidemenu('linkToURL')\" class=\"divstyle-jusnsmenu\" style=\"display: none;\"  id=\"linkToURL\"><ul>" + listSTR + "</ul></span>";
                s += "<a class=\"hn-apps\" href=\"javascript:void(0);\" onmouseover=\"showSetMenu('linkToURL');\" onmouseout=\"delayhidemenu('linkToURL')\">" + SiteName + "</a>";
            }
            return s;
        }

        protected string Version()
        {
            string s = JuSNS.Config.UiConfig.version;
            return s;
        }

        protected string PageTitle()
        {
            string s = JuSNS.Config.UiConfig.PageTitle;
            return s;
        }

        protected string Domain()
        {
            string s = JuSNS.Config.UiConfig.Domain;
            return s;
        }

        protected string NoteStr()
        {
            string noteStr = "<span id=\"notinumber\"><a href=\"" + Public.rootDir + "/home/mail/notice.aspx\">通知<span  class=\"font11\">(0)</span></a></span>";
            JuSNS.Home.JUser us = new JuSNS.Home.JUser();
            int noteNumber = us.NoticeInfo(_UserID);
            if (noteNumber > 0)
            {
                noteStr = "<span id=\"notifications\"><a href=\"" + Public.rootDir + "/home/mail/notice.aspx\" title=\"你有" + noteNumber + "个新通知\"><span>" + noteNumber + "</span></a><script>newnote();</script></span>";
            }
            return noteStr;
        }
    }
}
