package com.mime.dualscreenview.webcontents.contentsinfo

import com.mime.dualscreenview.webcontents.BaseWebContents

object Agit : BaseWebContents() {

    override var lastNumber : Int = 664

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

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

    override fun getContentsList(): String {
        return  " var datasets = [];\n" +
                " var pageNum = 0;\n" +
                " var totalCount = 0;\n" +
                " var currentIdx = 0;\n" +
                " function getContentList() {\n" +
                " \n" +
                "     if(document.getElementById('count_list') != undefined && totalCount <= 0) {\n" +
                "         totalCount = Number(document.getElementById('count_list').textContent);\n" +
                "         console.log(\"totalCount \" + totalCount);\n" +
                "         currentIdx = totalCount;\n" +
                "     }\n" +
                " \n" +
                "     var bookTitle = document.getElementsByClassName('pt-2')[1].textContent;\n" +
                "     var bookPageUrl = new URL(location.href).pathname;\n" +
                "     var pages = document.getElementById('id_page_novel_c').childNodes;\n" +
                "     var list = document.getElementById('id_list_novel_c').childNodes;\n" +
                "     console.log(\"list.length >> \" + list.length);\n" +
                "     for (i = 0; i < list.length; i++) {\n" +
                "         var onclickValue = list[i].attributes['onclick'].value;\n" +
                "         var pageUrl = onclickValue.split('location.href=')[1].replace(';', '');\n" +
                "         var chapterTitle = list[i].childNodes[1].children[0].textContent;\n" +
                "         var chapterID = list[i].childNodes[1].getAttribute('data-wr-id-c');\n" +
                " \n" +
                "         var data = {\n" +
                "             'chapterID': Number(chapterID),\n" +
                "             'idx': Number(currentIdx),\n" +
                "             'pathUrl': pageUrl,\n" +
                "             'bookPageUrl': bookPageUrl,\n" +
                "             'chapterTitle': chapterTitle,\n" +
                "             'bookTitle': bookTitle,\n" +
                "         };\n" +
                "         datasets.push(data);\n" +
                "         currentIdx = currentIdx - 1\n" +
                "     }\n" +
                " \n" +
                " \n" +
                " \n" +
                "     var currentTime = 500;\n" +
                "     try { currentTime = new Date().valueOf() % 1000;\n" +
                " \n" +
                "     } catch (e) {\n" +
                " \n" +
                "     }\n" +
                " \n" +
                "     console.log(\"pages.length >> \" + pages.length);\n" +
                "     console.log(\"datasets.length >> \" + datasets.length);\n" +
                "     console.log(\"totalCount >> \" + totalCount);\n" +
                "     console.log(\"pages.length >> \" + pages.length);\n" +
                " \n" +
                "     if ((pages.length > pageNum) ||\n" +
                "         (totalCount > datasets.length)) {\n" +
                "         pageNum += 1;\n" +
                "         get_data_novel_list_c(pageNum);\n" +
                "         setTimeout(function() {\n" +
                "             getContentList();\n" +
                "         }, 2000 + currentTime);\n" +
                "     } else if ((pages.length == pageNum) ||\n" +
                "                (totalCount > 10 &&\n" +
                "                totalCount == datasets.length)) {\n" +
                "         datasets.sort((a, b) => b.idx < a.idx);\n" +
                "         var i = 0;\n" +
                "         datasets.forEach(function(item) {\n" +
                "             item['chapterNum'] = i;\n" +
                "             i = i + 1;\n" +
                "          } );\n" +
                " \n" +
                "         PAgit.onBookInfo(JSON.stringify({\n" +
                "             'bookTitle': bookTitle,\n" +
                "             'bookPageUrl': new URL(location.href)\n" +
                "                 .pathname,\n" +
                "             'pages': datasets ,\n" +
                "         }));\n" +
                "     }\n" +
                " }\n" +
                " \n" +
                " function wait(sec) {\n" +
                "     let start = Date.now(),\n" +
                "         now = start;\n" +
                "     while (now - start < sec * 1000) {\n" +
                "         now = Date.now();\n" +
                "     }\n" +
                " }\n" +
                " getContentList();"
    }

    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('id_wr_content') != null ? document.getElementById('id_wr_content').innerText : null"
    }

    override fun checkCorrectContents(contents: String): String {
        return if (contents != null && !contents.isNullOrEmpty()) {
            contents
        } else {
            "fail load"
        }
    }

    override fun onLoadedJs(): String {
        return "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();"
    }
}