package org.foxarmy.barcodescannerforemployees.dataclasses

import android.os.Build
import android.os.Parcel
import android.os.Parcelable
import androidx.annotation.RequiresApi
import org.foxarmy.barcodescannerforemployees.calculateProductFreshness

class Product() : Parcelable {
    var id = 0
    var abstractProductId = 0
    var amount = 0
    var dateOfProduction: Long = 0
    var dateOfExpiry: Long = 0
    var freshness: Double = 0.0

    @RequiresApi(Build.VERSION_CODES.O)
    constructor(id: Int, abstractProductId: Int, amount: Int, dateOfProduction: Long, dateOfExpiry: Long) : this() {
        this.id = id
        this.abstractProductId = abstractProductId
        this.amount = amount
        this.dateOfProduction = dateOfProduction
        this.dateOfExpiry = dateOfExpiry
        this.freshness = calculateProductFreshness(dateOfProduction, dateOfExpiry)
    }

    constructor(parcel: Parcel) : this() {
        id = parcel.readInt()
        abstractProductId = parcel.readInt()
        amount = parcel.readInt()
        dateOfProduction = parcel.readLong()
        dateOfExpiry = parcel.readLong()
        freshness = parcel.readDouble()
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeInt(id)
        parcel.writeInt(abstractProductId)
        parcel.writeInt(amount)
        parcel.writeLong(dateOfProduction)
        parcel.writeLong(dateOfExpiry)
        parcel.writeDouble(freshness)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<Product> {
        override fun createFromParcel(parcel: Parcel): Product {
            return Product(parcel)
        }

        override fun newArray(size: Int): Array<Product?> {
            return arrayOfNulls(size)
        }
    }
}