package com.avg.zaap.crashlibrary;

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

import com.avg.zaap.crashlibrary.offline.Connection;

/**
 * This class serves as the structure holding necessary connection status data.
 * <p/>
 * Created by marek.osvald on 26.7.2016.
 */
public class ConnectionStatus implements Parcelable {

    private static final int TRUE = 1;
    private static final int FALSE = 0;
    private static final int NO_MASK = 0;

    private Connection mConnection;
    private boolean mConnected;
    private boolean mRoaming;

    /**
     * Default constructor.
     */
    public ConnectionStatus() {
    }

    /**
     * Parametric constructor. Reconstruct connection status from a parcel.
     *
     * @param in parcel to reconstruct he connection status from
     */
    protected ConnectionStatus(Parcel in) {
        mConnection = Connection.values()[in.readInt()];
        mConnected = in.readInt() != FALSE;
        mRoaming = in.readInt() != FALSE;
    }

    // generated not tested
    public static final Creator<ConnectionStatus> CREATOR = new Creator<ConnectionStatus>() {
        @Override
        public ConnectionStatus createFromParcel(Parcel in) {
            return new ConnectionStatus(in);
        }

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

    /**
     * Returns type of connection.
     *
     * @return type of connection
     */
    public Connection getConnection() {
        return mConnection;
    }

    /**
     * Sets type of connection.
     *
     * @param connection connection type to be set
     */
    public void setConnection(Connection connection) {
        mConnection = connection;
    }

    /**
     * Returns whether the device is connected or not.
     *
     * @return true, provided that the device is connected, false otherwise
     */
    public boolean isConnected() {
        return mConnected;
    }

    /**
     * Sets whether the device is connected or not.
     *
     * @param connected whether the device is connected or not
     */
    public void setConnected(boolean connected) {
        mConnected = connected;
    }

    /**
     * Returns whether the device is currently roaming or not.
     *
     * @return true, provided that device is currently roaming, false otherwise
     */
    public boolean isRoaming() {
        return mRoaming;
    }

    /**
     * Sets whether the device is currently roaming or not.
     *
     * @param roaming whether the device is currently roaming or not
     */
    public void setRoaming(boolean roaming) {
        mRoaming = roaming;
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(mConnection.ordinal());
        dest.writeInt(mConnected ? TRUE : FALSE);
        dest.writeInt(mRoaming ? TRUE : FALSE);
    }

    @Override
    public String toString() {
        return "ConnectionStatus {" +
                "mConnection=" + mConnection +
                ", mConnected=" + mConnected +
                ", mRoaming=" + mRoaming +
                " }";
    }

}
