package com.kdanmobile.kdan_others_library_for_android.model


import com.google.gson.Gson
import com.google.gson.annotations.SerializedName


data class CreativeStoreRemoteData(@SerializedName("imgUrl")
                                   val imgUrl: Map<String, String>?,
                                   @SerializedName("imgClickUrl")
                                   val imgClickUrl: Map<String, String>?,
                                   @SerializedName("btnText")
                                   val btnTexts: List<Map<String, String>>?,
                                   @SerializedName("btnUrl")
                                   val btnUrls: List<Map<String, String>>?,
                                   @SerializedName("startTime")
                                   val startTime: Long = 0,
                                   @SerializedName("endTime")
                                   val endTime: Long = 0,
                                   @SerializedName("title")
                                   val title: Map<String, String>?,
                                   @SerializedName("content")
                                   val content: Map<String, String>?) {
    companion object {
        const val DEFAULT_LANGUAGE = "en"

        fun parse(jsonString: String): CreativeStoreRemoteData? {
            var creativeStoreRemoteData: CreativeStoreRemoteData? = null
            try {
                creativeStoreRemoteData = Gson().fromJson(jsonString, CreativeStoreRemoteData::class.java)
            } catch (e: Exception) {
            }
            return creativeStoreRemoteData
        }
    }

    private var currentTime: Long = 0
    private var deviceLanguage: String = DEFAULT_LANGUAGE

    fun setCurrentTime(currentTime: Long) {
        this.currentTime = currentTime
    }

    fun setDeviceLanguage(deviceLanguage: String) {
        this.deviceLanguage = deviceLanguage
    }

    fun isActivityPeriod(): Boolean {
        return (startTime <= currentTime) && (currentTime <= endTime)
    }

    fun isDataValid(): Boolean {
        var isBtnTextNonNull = false
        btnTexts?.let {
            for (btnText in it) {
                isBtnTextNonNull = btnText.isNotEmpty()
                isBtnTextNonNull = isBtnTextNonNull && (btnText[deviceLanguage] != null || btnText[DEFAULT_LANGUAGE] != null)
            }
        }
        var isBtnUrlNonNull = false
        btnUrls?.let {
            for (btnUrl in it) {
                isBtnUrlNonNull = btnUrl.isNotEmpty()
                isBtnUrlNonNull = isBtnUrlNonNull && (btnUrl[deviceLanguage] != null || btnUrl[DEFAULT_LANGUAGE] != null)

            }
        }

        return (title != null && (title[deviceLanguage] != null || title[DEFAULT_LANGUAGE] != null))
                && (content != null && (content[deviceLanguage] != null || content[DEFAULT_LANGUAGE] != null))
                && (imgUrl != null && (imgUrl[deviceLanguage] != null || imgUrl[DEFAULT_LANGUAGE] != null))
                && (imgClickUrl != null && (imgClickUrl[deviceLanguage] != null || imgClickUrl[DEFAULT_LANGUAGE] != null))
                && isBtnTextNonNull
                && isBtnUrlNonNull
    }

    fun getTitle(): String? {
        title?.let {
            return if (it[deviceLanguage] != null) it[deviceLanguage] else it[DEFAULT_LANGUAGE]
        }
        return null
    }

    fun getContent(): String? {
        content?.let {
            return if (content[deviceLanguage] != null) content[deviceLanguage] else content[DEFAULT_LANGUAGE]
        }
        return null
    }

    fun getImgUrl(): String? {
        imgUrl?.let {
            return if (imgUrl[deviceLanguage] != null) imgUrl[deviceLanguage] else imgUrl[DEFAULT_LANGUAGE]
        }
        return null
    }

    fun getImgClickUrl(): String? {
        imgClickUrl?.let {
            return if (imgClickUrl[deviceLanguage] != null) imgClickUrl[deviceLanguage] else imgClickUrl[DEFAULT_LANGUAGE]
        }
        return null
    }

    fun getBtnTextList(): List<String?> {
        val btnTextList = ArrayList<String?>()
        btnTexts?.let {
            for (btnTextMap in it) {
                val btnText = if (btnTextMap[deviceLanguage] != null) btnTextMap[deviceLanguage] else btnTextMap[DEFAULT_LANGUAGE]
                btnTextList.add(btnText)
            }
        }

        return btnTextList
    }

    fun getBtnUrlList(): List<String?> {
        val btnUrlList = ArrayList<String?>()
        btnUrls?.let {
            for (btnUrlMap in it) {
                val btnUrl = if (btnUrlMap[deviceLanguage] != null) btnUrlMap[deviceLanguage] else btnUrlMap[DEFAULT_LANGUAGE]
                btnUrlList.add(btnUrl)
            }
        }

        return btnUrlList
    }
}