/*
 * 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
 */

package com.pullenti.unitext.internal.misc;

public class Fb2Helper {

    public static com.pullenti.unitext.UnitextDocument createDocZip(String fileName, byte[] content, com.pullenti.unitext.CreateDocumentParam pars) throws Exception, javax.xml.stream.XMLStreamException, java.io.IOException, org.xml.sax.SAXException, java.io.FileNotFoundException {
        MyZipFile zip = new MyZipFile(fileName, content);
        com.pullenti.unitext.UnitextDocument doc = com.pullenti.unitext.UnitextDocument._new69(fileName, com.pullenti.unitext.FileFormat.EPUB);
        doc.content = new com.pullenti.unitext.UnitextContainer();
        java.util.HashMap<String, com.pullenti.unitext.UnitextItem> footNotes = new java.util.HashMap<String, com.pullenti.unitext.UnitextItem>();
        String coverImage = null;
        try {
            for (MyZipEntry entry : zip.entries) {
                if (com.pullenti.unisharp.Utils.endsWithString(entry.name, "body.xml", true)) {
                    byte[] dat = entry.getData();
                    if (dat == null) 
                        continue;
                    com.pullenti.unisharp.XmlDocumentWrapper xml = new com.pullenti.unisharp.XmlDocumentWrapper();
                    try (com.pullenti.unisharp.MemoryStream documentXml = new com.pullenti.unisharp.MemoryStream(dat)) {
                        xml.load(documentXml);
                        documentXml.close();
                    }
                    String img = _loadBody(xml.doc.getDocumentElement(), doc, footNotes, pars);
                    if (img != null) 
                        coverImage = img;
                }
            }
        } finally {
            if (zip != null) 
                zip.close();
        }
        doc.optimize(false, null);
        return doc;
    }

    public static com.pullenti.unitext.UnitextDocument createDoc(String fileName, byte[] content, com.pullenti.unitext.CreateDocumentParam pars) throws Exception, java.io.FileNotFoundException, java.io.IOException, javax.xml.stream.XMLStreamException, org.xml.sax.SAXException {
        com.pullenti.unisharp.XmlDocumentWrapper xml = new com.pullenti.unisharp.XmlDocumentWrapper();
        if (content == null && ((new java.io.File(fileName)).exists() && (new java.io.File(fileName)).isFile())) {
            try (com.pullenti.unisharp.FileStream fs = new com.pullenti.unisharp.FileStream(fileName, "r", "")) {
                xml.load(fs);
            }
        }
        else if (content != null) {
            try (com.pullenti.unisharp.MemoryStream mem = new com.pullenti.unisharp.MemoryStream(content)) {
                try {
                    xml.load(mem);
                } catch (Exception ex) {
                    if (xml.doc.getDocumentElement() == null) 
                        throw ex;
                }
            }
        }
        else 
            return null;
        java.util.HashMap<String, com.pullenti.unitext.UnitextItem> footNotes = new java.util.HashMap<String, com.pullenti.unitext.UnitextItem>();
        for (org.w3c.dom.Node x : (new com.pullenti.unisharp.XmlNodeListWrapper(xml.doc.getDocumentElement().getChildNodes())).arr) {
            if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(x), "body")) {
                String name = null;
                if (x.getAttributes() != null) {
                    for (org.w3c.dom.Node a : (new com.pullenti.unisharp.XmlAttrListWrapper(x.getAttributes())).arr) {
                        if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "name")) 
                            name = a.getTextContent();
                    }
                }
                if (com.pullenti.unisharp.Utils.stringsNe(name, "notes")) 
                    continue;
                for (org.w3c.dom.Node xx : (new com.pullenti.unisharp.XmlNodeListWrapper(x.getChildNodes())).arr) {
                    if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(xx), "section")) {
                        String id = null;
                        if (xx.getAttributes() != null) {
                            for (org.w3c.dom.Node a : (new com.pullenti.unisharp.XmlAttrListWrapper(xx.getAttributes())).arr) {
                                if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "id")) 
                                    id = a.getTextContent();
                            }
                        }
                        if (id == null || footNotes.containsKey(id)) 
                            continue;
                        com.pullenti.unitext.internal.uni.UnitextGen gen = new com.pullenti.unitext.internal.uni.UnitextGen();
                        _GetUniText(xx, gen, null, null, pars);
                        footNotes.put(id, gen.finish(true, null));
                    }
                }
            }
        }
        com.pullenti.unitext.UnitextDocument doc = com.pullenti.unitext.UnitextDocument._new69(fileName, com.pullenti.unitext.FileFormat.FB2);
        String coverImage = null;
        for (org.w3c.dom.Node x : (new com.pullenti.unisharp.XmlNodeListWrapper(xml.doc.getDocumentElement().getChildNodes())).arr) {
            if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(x), "body")) {
                String img = _loadBody(x, doc, footNotes, pars);
                if (img != null) 
                    coverImage = img;
                break;
            }
            else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(x), "description")) {
                for (org.w3c.dom.Node xx : (new com.pullenti.unisharp.XmlNodeListWrapper(x.getChildNodes())).arr) {
                    if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(xx), "title-info")) {
                        for (org.w3c.dom.Node xxx : (new com.pullenti.unisharp.XmlNodeListWrapper(xx.getChildNodes())).arr) {
                            if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(xxx), "book-title")) {
                                com.pullenti.unitext.internal.uni.UnitextGen gen = new com.pullenti.unitext.internal.uni.UnitextGen();
                                _GetUniText(xxx, gen, null, null, pars);
                                if (!pars.loadDocumentStructure) {
                                    com.pullenti.unitext.UnitextItem fi = gen.finish(true, null);
                                    if (fi != null) {
                                        doc.sections.add(new com.pullenti.unitext.UnitextPagesection());
                                        doc.sections.get(0).items.add(com.pullenti.unitext.UnitextPagesectionItem._new48(fi));
                                    }
                                }
                                if (!doc.attrs.containsKey("title")) 
                                    doc.attrs.put("title", xxx.getTextContent());
                            }
                            else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(xxx), "author")) {
                                for (org.w3c.dom.Node xxxx : (new com.pullenti.unisharp.XmlNodeListWrapper(xxx.getChildNodes())).arr) {
                                    if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(xxxx), "first-name")) {
                                        if (!doc.attrs.containsKey("author-firstname")) 
                                            doc.attrs.put("author-firstname", xxxx.getTextContent());
                                    }
                                    else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(xxxx), "last-name")) {
                                        if (!doc.attrs.containsKey("author-lastname")) 
                                            doc.attrs.put("author-lastname", xxxx.getTextContent());
                                    }
                                }
                            }
                        }
                    }
                }
                coverImage = loadTitleInfo(doc.attrs, x, null);
            }
        }
        if (!pars.onlyForPureText) {
            for (org.w3c.dom.Node x : (new com.pullenti.unisharp.XmlNodeListWrapper(xml.doc.getDocumentElement().getChildNodes())).arr) {
                if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(x), "binary")) {
                    String id = null;
                    if (x.getAttributes() != null) {
                        for (org.w3c.dom.Node a : (new com.pullenti.unisharp.XmlAttrListWrapper(x.getAttributes())).arr) {
                            if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "id")) 
                                id = a.getTextContent();
                        }
                    }
                    if (id == null) 
                        continue;
                    com.pullenti.unitext.UnitextImage img = (com.pullenti.unitext.UnitextImage)com.pullenti.unisharp.Utils.cast(doc.findById(id), com.pullenti.unitext.UnitextImage.class);
                    if (img != null) {
                        try {
                            img.content = java.util.Base64.getDecoder().decode((x.getTextContent()).replace("\r", "").replace("\n", ""));
                        } catch (Exception ex93) {
                        }
                    }
                }
            }
        }
        return doc;
    }

    private static String _loadBody(org.w3c.dom.Node x, com.pullenti.unitext.UnitextDocument doc, java.util.HashMap<String, com.pullenti.unitext.UnitextItem> footNotes, com.pullenti.unitext.CreateDocumentParam pars) {
        String coverImage = null;
        if (pars.loadDocumentStructure) {
            com.pullenti.unitext.UnitextDocblock dbl = new com.pullenti.unitext.UnitextDocblock();
            dbl.typname = "Document";
            dbl.body = new com.pullenti.unitext.UnitextContainer();
            com.pullenti.unitext.internal.uni.UnitextGen gg = new com.pullenti.unitext.internal.uni.UnitextGen();
            _GetUniText(x, gg, dbl, footNotes, pars);
            doc.content = dbl;
            for (org.w3c.dom.Node xx : (new com.pullenti.unisharp.XmlNodeListWrapper(x.getChildNodes())).arr) {
                if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(xx), "title")) {
                    com.pullenti.unitext.internal.uni.UnitextGen g = new com.pullenti.unitext.internal.uni.UnitextGen();
                    _GetUniText(xx, g, null, footNotes, pars);
                    dbl.head = com.pullenti.unitext.UnitextContainer._new94(com.pullenti.unitext.UnitextContainerType.HEAD);
                    com.pullenti.unitext.UnitextItem nn = g.finish(true, null);
                    com.pullenti.unitext.UnitextContainer ccc = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(nn, com.pullenti.unitext.UnitextContainer.class);
                    if (ccc == null) {
                        ccc = new com.pullenti.unitext.UnitextContainer();
                        ccc.children.add(nn);
                    }
                    ccc.typ = com.pullenti.unitext.UnitextContainerType.NAME;
                    dbl.head.children.add(ccc);
                    dbl.head.children.add(new com.pullenti.unitext.UnitextNewline());
                    break;
                }
            }
            if (coverImage != null) {
                if (coverImage.startsWith("#")) 
                    coverImage = coverImage.substring(1);
                if (dbl.head == null) 
                    dbl.head = com.pullenti.unitext.UnitextContainer._new94(com.pullenti.unitext.UnitextContainerType.HEAD);
                dbl.head.children.add(0, com.pullenti.unitext.UnitextImage._new96(coverImage));
                dbl.head.children.add(1, new com.pullenti.unitext.UnitextNewline());
            }
        }
        else {
            com.pullenti.unitext.internal.uni.UnitextGen gen = new com.pullenti.unitext.internal.uni.UnitextGen();
            _GetUniText(x, gen, null, footNotes, pars);
            doc.content = gen.finish(true, null);
        }
        return coverImage;
    }

    private static void _GetUniText(org.w3c.dom.Node xml, com.pullenti.unitext.internal.uni.UnitextGen gen, com.pullenti.unitext.UnitextDocblock dbl, java.util.HashMap<String, com.pullenti.unitext.UnitextItem> footNotes, com.pullenti.unitext.CreateDocumentParam pars) {
        for (org.w3c.dom.Node x : (new com.pullenti.unisharp.XmlNodeListWrapper(xml.getChildNodes())).arr) {
            if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(x), "#text")) {
                if (gen != null) 
                    gen.appendText(x.getTextContent(), false);
            }
            else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(x), "p") || com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(x), "v")) {
                gen.appendNewline(true, false);
                _GetUniText(x, gen, dbl, footNotes, pars);
                gen.appendNewline(false, false);
            }
            else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(x), "title")) {
                if (footNotes == null || pars.loadDocumentStructure) 
                    continue;
                gen.appendNewline(false, false);
                _GetUniText(x, gen, dbl, footNotes, pars);
                gen.appendNewline(false, false);
                gen.appendNewline(false, false);
            }
            else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(x), "section")) {
                if (!pars.loadDocumentStructure) {
                    gen.appendNewline(false, false);
                    _GetUniText(x, gen, null, footNotes, pars);
                    gen.appendPagebreak();
                }
                else {
                    com.pullenti.unitext.UnitextDocblock db = com.pullenti.unitext.UnitextDocblock._new38("Section");
                    if (com.pullenti.unisharp.Utils.stringsEq(dbl.typname, "Section")) 
                        db.typname = "Subsection";
                    else if (com.pullenti.unisharp.Utils.stringsEq(dbl.typname, "Subsection")) 
                        db.typname = "Chapter";
                    db.body = new com.pullenti.unitext.UnitextContainer();
                    ((com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(dbl.body, com.pullenti.unitext.UnitextContainer.class)).children.add(db);
                    for (org.w3c.dom.Node xx : (new com.pullenti.unisharp.XmlNodeListWrapper(x.getChildNodes())).arr) {
                        if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(xx), "title")) {
                            com.pullenti.unitext.internal.uni.UnitextGen g = new com.pullenti.unitext.internal.uni.UnitextGen();
                            _GetUniText(xx, g, null, footNotes, pars);
                            db.head = com.pullenti.unitext.UnitextContainer._new94(com.pullenti.unitext.UnitextContainerType.HEAD);
                            com.pullenti.unitext.UnitextItem nn = g.finish(true, null);
                            com.pullenti.unitext.UnitextContainer ccc = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(nn, com.pullenti.unitext.UnitextContainer.class);
                            if (ccc == null) {
                                ccc = new com.pullenti.unitext.UnitextContainer();
                                ccc.children.add(nn);
                            }
                            ccc.typ = com.pullenti.unitext.UnitextContainerType.NAME;
                            db.head.children.add(ccc);
                            db.head.children.add(new com.pullenti.unitext.UnitextNewline());
                            break;
                        }
                    }
                    com.pullenti.unitext.internal.uni.UnitextGen gg = new com.pullenti.unitext.internal.uni.UnitextGen();
                    _GetUniText(x, gg, db, footNotes, pars);
                    com.pullenti.unitext.UnitextItem bb = gg.finish(false, null);
                    if (bb == null) 
                        continue;
                    if (((com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(db.body, com.pullenti.unitext.UnitextContainer.class)).children.size() == 0) 
                        db.body = bb;
                    else 
                        ((com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(db.body, com.pullenti.unitext.UnitextContainer.class)).children.add(0, bb);
                    com.pullenti.unitext.UnitextContainer cnt = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(db.body, com.pullenti.unitext.UnitextContainer.class);
                    if (cnt != null && cnt.children.size() > 0 && (cnt.children.get(0) instanceof com.pullenti.unitext.UnitextNewline)) 
                        cnt.children.remove(0);
                }
            }
            else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(x), "empty-line")) 
                gen.appendNewline(false, false);
            else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(x), "image")) {
                String id = null;
                if (x.getAttributes() != null) {
                    for (org.w3c.dom.Node a : (new com.pullenti.unisharp.XmlAttrListWrapper(x.getAttributes())).arr) {
                        if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "href") && !com.pullenti.unisharp.Utils.isNullOrEmpty(a.getNodeValue()) && a.getNodeValue().charAt(0) == '#') {
                            id = a.getNodeValue().substring(1);
                            break;
                        }
                    }
                }
                if (id != null) 
                    gen.append(com.pullenti.unitext.UnitextImage._new96(id), null, -1, false);
            }
            else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(x), "a")) {
                String id = null;
                String typ = null;
                if (x.getAttributes() != null) {
                    for (org.w3c.dom.Node a : (new com.pullenti.unisharp.XmlAttrListWrapper(x.getAttributes())).arr) {
                        if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "href") && !com.pullenti.unisharp.Utils.isNullOrEmpty(a.getNodeValue()) && a.getNodeValue().charAt(0) == '#') 
                            id = a.getNodeValue().substring(1);
                        else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "type")) 
                            typ = a.getNodeValue();
                    }
                }
                if ((id != null && com.pullenti.unisharp.Utils.stringsEq(typ, "note") && footNotes != null) && footNotes.containsKey(id)) {
                    com.pullenti.unitext.UnitextFootnote fn = com.pullenti.unitext.UnitextFootnote._new100(x.getTextContent());
                    fn.content = footNotes.get(id);
                    gen.append(fn, null, -1, false);
                    continue;
                }
                gen.appendText(x.getTextContent(), false);
            }
            else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(x), "subtitle")) 
                gen.appendNewline(false, false);
            else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(x), "epigraph")) {
                com.pullenti.unitext.internal.uni.UnitextGen g = new com.pullenti.unitext.internal.uni.UnitextGen();
                _GetUniText(x, g, null, null, pars);
                com.pullenti.unitext.UnitextFootnote fn = new com.pullenti.unitext.UnitextFootnote();
                fn.content = g.finish(true, null);
                if (fn.content != null) 
                    gen.append(fn, null, -1, false);
            }
            else 
                _GetUniText(x, gen, dbl, footNotes, pars);
        }
    }

    public static String loadTitleInfo(java.util.HashMap<String, String> attrs, org.w3c.dom.Node xml, String nam) {
        String ret = null;
        for (org.w3c.dom.Node ch : (new com.pullenti.unisharp.XmlNodeListWrapper(xml.getChildNodes())).arr) {
            if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(ch), "image") && com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(xml), "coverpage")) {
                for (org.w3c.dom.Node a : (new com.pullenti.unisharp.XmlAttrListWrapper(ch.getAttributes())).arr) {
                    if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "href")) 
                        ret = a.getNodeValue();
                }
                continue;
            }
            String key = (nam == null ? com.pullenti.unisharp.Utils.getXmlLocalName(ch) : (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(ch), "#text") ? nam : (nam + "/" + com.pullenti.unisharp.Utils.getXmlLocalName(ch))));
            if (ch.getChildNodes().getLength() > 0) {
                String img = loadTitleInfo(attrs, ch, key);
                if (img != null) 
                    ret = img;
                continue;
            }
            String val = ch.getTextContent();
            if (com.pullenti.unisharp.Utils.isNullOrEmpty(val)) 
                continue;
            if (!attrs.containsKey(key)) 
                attrs.put(key, val);
            else 
                attrs.put(key, (attrs.get(key) + "; " + val));
        }
        return ret;
    }
    public Fb2Helper() {
    }
}
