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

    public static com.pullenti.unitext.UnitextDocument createDoc(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();
        try {
            java.util.HashMap<String, com.pullenti.unitext.UnitextItem> textEntries = new java.util.HashMap<String, com.pullenti.unitext.UnitextItem>();
            java.util.ArrayList<String> itemsOrder = new java.util.ArrayList<String>();
            java.util.HashMap<String, String> imagesEntries = new java.util.HashMap<String, String>();
            for (MyZipEntry entry : zip.entries) {
                if (com.pullenti.unisharp.Utils.endsWithString(entry.name, "content.opf", true)) {
                    com.pullenti.unisharp.XmlDocumentWrapper xml = new com.pullenti.unisharp.XmlDocumentWrapper();
                    byte[] dat = entry.getData();
                    if (dat == null) 
                        continue;
                    try (com.pullenti.unisharp.MemoryStream documentXml = new com.pullenti.unisharp.MemoryStream(dat)) {
                        xml.load(documentXml);
                        documentXml.close();
                    }
                    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), "manifest")) {
                            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), "item")) {
                                    String typ = null;
                                    String id = null;
                                    String href = 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.getNodeValue();
                                            else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "href")) 
                                                href = a.getNodeValue();
                                            else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "media-type")) 
                                                typ = a.getNodeValue();
                                        }
                                    }
                                    if (typ == null || href == null) 
                                        continue;
                                    if (typ.startsWith("application") && (typ.indexOf("xhtml") >= 0)) {
                                        if (!textEntries.containsKey(href)) 
                                            textEntries.put(href, null);
                                        itemsOrder.add(href);
                                    }
                                    else if (typ.startsWith("image/")) {
                                        if (!imagesEntries.containsKey(href)) 
                                            imagesEntries.put(href, null);
                                    }
                                }
                            }
                        }
                        else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(x), "metadata")) {
                            for (org.w3c.dom.Node xxx : (new com.pullenti.unisharp.XmlNodeListWrapper(x.getChildNodes())).arr) {
                                if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(xxx), "title")) {
                                    if (!doc.attrs.containsKey("title")) 
                                        doc.attrs.put("title", xxx.getTextContent());
                                }
                                else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(xxx), "creator")) {
                                    boolean isAuth = false;
                                    if (xxx.getAttributes() != null) {
                                        for (org.w3c.dom.Node a : (new com.pullenti.unisharp.XmlAttrListWrapper(xxx.getAttributes())).arr) {
                                            if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "role")) {
                                                if (com.pullenti.unisharp.Utils.stringsEq(a.getNodeValue(), "aut")) 
                                                    isAuth = true;
                                            }
                                        }
                                    }
                                    if (isAuth) {
                                        if (!doc.attrs.containsKey("author")) 
                                            doc.attrs.put("author", xxx.getTextContent());
                                    }
                                }
                            }
                        }
                    }
                }
            }
            byte[] buf = new byte[100000];
            for (MyZipEntry entry : zip.entries) {
                String txt = null;
                for (java.util.Map.Entry<String, com.pullenti.unitext.UnitextItem> kp : textEntries.entrySet()) {
                    if (entry.name.endsWith(kp.getKey())) {
                        txt = kp.getKey();
                        break;
                    }
                }
                if (txt == null) 
                    continue;
                if ((txt.indexOf("annotation") >= 0) || (txt.indexOf("about") >= 0) || (txt.indexOf("info") >= 0)) 
                    continue;
                try {
                    byte[] dat = entry.getData();
                    com.pullenti.unitext.internal.html.HtmlNode nod = com.pullenti.unitext.internal.html.HtmlHelper.createNode(null, dat, null);
                    if (nod == null) 
                        continue;
                    com.pullenti.unitext.UnitextDocument doc0 = com.pullenti.unitext.internal.html.HtmlHelper.create(nod, null, null, new com.pullenti.unitext.CreateDocumentParam());
                    if (doc0 != null && doc0.content != null) {
                        if ((txt.indexOf("title") >= 0)) {
                            doc.sections.add(new com.pullenti.unitext.UnitextPagesection());
                            doc.sections.get(0).items.add(com.pullenti.unitext.UnitextPagesectionItem._new48(doc0.content));
                        }
                        else 
                            textEntries.put(txt, doc0.content);
                    }
                } catch (Exception ex) {
                }
            }
            com.pullenti.unitext.UnitextContainer cnt = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(doc.content, com.pullenti.unitext.UnitextContainer.class);
            for (String key : itemsOrder) {
                if (textEntries.containsKey(key)) {
                    com.pullenti.unitext.UnitextItem ccc = textEntries.get(key);
                    if (ccc == null) 
                        continue;
                    if (com.pullenti.unisharp.Utils.endsWithString(key, "TOC.NCX", true)) 
                        continue;
                    if ((key.toUpperCase().indexOf("CONTENT.") >= 0)) 
                        continue;
                    if (cnt.children.size() > 0) 
                        cnt.children.add(new com.pullenti.unitext.UnitextPagebreak());
                    if (ccc instanceof com.pullenti.unitext.UnitextContainer) 
                        com.pullenti.unisharp.Utils.addToArrayList(cnt.children, ((com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(ccc, com.pullenti.unitext.UnitextContainer.class)).children);
                    else 
                        cnt.children.add(ccc);
                }
            }
            java.util.ArrayList<com.pullenti.unitext.UnitextItem> its = new java.util.ArrayList<com.pullenti.unitext.UnitextItem>();
            doc.getAllItems(its, 0);
            for (MyZipEntry entry : zip.entries) {
                if (pars.onlyForPureText) 
                    break;
                com.pullenti.unitext.UnitextImage img = null;
                for (com.pullenti.unitext.UnitextItem it : its) {
                    if (it instanceof com.pullenti.unitext.UnitextImage) {
                        if (entry.name.endsWith(it.id)) {
                            img = (com.pullenti.unitext.UnitextImage)com.pullenti.unisharp.Utils.cast(it, com.pullenti.unitext.UnitextImage.class);
                            break;
                        }
                    }
                }
                if (img == null) 
                    continue;
                img.content = entry.getData();
            }
        } finally {
            if (zip != null) 
                zip.close();
        }
        doc.optimize(false, null);
        return doc;
    }
    public EpubHelper() {
    }
}
