package com.mime.dualscreenview.webcontents

import android.util.Log
import android.webkit.*
import com.mime.dualscreenview.webcontents.contentsinfo.ActionByBool
import com.mime.dualscreenview.webcontents.contentsinfo.ContentsInfoInterface
import com.mime.dualscreenview.webcontents.contentsinfo.DidFindContents


abstract class BaseWebContents : ContentsInfoInterface {
    open var lastNumber : Int = 221
    var completeAction : ActionByBool? = null
//    val definedActionCount = 5
//    var completeActionCount = 0
//        set(value) {
//            field = value
//            if(field == definedActionCount && completeAction != null) {
//                completeAction?.invoke(true)
//            }
//        }

    fun findListItem(webview: WebView, callBakItems : DidFindContents) {
        webview.evaluateJavascript(getContentsList()) { result ->
            callBakItems.invoke(result)
        }
    }

    fun doOnloaded(webview: WebView, findContents : DidFindContents, completeAction : ActionByBool) {
//        completeActionCount = 0
        this.completeAction = completeAction
//        webview.evaluateJavascript(getTitleJs()) { result : String? ->
//            result?.let { resultString ->
//                findTitle.invoke(resultString)
//            }
//            completeActionCount = completeActionCount + 1
//        }
        webview.evaluateJavascript(getFindContentsJs()) { result : String? ->
            result?.let { resultString ->
                checkCorrectContents(resultString)?.let { contents ->
                    if(resultString != null && !"null".equals(resultString) && !resultString.isNullOrEmpty()) {
                        findContents.invoke(contents)
                    } else {
                        findContents.invoke(null)
                    }
                }
            } ?: findContents.invoke(null)
//            completeActionCount = completeActionCount + 1
        }
//        webview.evaluateJavascript("document.getElementById('${getNextButtonJs()}')") { result : String? ->
//            result?.let { resultString ->
//                Log.e("BaseWebContents", "getNextButtonJs() >> ${resultString}")
//                if(resultString != null && !"null".equals(resultString)) {
//                    findNextButton.invoke(resultString)
//                } else {
//                    findNextButton.invoke(null)
//                }
//            }
//            completeActionCount = completeActionCount + 1
//        }


//        webview.evaluateJavascript("document.getElementById('${getPrevButtonJs()}')") { result : String? ->
//            result?.let { resultString ->
//                Log.e("BaseWebContents", "getPrevButtonJs() >> ${resultString}")
//                if(resultString != null && !"null".equals(resultString)) {
//                    findPrevButton.invoke(resultString)
//                }else {
//                    findPrevButton.invoke(null)
//                }
//            }
//            completeActionCount = completeActionCount + 1
//        }

        webview.evaluateJavascript(onLoadedJs()) {
            Log.e("BaseWebContents", "onLoadedJs() >> ${it}")
//            completeActionCount = completeActionCount + 1
        }
        completeAction?.invoke(true)
    }

}
