package com.avg.zaap.crashlibrary;

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

import java.util.ArrayList;
import java.util.List;

/**
 * This class represents the crash report metadata.
 * <p/>
 * Created by marek.osvald on 18.7.2016.
 */
public class ReportData implements Parcelable {

    private static final int NO_MASK = 0;

    // common
    private String mCrashCode;
    private String mErrorName;
    private String mErrorMessage;
    private String mErrorDetails;
    private List<String> mFilesToPack;

    // CAP
    private String mCapExceptionCode;
    private int mCapFaultingOffset;
    private String mCapModuleName;
    private String mCapModuleVersion;
    private String mCapProcessName;
    private String mCapProcessVersion;
    private String mCapGUID;

    // offline
    private int mPublishAttempts;
    private long mLastAttemptTimestamp;

    /**
     * Default constructor.
     */
    public ReportData() {
        // reset publish attempts
        mPublishAttempts = 0;
    }

    /**
     * Parametric constructor. Reconstructs the metadata from a parcel.
     *
     * @param parcel parcel to reconstruct the metadata from
     */
    protected ReportData(Parcel parcel) {
        // common
        mCrashCode = parcel.readString();
        mErrorName = parcel.readString();
        mErrorMessage = parcel.readString();
        mErrorDetails = parcel.readString();
        mFilesToPack = new ArrayList<>();
        parcel.readStringList(mFilesToPack);

        // CAP
        mCapExceptionCode = parcel.readString();
        mCapFaultingOffset = parcel.readInt();
        mCapModuleName = parcel.readString();
        mCapModuleVersion = parcel.readString();
        mCapProcessName = parcel.readString();
        mCapProcessVersion = parcel.readString();
        mCapGUID = parcel.readString();

        // offline
        mPublishAttempts = parcel.readInt();
        mLastAttemptTimestamp = parcel.readLong();
    }

    // generated, not tested
    public static final Creator<ReportData> CREATOR = new Creator<ReportData>() {

        @Override
        public ReportData createFromParcel(Parcel in) {
            return new ReportData(in);
        }

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

    };

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // common
        dest.writeString(mCrashCode);
        dest.writeString(mErrorName);
        dest.writeString(mErrorMessage);
        dest.writeString(mErrorDetails);
        dest.writeStringList(mFilesToPack);

        // CAP
        dest.writeString(mCapExceptionCode);
        dest.writeInt(mCapFaultingOffset);
        dest.writeString(mCapModuleName);
        dest.writeString(mCapModuleVersion);
        dest.writeString(mCapProcessName);
        dest.writeString(mCapProcessVersion);
        dest.writeString(mCapGUID);

        // offline
        dest.writeInt(mPublishAttempts);
        dest.writeLong(mLastAttemptTimestamp);
    }

    /**
     * Returns unique crash code.
     *
     * @return unique crash code
     */
    public String getCrashCode() {
        return mCrashCode;
    }

    /**
     * Sets unique crash code.
     *
     * @param crashCode unique crash code to be set
     */
    public void setCrashCode(String crashCode) {
        mCrashCode = crashCode;
    }

    /**
     * Returns error name.
     *
     * @return error name
     */
    public String getErrorName() {
        return mErrorName;
    }

    /**
     * Sets error name.
     *
     * @param errorName error name to be set
     */
    public void setErrorName(String errorName) {
        mErrorName = errorName;
    }

    /**
     * Returns crash error message.
     *
     * @return crash error message
     */
    public String getErrorMessage() {
        return mErrorMessage;
    }

    /**
     * Sets crash error message.
     *
     * @param errorMessage error message to set
     */
    public void setErrorMessage(String errorMessage) {
        mErrorMessage = errorMessage;
    }

    /**
     * Returns crash error details.
     *
     * @return crash error details
     */
    public String getErrorDetails() {
        return mErrorDetails;
    }

    /**
     * Sets crash error details.
     *
     * @param errorDetails crash error details to be set
     */
    public void setErrorDetails(String errorDetails) {
        mErrorDetails = errorDetails;
    }

    /**
     * Returns a list of files to pack.
     *
     * @return a list fo files to pack
     */
    public List<String> getFilesToPack() {
        return mFilesToPack;
    }

    /**
     * Sets a list of files to pack.
     *
     * @param filesToPack a list of files to pack
     */
    public void setFilesToPack(List<String> filesToPack) {
        mFilesToPack = filesToPack;
    }

    /**
     * Returns CAP exception codeID.
     *
     * @return CAP exception code
     */
    public String getCapExceptionCode() {
        return mCapExceptionCode;
    }

    /**
     * Sets CAP exception code.
     *
     * @param capExceptionCode CAP exception code to be set
     */
    public void setCapExceptionCode(String capExceptionCode) {
        mCapExceptionCode = capExceptionCode;
    }

    /**
     * Returns CAP faulting offset in crashing module
     *
     * @return CAP faulting offset in crashing module
     */
    public int getCapFaultingOffset() {
        return mCapFaultingOffset;
    }

    /**
     * Sets CAP faulting offset in crashing module.
     *
     * @param capFaultingOffset CAP faulting offset in crashing module
     */
    public void setCapFaultingOffset(int capFaultingOffset) {
        mCapFaultingOffset = capFaultingOffset;
    }

    /**
     * Returns CAP crashing module name.
     *
     * @return CAP crashing module name
     */
    public String getCapModuleName() {
        return mCapModuleName;
    }

    /**
     * Sets CAP crashing module name.
     *
     * @param capModuleName CAP crashing module name to be set
     */
    public void setCapModuleName(String capModuleName) {
        mCapModuleName = capModuleName;
    }

    /**
     * Returns CAP crashing module version.
     *
     * @return CAP crashing module version
     */
    public String getCapModuleVersion() {
        return mCapModuleVersion;
    }

    /**
     * Sets CAP crashing module version.
     *
     * @param capModuleVersion CAP crashing module version to be set
     */
    public void setCapModuleVersion(String capModuleVersion) {
        mCapModuleVersion = capModuleVersion;
    }

    /**
     * Returns CAP crashing process name.
     *
     * @return CAP crashing process name
     */
    public String getCapProcessName() {
        return mCapProcessName;
    }

    /**
     * Sets CAP CAP crashing process name.
     *
     * @param capProcessName CAP crashing process name to be set
     */
    public void setCapProcessName(String capProcessName) {
        mCapProcessName = capProcessName;
    }

    /**
     * Returns CAP crashing process version.
     *
     * @return CAP crashing process version
     */
    public String getCapProcessVersion() {
        return mCapProcessVersion;
    }

    /**
     * Sets CAP crashing process version.
     *
     * @param capProcessVersion CAP crashing process version to be set
     */
    public void setCapProcessVersion(String capProcessVersion) {
        mCapProcessVersion = capProcessVersion;
    }

    /**
     * Returns CAP unique message GUID.
     *
     * @return CAP unique message GUID
     */
    public String getCapGUID() {
        return mCapGUID;
    }

    /**
     * Sets CAP unique message GUID.
     *
     * @param capGUID CAP unique message GUID to be set
     */
    public void setCapGUID(String capGUID) {
        mCapGUID = capGUID;
    }

    /**
     * Returns the number of publishing attempts.
     *
     * @return the number of publishing attempts
     */
    public int getPublishAttempts() {
        return mPublishAttempts;
    }

    /**
     * Sets the number of publishing attempts.
     *
     * @param publishAttempts publishing attempts to be set
     */
    public void setPublishAttempts(int publishAttempts) {
        mPublishAttempts = publishAttempts;
    }

    /**
     * Increments publish attempts by one.
     */
    public void incrementsPublishAttempts() {
        mPublishAttempts++;
    }

    /**
     * Returns the last attempt timestamp.
     *
     * @return the last attempt timestamp
     */
    public long getLastAttemptTimestamp() {
        return mLastAttemptTimestamp;
    }

    /**
     * Sets the last attempt timestamp.
     *
     * @param lastAttemptTimestamp last attempt timestamp to be set
     */
    public void setLastAttemptTimestamp(long lastAttemptTimestamp) {
        mLastAttemptTimestamp = lastAttemptTimestamp;
    }

}
