package de.beyondsoft.ownchat.model;

import com.google.gson.annotations.SerializedName;

import de.beyondsoft.ownchat.R;
import de.beyondsoft.ownchat.data.repository.models.MessageRealm;

import java.util.ArrayList;

/**
 * Created by imarneanu on 2/21/17.
 */
public class Message {

    private static final int STATUS_DELIVERED = 0;
    private static final int STATUS_FAILED = 1;
    private static final int STATUS_PENDING = 2;

    @SerializedName("id")
    public String id;
    @SerializedName("userid")
    public long senderId;
    @SerializedName("user_name")
    public String senderName;
    @SerializedName("user_picture")
    public String senderAvatar;
    @SerializedName("groupid")
    public long groupId;
    @SerializedName("text")
    public String message;
    @SerializedName("original_filename")
    public String originalFilename;
    @SerializedName("file")
    public String filePath;
    @SerializedName("smaller_image")
    public String smallerImage;
    @SerializedName("created_at")
    public long createdAt;

    @SerializedName("group_nb_users")
    public int numberOfGroupUsers;

    @SerializedName("groupname")
    public String groupName;

    public Status status;
    public String uri;
    public boolean isRead;
    public long userId;
    public boolean fileDownloaded;

    public Message() {
    }

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

    public String getUniqueId() {
        return MessageRealm.getUniqueId(id, userId);
    }

    public enum Status {
        DELIVERED(R.drawable.message_delivered),
        FAILED(R.drawable.message_failed),
        PENDING(-1);

        private int resourceId;

        Status(int value) {
            resourceId = value;
        }

        public static Status getStatus(int value) {
            switch (value) {
                case STATUS_DELIVERED:
                    return DELIVERED;
                case STATUS_FAILED:
                    return FAILED;
                case STATUS_PENDING:
                    return PENDING;
                default:
                    return FAILED;
            }
        }

        public static int getValue(Status status) {
            if (status == null) {
                return STATUS_DELIVERED;
            }

            switch (status) {
                case DELIVERED:
                    return STATUS_DELIVERED;
                case FAILED:
                    return STATUS_FAILED;
                case PENDING:
                    return STATUS_PENDING;
            }

            return 1;
        }
    }

    public int getStatusResourceId() {
        return status != null ? status.resourceId : 0;
    }

    public static Message createFromMessageRealm(MessageRealm messageRealm) {
        if (messageRealm == null) {
            return null;
        }
        return new Message(messageRealm.getId(),
                messageRealm.getGroupId(),
                messageRealm.getCreatedAt(),
                messageRealm.getMessage(),
                messageRealm.getFilePath(),
                messageRealm.getSmallerImage(),
                messageRealm.getFilename(),
                messageRealm.getUri(),
                messageRealm.getSenderId(),
                messageRealm.getSenderName(),
                messageRealm.getSenderAvatar(),
                Status.getStatus(messageRealm.getStatus()),
                messageRealm.isRead(),
                messageRealm.getUserId(),
                messageRealm.isFileDownloaded());
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Message message1 = (Message) o;

        if (senderId != message1.senderId) {
            return false;
        }

        if (groupId != message1.groupId) {
            return false;
        }

        if (createdAt != message1.createdAt && createdAt != message1.createdAt * 1000 && createdAt * 1000 != message1.createdAt) {
            return false;
        }

        if (!id.equals(message1.id)) {
            return false;
        }

        if (uri != null ? !uri.equals(message1.uri) : message1.uri != null) {
            return false;
        }

        if (senderName != null ? !senderName.equals(message1.senderName) : message1.senderName != null) {
            return false;
        }

        if (senderAvatar != null ? !senderAvatar.equals(message1.senderAvatar) : message1.senderAvatar != null) {
            return false;
        }

        if (message != null ? !message.equals(message1.message) : message1.message != null) {
            return false;
        }

        if (filePath != null ? !filePath.equals(message1.filePath) : message1.filePath != null) {
            return false;
        }

        if (smallerImage != null ? !smallerImage.equals(message1.smallerImage) : message1.smallerImage != null) {
            return false;
        }

        if (isRead != ((Message) o).isRead) {
            return false;
        }

        return status == message1.status;
    }

    @Override
    public int hashCode() {
        int result = id.hashCode();

        result = 31 * result + (int) (senderId ^ (senderId >>> 32));
        result = 31 * result + (senderName != null ? senderName.hashCode() : 0);
        result = 31 * result + (senderAvatar != null ? senderAvatar.hashCode() : 0);
        result = 31 * result + (uri != null ? uri.hashCode() : 0);
        result = 31 * result + (int) (groupId ^ (groupId >>> 32));
        result = 31 * result + (message != null ? message.hashCode() : 0);
        result = 31 * result + (filePath != null ? filePath.hashCode() : 0);
        result = 31 * result + (smallerImage != null ? smallerImage.hashCode() : 0);
        result = 31 * result + (int) (createdAt ^ (createdAt >>> 32));
        result = 31 * result + (status != null ? status.hashCode() : 0);

        return result;
    }

    @Override
    public String toString() {
        return "Message{" +
                "id='" + id + '\'' +
                ", senderId=" + senderId +
                ", senderName='" + senderName + '\'' +
                ", senderAvatar='" + senderAvatar + '\'' +
                ", groupId=" + groupId +
                ", message='" + message + '\'' +
                ", filePath='" + filePath + '\'' +
                ", smallerImage='" + smallerImage + '\'' +
                ", uri=" + uri +
                ", createdAt=" + createdAt +
                ", status=" + status +
                ", isRead=" + isRead +
                '}';
    }

    public boolean removeFromList(ArrayList<Message> messages) {
        for (int index = 0, size = messages.size(); index < size; index++) {
            if (id.equals(messages.get(index).id)) {
                messages.remove(index);
                return true;
            }
        }

        return false;
    }
}
