using System;
using System.IO;
using System.Collections.Generic;

namespace Sex
{
    public partial class Merge : System.Web.UI.Page
    {
        string[] FileList = new string[]
        { 
            "default.aspx.cs",
            "ExeDirectoryListThumbs.cs",
            "ExeFileDirectory.cs",
            "ExeFileDownload.cs",
            "ExeImage.cs",
            "ExeListExif.cs",
            "ExeListFileDirectory.cs",
            "ExeListImage.cs",
            "ExeListPermissions.cs",
            // "ExeReadWordDocuments.cs",
            "ExeState.cs",
            "ExeTreeValue.cs",
            "List.cs",
            "ListExplorerAbout.cs",
            "ListExplorerAttributes.cs",
            "ListExplorerExif.cs",
            "ListExplorerExtensions.cs",
            "ListExplorerFileGroups.cs",
            "ListExplorerMimeTypes.cs",
            "ListExplorerTemplates.cs",
            "ListExplorerViewState.cs",
            "ListServer.cs",
            "ListServerApplication.cs",
            "ListServerBrowser.cs",
            "ListServerContext.cs",
            "ListServerCookies.cs",
            "ListServerEnvironment.cs",
            "ListServerMobile.cs",
            "ListServerModules.cs",
            "ListServerRequest.cs",
            "ListServerResponse.cs",
            "ListServerSession.cs",
            "ListServerVariables.cs",
            "ListServerWebSettings.cs",
            "StaticExif.cs",
            "StaticFileGroups.cs",
            "StaticFileTypes.cs",
            "StaticHex.cs",
            "StaticMime.cs",
            "StaticSyntaxHighlighting.cs",
            "StaticTemplates.cs",
            "StaticTree.cs",
            "StaticXml.cs"
        };
        List<string> Usings = new List<string>();
        protected void Page_Load(object sender, EventArgs e)
        {
            string ThisPath = Request.PhysicalApplicationPath;
            string NewFile = "";
            for (int i = 0; i < FileList.Length; i++)
            {
                Response.Write("Merging " + FileList[i] + "<br/>");
                NewFile += GetCode(ThisPath + FileList[i]);
            }
            Response.Write("Reading default.aspx, skiping first line<br/>");
            string[] arrBody = File.ReadAllLines(ThisPath + "default.aspx");
            string Body = "";
            Response.Write("Adding Body of default.aspx<br/>");
            for (int i = 1; i < arrBody.Length; i++)
            {
                Body = Body + arrBody[i] + "\r\n";
            }
            Response.Write("Importing Namespaces<br/>");
            Usings.Sort();
            for (int i = 0; i < Usings.Count; i++)
            {
                Body = Usings[i].Replace("using ", "<%@ Import Namespace=\"").Replace(";", "\" %>") + "\r\n" + Body;
            }
            Response.Write("Adding new Page Reference to default.aspx<br/>");
            Body = "<%@ Page Language=\"c#\" Debug=\"true\" %>\r\n" + Body;
            Response.Write("Saving in sex.aspx<br/>");
            Body = Body.Replace("/*##*/", NewFile);
            Response.Write("Inserting Sex.css<br/>");
            Body = Body.Replace("/*#STYLE#*/", File.ReadAllText(ThisPath + "sex.css"));
            File.WriteAllText(ThisPath + "SEX.aspx", Body);
            Response.Write("<a href='sex.aspx'>Start SEX</a>");
        }
        enum PHASE { USING, HEADER, BODY };
        protected string GetCode(string FilePath)
        {
            string[] allLines = File.ReadAllLines(FilePath);
            string CodeToReturn = "";
            PHASE phase = PHASE.USING;
            for (int i = 0; i < allLines.Length - 2; i++)
            {
                string line = allLines[i];
                switch (phase)
                {
                    case PHASE.USING:
                        if (line.StartsWith("using"))
                        {
                            line = line.Trim().Replace("  ", " ");
                            //                            if (!line.Contains("System.DirectoryServices")) // for some reason this assembly is not found
                            if (!Usings.Contains(line)) Usings.Add(line);
                        }
                        else if (line.Contains("public partial class Sex")) phase = PHASE.HEADER;
                        break;
                    case PHASE.HEADER:
                        if (line.Contains("{"))
                        {
                            phase = PHASE.BODY;
                            string CodeFile = Path.GetFileName(FilePath);
                            CodeToReturn += "\r\n// Start of " + CodeFile + "\r\n\r\n";
                        }
                        break;
                    case PHASE.BODY:
                        CodeToReturn += line + "\r\n";
                        break;
                }
            }
            return CodeToReturn;
        }
    }
}