package de.beyondsoft.ownchat.model;

import com.google.gson.annotations.SerializedName;

import de.beyondsoft.ownchat.data.repository.models.UserRealm;

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

/**
 * Created by laszloffiferenc on 2/21/17.
 */
public class User implements Parcelable {

    @SerializedName("oid")
    public long id;

    @SerializedName("firstname")
    public String firstName;

    @SerializedName("lastname")
    public String lastName;

    @SerializedName("picture")
    public String avatar;

    @SerializedName("mobile")
    public String mobile;

    @SerializedName("token")
    public String token;

    @SerializedName("is_employee")
    public boolean isEmployee;

    public User(long id, String firstName, String lastName, String avatar, String mobile, boolean isEmployee) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.avatar = avatar;
        this.mobile = mobile;
        this.isEmployee = isEmployee;
    }

    public String getName() {
        return firstName.concat(" ").concat(lastName);
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.firstName);
        dest.writeString(this.lastName);
        dest.writeString(this.avatar);
        dest.writeString(this.mobile);
        dest.writeByte(this.isEmployee ? (byte) 1 : (byte) 0);
    }

    private User(Parcel in) {
        this.firstName = in.readString();
        this.lastName = in.readString();
        this.avatar = in.readString();
        this.mobile = in.readString();
        this.isEmployee = in.readByte() != 0;
    }

    public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {
        @Override
        public User createFromParcel(Parcel source) {
            return new User(source);
        }

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

    public static User createFromUserRealm(UserRealm userRealm) {
        if (userRealm == null) {
            return null;
        }

        return new User(userRealm.getId(), userRealm.getFirstName(), userRealm.getLastName(),
                userRealm.getAvatar(), userRealm.getMobile(), userRealm.isEmployee());
    }
}
