package com.amazon.ads.ema.api.model;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * Status of OTA updates for CMS and OS.
 * Used to report current update state to Partner API clients.
 */
public class UpdateStatus implements Parcelable {
    public int code;                    // HTTP-style status code
    public String message;              // Response message
    public String timestamp;            // ISO 8601 timestamp

    // CMS Update Info
    public boolean cmsUpdateAvailable;
    public String cmsPackageName;
    public String cmsCurrentVersion;
    public String cmsTargetVersion;
    public String cmsUpdateStatus;      // "none", "pending", "downloading", "downloaded", "installing", "completed", "failed"
    public int cmsDownloadProgress;     // 0-100

    // OS Update Info
    public boolean osUpdateAvailable;
    public String osCurrentVersion;
    public String osTargetVersion;
    public String osUpdateStatus;       // "none", "pending", "downloading", "downloaded", "installing", "completed", "failed"
    public int osDownloadProgress;      // 0-100

    public UpdateStatus() {
        cmsUpdateStatus = "none";
        osUpdateStatus = "none";
    }

    protected UpdateStatus(Parcel in) {
        code = in.readInt();
        message = in.readString();
        timestamp = in.readString();
        cmsUpdateAvailable = in.readByte() != 0;
        cmsPackageName = in.readString();
        cmsCurrentVersion = in.readString();
        cmsTargetVersion = in.readString();
        cmsUpdateStatus = in.readString();
        cmsDownloadProgress = in.readInt();
        osUpdateAvailable = in.readByte() != 0;
        osCurrentVersion = in.readString();
        osTargetVersion = in.readString();
        osUpdateStatus = in.readString();
        osDownloadProgress = in.readInt();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(code);
        dest.writeString(message);
        dest.writeString(timestamp);
        dest.writeByte((byte) (cmsUpdateAvailable ? 1 : 0));
        dest.writeString(cmsPackageName);
        dest.writeString(cmsCurrentVersion);
        dest.writeString(cmsTargetVersion);
        dest.writeString(cmsUpdateStatus);
        dest.writeInt(cmsDownloadProgress);
        dest.writeByte((byte) (osUpdateAvailable ? 1 : 0));
        dest.writeString(osCurrentVersion);
        dest.writeString(osTargetVersion);
        dest.writeString(osUpdateStatus);
        dest.writeInt(osDownloadProgress);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<UpdateStatus> CREATOR = new Creator<UpdateStatus>() {
        @Override
        public UpdateStatus createFromParcel(Parcel in) {
            return new UpdateStatus(in);
        }

        @Override
        public UpdateStatus[] newArray(int size) {
            return new UpdateStatus[size];
        }
    };
}
