package com.foxx.androidcast.ota;

/*********************************************************************
 * OtaVersion.java
 * Created at: Wed 20 May 2026 14:31:55 +0200
 * Updated at: Wed 20 May 2026 15:17:13 +0200 by Anton Afanasyeu <a.afanasieff@gmail.com>
 * Commit: 5d8e82d2e60a21fff3138d2a394ee4e8b4c6dcb8
 * class OtaVersion
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (2 commits, 56 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	4a7d39b7a223c2361aad2601f067cf91c9203dd292f64d209a5de4d78bb33d34
**********************************************************************/
import com.foxx.androidcast.BuildConfig;

/** major.minor.build packed the same way as Gradle {@code versionCode}. */
public final class OtaVersion {
    public static final int MAX_COMPONENT = 99;

    public final int major;
    public final int minor;
    public final int build;

    public OtaVersion(int major, int minor, int build) {
        this.major = major;
        this.minor = minor;
        this.build = build;
    }

    public static int pack(int major, int minor, int build) {
        if (major < 0 || minor < 0 || build < 0
                || major > MAX_COMPONENT || minor > MAX_COMPONENT || build > MAX_COMPONENT) {
            throw new IllegalArgumentException("OTA version out of range 0..99");
        }
        return major * 10_000 + minor * 100 + build;
    }

    public int packed() {
        return pack(major, minor, build);
    }

    public String displayName() {
        return major + "." + minor + "." + build;
    }

    public static OtaVersion fromPacked(int packed) {
        int major = packed / 10_000;
        int minor = (packed / 100) % 100;
        int build = packed % 100;
        return new OtaVersion(major, minor, build);
    }

    public static OtaVersion installed() {
        return new OtaVersion(BuildConfig.VERSION_MAJOR, BuildConfig.VERSION_MINOR, BuildConfig.VERSION_BUILD);
    }

    public static boolean isUpdateAvailable(int remotePacked, int installedPacked) {
        return remotePacked > installedPacked;
    }
}
