﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HTMLTools.Common
{
    public class ScriptString
    {
        static List<string> ToolsKeyWord;
        static string KeywordPath = "Keywords.txt";

        public static string ScriptEncode(string OriginString, string ObjectType, string ObjectName)
        {
            if (File.Exists(KeywordPath))
            {
                KeywordReader kr = new KeywordReader(KeywordPath);
                ToolsKeyWord = kr.GetKeywords();
            }

            StringBuilder sbExport = new StringBuilder();
            string NewString = string.Empty;
            string Command = string.Empty;
            string PreChar = "(\"";
            string PostChar = "\");";
            bool FlowControl = false;

            sbExport.Append(Tools.StatementBuilder(ref Command, ref PreChar, ref PostChar, ObjectType, ObjectName));
            OriginString = OriginString.Replace("\n", "*").Replace("\n\r", "*").Replace("\r\n", "*").Replace("\"", "\\\"").Replace("\t", "");
            if (!string.IsNullOrEmpty(OriginString) && OriginString.Contains("*"))
            {
                string[] temp = OriginString.Split('*');
                foreach (string str in temp)
                {
                    if (string.IsNullOrEmpty(str))
                        continue;
                    if (str == "[END]")
                    {
                        sbExport.AppendLine("}");
                        FlowControl = false;
                        continue;
                    }

                    if (ToolsKeyWord.Any(s => str.Contains(s)))
                    {
                        FlowControl = true;
                        sbExport.AppendLine(Tools.KeyWordToStatement(str));
                    }
                    else
                    {
                        string PreWord = FlowControl ? "\t" : "";
                        sbExport.AppendLine(PreWord + Command + PreChar + str + PostChar);
                    }
                }
            }
            else
            {
                sbExport.AppendLine(Command + PreChar + OriginString + PostChar);
            }

            return sbExport.ToString();
        }


        public static string GetForeachString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("[FOREACH_START = var:Item:ItemList]");
            sb.AppendLine("\t");
            sb.AppendLine("[END]");
            return sb.ToString();
        }

        public static string GetForLoopString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("[FORLOOP_START = int:X:0:10:1]");
            sb.AppendLine("\t");
            sb.AppendLine("[END]");
            return sb.ToString();
        }

        public static string GetWhileLoopString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("[WHILELOOP_START = int:X:0:10:1]");
            sb.AppendLine("\t");
            sb.AppendLine("[END]");
            return sb.ToString();
        }

        public static string GetIfString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("[IF_START = int:X:0:10]");
            sb.AppendLine("\t");
            sb.AppendLine("[END]");
            return sb.ToString();
        }

        public static string GetElseString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("[ELSE_START]");
            sb.AppendLine("\t");
            sb.AppendLine("[END]");
            return sb.ToString();
        }
    }
}
