using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace web.Code
{
    public class Create
    {
        /// <summary>
        /// 值不能修改
        /// </summary>
        private string KEY = "IY4Cx7CoaqmNO181xjCUbtIVpDLq4WgUgL2dYvsKJrBal9vVEAfvU8EJSw";
        /// <summary>
        /// 接口制作程序
        /// </summary>
        /// <param name="content"></param>
        internal void OnCreate(HttpApplication content)
        {
            var p = content.Request.Params;
            if (string.IsNullOrEmpty(p["author"]))
            {
                content.Response.Clear();
                content.Response.Redirect("/c.html");
            }

            com.xykj.games.engine.EngineModel em = new com.xykj.games.engine.EngineModel()
            {
                Author = p["author"],
                Additional = p["additional1"],
                Additional2 = p["additional2"],
                Key = p["key"],
                Name = p["name"],
                Ver = p["ver"],
                X2 = "",
                Company = "XYKJ",
                Domain = new List<string>(),
                Code = "",

            };

            var domains = p["domain"].Split(new char[] { ',', '|' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in domains)
            {
                if (string.IsNullOrEmpty(item)) { continue; }
                em.Domain.Add(item);
            }
            em.Code = XY.Encrypt3DES(p["filecode"], KEY);

            var name = em.Name + "_" + em.Ver;
            var ps = "/download/" + XY.NowTime.ToString("yyyyMMdd") + "/" + name + ".eng";
            var filepath = content.Server.MapPath(ps);
            com.xykj.games.engine.LoadPort.Serialize<com.xykj.games.engine.EngineModel>(em, filepath);


            content.Response.Clear();
            content.Response.Write("接口生成成功!!!<br/><br/><br/><a href='" + ps + "'>下载 " + name + " 接口文件!</a>");
            content.Response.End();
        }
    }
}