/*
 * SDK Pullenti Unitext, version 4.33, fabruary 2026. Copyright (c) 2013-2026, Pullenti. All rights reserved. 
 * Non-Commercial Freeware and Commercial Software. 
 * This class is generated using the converter UniSharping (www.unisharping.ru) from Pullenti C# project. 
 * The latest version of the code is available on the site www.pullenti.ru
 */

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Pullenti.Unitext.Internal.Uni
{
    class UnitextCorrPdfLine
    {
        public List<Pullenti.Unitext.UnitextPlaintext> Texts = new List<Pullenti.Unitext.UnitextPlaintext>();
        public List<Pullenti.Unitext.UnitextNewline> Newlines = new List<Pullenti.Unitext.UnitextNewline>();
        public void RemoveAll()
        {
            foreach (Pullenti.Unitext.UnitextPlaintext txt in Texts) 
            {
                txt.Tag = null;
            }
            foreach (Pullenti.Unitext.UnitextNewline nl in Newlines) 
            {
                nl.Tag = null;
            }
        }
        public bool IsPureNumber
        {
            get
            {
                if (Texts.Count > 1) 
                    return false;
                int num = 0;
                foreach (char ch in Texts[0].Text) 
                {
                    if (char.IsDigit(ch)) 
                        num++;
                    else if (ch != ' ' && ch != '.') 
                        return false;
                }
                return num > 0;
            }
        }
        public string HeadTextWithoutNumbers
        {
            get
            {
                StringBuilder tmp = new StringBuilder();
                foreach (Pullenti.Unitext.UnitextPlaintext t in Texts) 
                {
                    tmp.Append(t.Text.Trim());
                }
                for (int i = 0; i < tmp.Length; i++) 
                {
                    if (!char.IsDigit(tmp[i])) 
                    {
                        if (i > 0) 
                            tmp.Remove(0, i);
                        break;
                    }
                }
                for (int i = tmp.Length - 1; i >= 0; i--) 
                {
                    if (!char.IsDigit(tmp[i])) 
                    {
                        tmp.Length = i + 1;
                        break;
                    }
                }
                if (tmp.Length == 0) 
                    return null;
                if (char.IsDigit(tmp[0])) 
                    return null;
                return tmp.ToString().Trim();
            }
        }
        public char LastChar
        {
            get
            {
                for (int i = Texts.Count - 1; i >= 0; i--) 
                {
                    for (int j = Texts[i].Text.Length - 1; j >= 0; j--) 
                    {
                        char ch = Texts[i].Text[j];
                        if (!char.IsWhiteSpace(ch)) 
                            return ch;
                    }
                }
                return ' ';
            }
        }
        public bool IsLastCharHiphen
        {
            get
            {
                return CorrLine.IsHiphen(LastChar);
            }
        }
        public void RemoveLastHiph()
        {
            for (int i = Texts.Count - 1; i >= 0; i--) 
            {
                for (int j = Texts[i].Text.Length - 1; j >= 0; j--) 
                {
                    char ch = Texts[i].Text[j];
                    if (CorrLine.IsHiphen(ch)) 
                    {
                        Texts[i].Text = Texts[i].Text.Substring(0, j);
                        return;
                    }
                    if (!char.IsWhiteSpace(ch)) 
                        return;
                }
            }
        }
        public char FirstChar
        {
            get
            {
                for (int i = 0; i < Texts.Count; i++) 
                {
                    foreach (char ch in Texts[i].Text) 
                    {
                        if (!char.IsWhiteSpace(ch)) 
                            return ch;
                    }
                }
                return ' ';
            }
        }
        public char SecondChar
        {
            get
            {
                int cou = 0;
                for (int i = 0; i < Texts.Count; i++) 
                {
                    foreach (char ch in Texts[i].Text) 
                    {
                        if (!char.IsWhiteSpace(ch)) 
                        {
                            cou++;
                            if (cou > 1) 
                                return ch;
                        }
                    }
                }
                return ' ';
            }
        }
        public int TextLength
        {
            get
            {
                int len = 0;
                foreach (Pullenti.Unitext.UnitextPlaintext t in Texts) 
                {
                    foreach (char ch in t.Text) 
                    {
                        if (!char.IsWhiteSpace(ch)) 
                            len += 1;
                    }
                }
                return len;
            }
        }
        public int NewlinesCount
        {
            get
            {
                int cou = 0;
                foreach (Pullenti.Unitext.UnitextNewline nl in Newlines) 
                {
                    cou += nl.Count;
                }
                return cou;
            }
        }
        static bool _isOpenBracket(char ch)
        {
            return "\"'`’<{([«“„”".IndexOf(ch) >= 0;
        }
        static bool _isCloseBracket(char ch)
        {
            return "\"'`’>})]»”“".IndexOf(ch) >= 0;
        }
        public int FootnoteHeadNum
        {
            get
            {
                string txt = Texts[0].Text;
                for (int i = 0; i < txt.Length; i++) 
                {
                    if (!char.IsWhiteSpace(txt[i])) 
                    {
                        if (!char.IsDigit(txt[i])) 
                            return 0;
                        int num = (int)((txt[i] - '0'));
                        for (i = i + 1; i < txt.Length; i++) 
                        {
                            if (char.IsDigit(txt[i])) 
                                num = (num * 10) + ((int)((txt[i] - '0')));
                            else 
                                break;
                        }
                        if (num > 900) 
                            return 0;
                        if (i < txt.Length) 
                        {
                            for (; i < txt.Length; i++) 
                            {
                                if (!char.IsWhiteSpace(txt[i])) 
                                    break;
                            }
                            if (i >= txt.Length) 
                            {
                                if (Texts.Count > 1 && Texts[0].Typ != Pullenti.Unitext.UnitextPlaintextType.Simple) 
                                    return num;
                                return 0;
                            }
                            if (char.IsLetter(txt[i])) 
                            {
                                if (!char.IsUpper(txt[i])) 
                                    return 0;
                            }
                            else if (!_isOpenBracket(txt[i])) 
                                return 0;
                            if (TextLength < 8) 
                                return 0;
                            return num;
                        }
                        if (Texts[0].Typ != Pullenti.Unitext.UnitextPlaintextType.Simple && Texts.Count > 1) 
                        {
                            if (TextLength < 8) 
                                return 0;
                            return num;
                        }
                        break;
                    }
                }
                return 0;
            }
        }
        public UnitextCorrPdfLine RefLine;
        public Pullenti.Unitext.UnitextPlaintext RefText;
        public int RefPos;
        public int RefLen;
        public Pullenti.Unitext.UnitextFootnote ResFoot;
        public bool CanUnion(UnitextCorrPdfLine next)
        {
            if (TextLength < 30) 
                return false;
            char last = LastChar;
            char first = next.FirstChar;
            if (CorrLine.IsHiphen(last) || last == ',') 
                return true;
            if (last == '.' || last == ';' || last == ':') 
                return false;
            if (char.IsLetter(first) && char.IsLower(first)) 
            {
                if (char.IsLetter(last) && char.IsUpper(last)) 
                    return false;
                char ch1 = next.SecondChar;
                if (ch1 == ')' || ch1 == '.') 
                    return false;
                return true;
            }
            if (_isOpenBracket(first)) 
            {
                if (first == '(') 
                    return true;
                char sec = next.SecondChar;
                if (char.IsLetter(sec) && char.IsLower(sec)) 
                    return true;
            }
            return false;
        }
        public bool AttachFoot(UnitextCorrPdfLine foot)
        {
            string num = foot.FootnoteHeadNum.ToString();
            if (num == "17") 
            {
            }
            for (int i = 0; i < Texts.Count; i++) 
            {
                string txt = Texts[i].Text;
                for (int j = 0; j < txt.Length; j++) 
                {
                    if (txt[j] == num[0]) 
                    {
                        int k;
                        for (k = 0; (k < num.Length) && ((k + j) < txt.Length); k++) 
                        {
                            if (num[k] != txt[j + k]) 
                                break;
                        }
                        if (k < num.Length) 
                            continue;
                        int j1 = (j + k) - 1;
                        char ch0 = ' ';
                        char ch00 = ' ';
                        bool doubt = false;
                        if (j == 0) 
                        {
                            if (i == 0) 
                                continue;
                            string txt0 = Texts[i - 1].Text;
                            ch0 = txt0[txt0.Length - 1];
                            if (txt0.Length > 1) 
                                ch00 = txt0[txt0.Length - 2];
                        }
                        else 
                        {
                            ch0 = txt[j - 1];
                            if (j > 1) 
                                ch00 = txt[j - 2];
                        }
                        if (char.IsWhiteSpace(ch0) || char.IsDigit(ch0)) 
                            continue;
                        if (char.IsLetter(ch0)) 
                        {
                        }
                        else if (ch0 == '.' || ch0 == ';' || _isCloseBracket(ch0)) 
                        {
                            if (!char.IsLetter(ch00)) 
                                continue;
                        }
                        else if (ch0 == ',') 
                            doubt = true;
                        else 
                            continue;
                        ch0 = ' ';
                        if (j1 == (txt.Length - 1)) 
                        {
                            if (i == (Texts.Count - 1)) 
                                ch0 = ' ';
                            else 
                                ch0 = Texts[i + 1].Text[0];
                        }
                        else 
                            ch0 = txt[j1 + 1];
                        if (char.IsLetterOrDigit(ch0)) 
                            continue;
                        if (doubt && ch0 != ' ' && Texts[i].Typ == Pullenti.Unitext.UnitextPlaintextType.Simple) 
                            continue;
                        foot.RefLine = this;
                        foot.RefText = Texts[i];
                        foot.RefPos = j;
                        foot.RefLen = (j1 + 1) - j;
                        return true;
                    }
                }
            }
            return false;
        }
        public override string ToString()
        {
            StringBuilder tmp = new StringBuilder();
            int num = FootnoteHeadNum;
            if (num > 0) 
                tmp.AppendFormat("[{0}] ", num);
            if (RefText != null) 
                tmp.AppendFormat(" -> ({0})[{1}] ", RefText.Text, RefPos);
            foreach (Pullenti.Unitext.UnitextPlaintext t in Texts) 
            {
                tmp.Append(t.Text);
            }
            return tmp.ToString();
        }
    }
}