namespace NcProgramManager.Cnc.Fanuc
{
    public static class IsoProgramFormatter
    {
        public static string EnsureTitleBrackets(string program)
        {
            if (string.IsNullOrEmpty(program)) return program;
            string head, body;
            if (!SplitFirstNonEmptyLine(program, out head, out body)) return program;

            int openAngle = head.IndexOf('<');
            if (openAngle >= 0) return program;

            int openPar = head.IndexOf('(');
            int closePar = head.IndexOf(')');

            int titleStart = FindTitleStart(head);
            head = head.Insert(titleStart, "<");
            if (closePar > 0)
            {
                openPar = head.IndexOf('(');
                head = head.Insert(openPar, ">");
            }
            else
            {
                head = head + ">";
            }
            return head + body;
        }

        public static string InsertOrReplaceComment(string program, string comment)
        {
            if (string.IsNullOrEmpty(program)) return program;
            program = EnsureTitleBrackets(program);

            string head, body;
            if (!SplitFirstNonEmptyLine(program, out head, out body)) return program;

            int openPar = head.IndexOf('(');
            int closePar = head.IndexOf(')');
            if (openPar > 0 && closePar > openPar)
            {
                head = head.Remove(openPar + 1, closePar - openPar - 1);
                head = head.Insert(openPar + 1, comment ?? "");
            }
            else
            {
                head = head + "(" + (comment ?? "") + ")";
            }
            return head + body;
        }

        public static string RenameAndComment(string program, string name, string comment)
        {
            if (string.IsNullOrEmpty(program)) return program;
            string head, body;
            if (!SplitFirstNonEmptyLine(program, out head, out body)) return program;

            string newHead = "%\n<" + (name ?? "") + ">(" + (comment ?? "") + ")";
            return newHead + body;
        }

        private static bool SplitFirstNonEmptyLine(string program, out string head, out string body)
        {
            head = program;
            body = "";
            bool sawFirstLf = false;
            int lastLf = -1;
            for (int i = 0; i < program.Length; i++)
            {
                if (program[i] == '\n')
                {
                    if (sawFirstLf)
                    {
                        head = program.Substring(0, i);
                        body = program.Substring(i);
                        return true;
                    }
                    sawFirstLf = true;
                    lastLf = i;
                }
            }
            return false;
        }

        private static int FindTitleStart(string head)
        {
            for (int i = 0; i < head.Length; i++)
            {
                if (head[i] == '\n') return i + 1;
            }
            return 0;
        }
    }
}
