package com.adealink.weparty.room.theme.data

import android.util.Patterns
import com.adealink.frame.storage.file.WeFile

enum class DisplayThemeType {
    Default,//默认主题
    Static,//静态主题
    Dynamic,//动态主题
}

enum class DynamicThemeResourceType(val suffix: String) {
    UnKnow(""),
    SVGA("svga"),
    MP4("mp4")
}

data class ThemeResource(
    val staticResourceUrl: String? = null,
    val dynamicResourceUrl: String? = null,
    var savePath: WeFile? = null,
) {
    fun getDisplayType(): DisplayThemeType {
        if (dynamicResourceUrl?.isNotEmpty() == true) {
            return DisplayThemeType.Dynamic
        } else if (staticResourceUrl?.isNotEmpty() == true) {
            return DisplayThemeType.Static
        }
        return DisplayThemeType.Default
    }

    fun getDynamicResourceType(): DynamicThemeResourceType {
        return when {
            dynamicResourceUrl?.endsWith(".svga") == true -> {
                DynamicThemeResourceType.SVGA
            }

            dynamicResourceUrl?.endsWith(".mp4") == true -> {
                DynamicThemeResourceType.MP4
            }

            else -> {
                DynamicThemeResourceType.UnKnow
            }
        }
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as ThemeResource

        if (staticResourceUrl != other.staticResourceUrl) return false
        if (dynamicResourceUrl != other.dynamicResourceUrl) return false
        if (savePath != other.savePath) return false

        return true
    }

    fun isDynamicResource(): Boolean {
        return !dynamicResourceUrl.isNullOrEmpty() && Patterns.WEB_URL.matcher(dynamicResourceUrl)
            .matches()
    }

    override fun hashCode(): Int {
        var result = staticResourceUrl?.hashCode() ?: 0
        result = 31 * result + (dynamicResourceUrl?.hashCode() ?: 0)
        result = 31 * result + (savePath?.hashCode() ?: 0)
        return result
    }

    companion object {
        @JvmStatic
        val EMPTY_THEME = ThemeResource("", "", null)
    }
}
