param($installPath, $toolsPath, $package, $project)

$source = @"
    public class Wijmo3Installer
    {
        public static void Install(string htmlPath)
        {
            var strReader = new System.IO.StreamReader(htmlPath);
            var html = strReader.ReadToEnd();
            strReader.Close();

            var lastLinkPos = html.LastIndexOf("<link", html.LastIndexOf("<link"));
            var cssPos = html.IndexOf("\r\n", lastLinkPos);
            var jsPos = html.LastIndexOf("\r\n", html.LastIndexOf("Scripts/Generated/generatedAssets.js"));

            if (cssPos != -1 && jsPos != -1)
            {
                var headPart = html.Substring(0, cssPos);
                var bodyPart = html.Substring(cssPos, jsPos - cssPos);
                var endPart = html.Substring(jsPos);

                if (headPart.IndexOf("Content/c1ls-light-4.0.20182.66.css") == -1)
                {
                    headPart = headPart + "\r\n\r\n    <!-- Wijmo3 Widgets CSS -->";
                    headPart = headPart + "\r\n    <link rel=\"stylesheet\" type=\"text/css\" href=\"Content/jquery.wijmo-pro.all.3.20153.83.min.css\" />";
                    headPart = headPart + "\r\n    <link rel=\"stylesheet\" type=\"text/css\" href=\"Content/c1ls-light-4.0.20182.66.css\" />";
                }

                if (bodyPart.IndexOf("Scripts/c1ls-4.0.20182.66.js") == -1)
                {
                    bodyPart = bodyPart + "\r\n\r\n    <!-- Wijmo3 Widgets JS -->";
                    bodyPart = bodyPart + "\r\n    <script type=\"text/javascript\" src=\"Scripts/amplify.min.js\"></script>";
                    bodyPart = bodyPart + "\r\n    <script type=\"text/javascript\" src=\"Scripts/jquery.wijmo-open.all.3.20153.83.min.js\"></script>";
                    bodyPart = bodyPart + "\r\n    <script type=\"text/javascript\" src=\"Scripts/jquery.wijmo-pro.all.3.20153.83.min.js\"></script>";
                    bodyPart = bodyPart + "\r\n    <script type=\"text/javascript\" src=\"Scripts/c1ls-4.0.20182.66.js\"></script>\r\n";
                }

                var cssReplaceList = "light-theme,.css|msls-light,.css|jquery.mobile.structure-,.min.css|msls-,.min.css";
                foreach (var css in cssReplaceList.Split('|'))
                {
                    headPart = replaceRow(headPart, "Content/" + css.Split(',')[0], "    <link rel=\"stylesheet\" type=\"text/css\" href=\"Content/" + css.Split(',')[0] + getCSSVersion(css.Split(',')[0], htmlPath) + css.Split(',')[1] + "\" />");
                }

                var jsReplaceList = "winjs-|jquery-|jquery.mobile-|datajs-|msls-";
                foreach (var js in jsReplaceList.Split('|'))
                {
                    bodyPart = replaceRow(bodyPart, "Scripts/" + js, "    <script type=\"text/javascript\" src=\"Scripts/" + js + getJSVersion(js, htmlPath) + ".min.js\"></script>");
                }

                System.IO.File.SetAttributes(htmlPath, System.IO.FileAttributes.Normal);
                var writer = new System.IO.StreamWriter(htmlPath);
                writer.Write(headPart);
                writer.Write(bodyPart);
                writer.Write(endPart);
                writer.Close();
            }
        }

        public static string replaceRow(string source, string target, string newRow)
        {
            var pos = source.LastIndexOf(target);
            if (pos != -1)
            {
                var startPos = source.LastIndexOf("\r\n", pos);
                if (startPos == -1)
                {
                    startPos = source.LastIndexOf("<", pos);
                }

                var endPos = source.IndexOf("\r\n", pos);
                if (startPos == -1)
                {
                    endPos = source.IndexOf(">", pos);
                }

                return source.Substring(0, startPos) + "\r\n" + newRow + source.Substring(endPos);
            }
            return source;
        }

        public static string getJSVersion(string starts, string htmlPath)
        {
            foreach (var file in new System.IO.DirectoryInfo(new System.IO.FileInfo(htmlPath).Directory.FullName + "\\Scripts").GetFiles())
            {
                if (file.Extension == ".js" && file.Name != starts + ".js" && !file.Name.Contains("-vsdoc") && !file.Name.Contains(".intellisense") && file.Name.StartsWith(starts, System.StringComparison.CurrentCultureIgnoreCase))
                {
                    return file.Name.Replace(starts, "").Replace(".min", "").Replace(".js", "");
                }
            }
            return "";
        }

        public static string getCSSVersion(string starts, string htmlPath)
        {
            foreach (var file in new System.IO.DirectoryInfo(new System.IO.FileInfo(htmlPath).Directory.FullName + "\\Content").GetFiles())
            {
                if (file.Extension == ".css" && file.Name != starts + ".css" && file.Name.StartsWith(starts, System.StringComparison.CurrentCultureIgnoreCase))
                {
                    return file.Name.Replace(starts, "").Replace(".min", "").Replace(".css", "");
                }
            }
            return "";
        }
    }
"@

$default = $project.ProjectItems | Where-Object { $_.Name -eq "default.htm" }
if ($default -ne $null)
{

  if (-not $default.Saved)
  {
    $default.Save($default.Properties.Item("FullPath").Value)
  }

  Add-Type -TypeDefinition $source
  [Wijmo3Installer]::Install($default.Properties.Item("FullPath").Value)
}
