package com.example.myapplication.api.model

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import com.example.myapplication.database.entities.Product
import kotlinx.serialization.Serializable
import java.io.ByteArrayOutputStream

@Serializable
data class ProductRemote(
    val id: Int = 0,
    val name: String,
    val info: String,
    val price: Double,
    val img: ByteArray,
    val categoryId: Int
)

fun ProductRemote.toProduct(): Product {
    val imgBitmap: Bitmap = BitmapFactory.decodeByteArray(img, 0, img.size)
    return Product(
        id,
        name,
        info,
        price,
        imgBitmap,
        categoryId
    )
}

fun Product.toProductRemote(): ProductRemote {
    val outputStream = ByteArrayOutputStream();
    img.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
    val imgByteArr: ByteArray = outputStream.toByteArray()

    return ProductRemote(
        0,
        name,
        info,
        price,
        imgByteArr,
        categoryId
    )
}