package com.p3.bewoplaner.data.repository.models;

import com.p3.bewoplaner.model.Message;

import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;

/**
 * Created by Rares Teodorescu on 22/02/2017.
 */
public class MessageRealm extends RealmObject {

    public static final String ID = "id";
    public static final String GROUP_ID = "groupId";
    public static final String CREATED_AT = "createdAt";
    public static final String USER_ID = "userId";

    @PrimaryKey
    private String uniqueId;
    private String id;
    private long groupId;
    private String message;
    private String filePath;
    private String smallerImage;
    private String filename;
    private String uri;
    private long createdAt;
    private String senderAvatar;
    private String senderName;
    private long senderId;
    private int status;
    private boolean isRead;
    private long userId;
    private boolean fileDownloaded;

    public MessageRealm() {
    }

    public MessageRealm(String id,
                        int groupId,
                        String message,
                        String filePath,
                        String smallerImage,
                        String filename,
                        String uri,
                        long createdAt,
                        int senderId,
                        String senderName,
                        String senderAvatar,
                        Message.Status status,
                        boolean isRead,
                        long userId,
                        boolean fileDownloaded) {
        this.id = id;
        this.groupId = groupId;
        this.message = message;
        this.filePath = filePath;
        this.smallerImage = smallerImage;
        this.filename = filename;
        this.uri = uri;
        this.createdAt = createdAt;
        this.senderId = senderId;
        this.senderName = senderName;
        this.senderAvatar = senderAvatar;
        this.status = Message.Status.getValue(status);
        this.isRead = isRead;
        this.userId = userId;
        this.fileDownloaded = fileDownloaded;
    }

    public long getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(long createdAt) {
        this.createdAt = createdAt;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public String getId() {
        return id;
    }

    public String getUniqueId() {
        return uniqueId;
    }

    public void setUniqueId(String uniqueId) {
        this.uniqueId = uniqueId;
    }

    public void setId(String id) {
        this.id = id;
    }

    public static String getID() {
        return ID;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getSenderAvatar() {
        return senderAvatar;
    }

    public void setSenderAvatar(String senderAvatar) {
        this.senderAvatar = senderAvatar;
    }

    public long getSenderId() {
        return senderId;
    }

    public void setSenderId(long senderId) {
        this.senderId = senderId;
    }

    public String getSenderName() {
        return senderName;
    }

    public void setSenderName(String senderName) {
        this.senderName = senderName;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public long getGroupId() {
        return groupId;
    }

    public void setGroupId(long groupId) {
        this.groupId = groupId;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public String getUri() {
        return uri;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public boolean isRead() {
        return isRead;
    }

    public void setRead(boolean read) {
        isRead = read;
    }

    public boolean isFileDownloaded() {
        return fileDownloaded;
    }

    public void setFileDownloaded(boolean fileDownloaded) {
        this.fileDownloaded = fileDownloaded;
    }

    public String getSmallerImage() {
        return smallerImage;
    }

    public void setSmallerImage(String smallerImage) {
        this.smallerImage = smallerImage;
    }

    public static MessageRealm createFromMessage(Message message) {
        if (message == null) {
            return null;
        }
        MessageRealm messageRealm = new MessageRealm();
        messageRealm.setId(message.id);
        messageRealm.setGroupId(message.groupId);
        messageRealm.setMessage(message.message);
        messageRealm.setFilePath(message.filePath);
        messageRealm.setSmallerImage(message.smallerImage);
        messageRealm.setFilename(message.originalFilename);
        messageRealm.setUri(message.uri);
        messageRealm.setCreatedAt(message.createdAt * 1000);
        messageRealm.setSenderId(message.senderId);
        messageRealm.setSenderName(message.senderName);
        messageRealm.setSenderAvatar(message.senderAvatar);
        messageRealm.setStatus(Message.Status.getValue(message.status));
        messageRealm.setRead(message.isRead);
        messageRealm.userId = message.userId;
        messageRealm.uniqueId = getUniqueId(message.id, message.userId);
        messageRealm.fileDownloaded = message.fileDownloaded;
        return messageRealm;
    }

    public static String getUniqueId(String messageId, long userId) {
        return messageId + "_" + userId;
    }

    public long getUserId() {
        return userId;
    }

}