package com.mime.dualscreenview.data.model

import io.realm.kotlin.ext.realmListOf
import io.realm.kotlin.types.RealmList
import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.annotations.Ignore
import io.realm.kotlin.types.annotations.PrimaryKey



class BookPageInfosJ  {
    var bookTitle : String = ""
    var bookPageUrl : String = ""

    var pages : ArrayList<BookPageInfoJ> = arrayListOf<BookPageInfoJ>()

    fun getTitleArray() : ArrayList<String> {
        var arrayList = ArrayList<String>()
        pages.forEach { arrayList.add(it.bookTitle ?: "") }
        return arrayList
    }

    fun getR() : BookPageInfos{
        var r = BookPageInfos()
        r.bookTitle = this.bookTitle
        r.bookPageUrl = this.bookPageUrl

        return r
    }
}
class BookPageInfoJ  {
    var chapterID : Int = 0
    var contents : String? = ""
    var bookPageUrl : String? = ""
    var chapterTitle : String? = ""
    var bookTitle : String? = ""
    var chapterNum : Int = 0
    var lastPage : Int? = 0
    var pathUrl : String? = ""

    fun getRealm() : BookPageInfo {
        var r = BookPageInfo()
        r.chapterID = this.chapterID
        r.contents = this.contents
        r.bookPageUrl = this.bookPageUrl ?: ""
        r.chapterTitle = this.chapterTitle
        r.bookTitle = this.bookTitle ?: ""
        r.chapterNum = this.chapterNum
        r.lastPage = this.lastPage
        r.pathUrl = this.pathUrl?.replace("'","")

        return r
    }
}


class BookPageInfos : RealmObject {

    var bookTitle : String = ""

    @PrimaryKey
    var bookPageUrl : String? = ""
    var pages : RealmList<BookPageInfo> = realmListOf()

    fun getTitleArray() : ArrayList<String> {
        var arrayList = ArrayList<String>()
        pages.forEach {
            arrayList.add(it.getTitleItem())}
        return arrayList
    }

    fun sort() {
        val comparator : Comparator<BookPageInfo> = compareBy { it.chapterID }
        pages.sortWith(comparator)
    }

    fun hasItem(item: BookPageInfo) : Boolean {
        var hasItem = false
        for (c in pages) {
            if (!hasItem) {
                hasItem = c.pathUrl.equals(item.pathUrl)
            }
        }
        return hasItem
    }


}
class BookPageInfo : RealmObject {

    var chapterID : Int = 0
    var contents : String? = ""

    var bookPageUrl : String? = ""
    var chapterTitle : String? = ""
    var bookTitle : String? = ""
    var chapterNum : Int = 0
    var lastPage : Int? = 0

    @PrimaryKey
    var pathUrl : String? = ""

    fun getTitleItem() : String {
        var result = StringBuilder()
        result.append(if ((contents?.length ?: 0) > 10) {
            "S:[0] "
        } else {
            "[X] "
        })

        result.append(chapterTitle?: "")

        result.append(if ((lastPage ?: 0) > 0) {
            " [0] "
        } else {
            " [X] "
        })

        return result.toString()
    }
}