package tk.zwander.common.util.migrations

import android.content.Context
import tk.zwander.common.data.WidgetSizeData
import tk.zwander.common.util.prefManager

class WidgetSizeMigration : Migration {
    override val runBelowDatabaseVersion: Int
        get() = 2

    override fun run(context: Context) {
        context.apply {
            @Suppress("DEPRECATION")
            val sizeInfos = prefManager.widgetSizes
            val widgets = prefManager.currentWidgets

            prefManager.currentWidgets = LinkedHashSet(
                widgets.map { widget ->
                    val sizeInfo = widget.size
                        ?: sizeInfos[widget.id]
                        ?: WidgetSizeData(1, 1)

                    widget.copy(size = sizeInfo)
                }
            )

            @Suppress("DEPRECATION")
            prefManager.widgetSizes = hashMapOf()
        }
    }
}
