package com.mime.dualscreenview.webcontents.contentsinfo

import com.mime.dualscreenview.webcontents.BaseWebContents

object Booktoki : BaseWebContents() {

    override var lastNumber : Int = 351

    override fun getWebcontentsName(): String {
        return "Booktoki"
    }

    override fun getLastedDoamin(): String {
        return String.format("https://booktoki%d.com/", lastNumber)
    }

    override fun getContentsList(): String {
        return  "function getList()  {\n" +
                "    const contentsArray = [];\n" +
                "    var children = document.getElementsByClassName('list-body')[0].children;\n" +
                "    var maxCount =  children.length;\n" +
                "    for (i= 0; i < maxCount; i++) {\n" +
                "        var chapterNum = children[i].getElementsByClassName('wr-num')[0].textContent;\n" +
                "        var pageUrl = children[i].getElementsByClassName('wr-subject')[0].getElementsByTagName('a')[0].href;\n" +
                "        if (pageUrl != null && pageUrl.length > 0 && pageUrl.startsWith(\"http\")) {\n" +
                "            pageUrl = new URL(pageUrl).pathname;\n" +
                "        }\n" +
                "        var chapterTitle = children[i].getElementsByClassName('wr-subject')[0].getElementsByTagName('a')[0].innerText;\n" +
                "        if(chapterTitle.split('\\n').length > 1) {\n" +
                "            chapterTitle = chapterTitle.split('\\n')[1];\n" +
                "        }\n" +
                "\n" +
                "        var bookPageUrl = location.pathname;\n" +
                "        var bookTitle = document.getElementsByClassName('view-title')[0].getElementsByTagName('span')[0].innerText;\n" +
                "        var data = {\n" +
                "            'chapterID': Number(chapterNum),\n" +
                "            'chapterNum': Number(chapterNum),\n" +
                "            'pathUrl': pageUrl,\n" +
                "            'bookPageUrl': bookPageUrl,\n" +
                "            'chapterTitle': chapterTitle,\n" +
                "            'bookTitle': bookTitle,\n" +
                "        };\n" +
                "        contentsArray.push(\n" +
                "            data\n" +
                "        );\n" +
                "    }\n" +
                "    \n" +
                "    if(contentsArray.length > 0) {\n" +
                "        PAgit.onBookInfo(JSON.stringify({\n" +
                "            'bookTitle': bookTitle,\n" +
                "            'bookPageUrl': new URL(location.href).pathname,\n" +
                "            'pages': contentsArray ,\n" +
                "        }));\n" +
                "    }\n" +
                "}\n" +
                "getList()\n" +
                ""
    }

    override fun acccceptResourceKeyword(): String {
        return "toki"
    }

    override fun getNextButtonJs(): String {
        return "goNextBtn"
    }

    override fun getPrevButtonJs(): String {
        return "goPrevBtn"
    }

    override fun getTitleJs(): String {
        return "document.getElementsByClassName(\"toon-title\").length > 0 ? document.getElementsByClassName(\"toon-title\")[0].title : null"
    }

    override fun getFindContentsJs(): String {
        return "document.getElementById(\"novel_content\") != null ? document.getElementById(\"novel_content\").innerText : null"
    }

    override fun checkCorrectContents(contents: String): String {
        return if (contents != null && !contents.isNullOrEmpty()) {
            contents
        } else {
            "fail load"
        }
    }
//    "if(document.getElementsByClassName(\"hd_pops\") != null && document.getElementsByClassName(\"hd_pops\").length > 0) document.getElementsByClassName(\"hd_pops\")[0].remove();" +
//    "if(document.getElementsByClassName(\"hd_pops\") != null && document.getElementsByClassName(\"hd_pops\").length > 0) document.getElementsByClassName(\"hd_pops\")[0].remove();" +
//    "if(document.getElementById(\"main-banner-view\") != null) document.getElementById(\"main-banner-view\").remove();" +
//    "if(document.getElementsByClassName(\"board-tail-banner\") != null && document.getElementsByClassName(\"board-tail-banner\").length > 0)document.getElementsByClassName(\"board-tail-banner\")[0].remove();" +
//    "if(document.getElementById(\"id_mbv\") != null)document.getElementById(\"id_mbv\").remove();"
    override fun onLoadedJs(): String {
        return ""
    }
}