package de.beyondsoft.ownchat.data.repository.models;

import de.beyondsoft.ownchat.model.Group;
import de.beyondsoft.ownchat.model.Message;
import de.beyondsoft.ownchat.model.User;

import java.util.ArrayList;

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

/**
 * Created by imarneanu on 3/6/17.
 */
public class GroupRealm extends RealmObject {

    public static final String ID = "id";
    public static final String NAME = "name";
    public static final String CREATED_AT = "createdAt";
    public static final String USER_ID = "userId";

    @PrimaryKey
    private String uniqueId;
    private long id;
    private String name;
    private String avatar;
    private MessageRealm message;
    private String mobile;
    private boolean onlyEmployees;
    private RealmList<UserRealm> userIdList;
    private long createdAt;
    private long userId;

    public GroupRealm() {
    }

    public GroupRealm(long id, String name, String avatar, Message message, String mobile, boolean onlyEmployees, ArrayList<User> userList, long userId) {
        this.userIdList = new RealmList<>();
        this.id = id;
        this.name = name;
        this.avatar = avatar;
        this.message = MessageRealm.createFromMessage(message);
        this.createdAt = this.message != null ? this.message.getCreatedAt() : 0;
        this.onlyEmployees = onlyEmployees;
        this.mobile = mobile;

        if (userList != null) {
            for (User l : userList) {
                this.userIdList.add(UserRealm.createFromUser(l));
            }
        }

        this.userId = userId;
        this.uniqueId = id + "_" + userId;
    }

    public long getUserId() {
        return userId;
    }

    public static GroupRealm createFromGroup(Group group) {
        if (group == null) {
            return null;
        }

        return new GroupRealm(group.id, group.name, group.avatar, group.lastMessage, group.mobile, group.onlyEmployees, group.users, group.userId);
    }

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

    public String getUniqueId() {
        return uniqueId;
    }

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

    public long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getAvatar() {
        return avatar;
    }

    public boolean isOnlyEmployees() {
        return onlyEmployees;
    }

    public MessageRealm getMessage() {
        return message;
    }

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

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public void setOnlyEmployees(boolean onlyEmployees) {
        this.onlyEmployees = onlyEmployees;
    }

    public String getMobile() {
        return mobile;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public RealmList<UserRealm> getUserIdList() {
        return userIdList;
    }

    public void setUserIdList(RealmList<UserRealm> userIdList) {
        this.userIdList = userIdList;
    }

    @Override
    public String toString() {
        return "GroupRealm{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", uniqueId" + uniqueId +
                ", avatar='" + avatar + '\'' +
                ", message=" + message +
                ", mobile='" + mobile + '\'' +
                ", onlyEmployees=" + onlyEmployees +
                ", userIdList=" + userIdList +
                ", createdAt=" + createdAt +
                '}';
    }
}
