package de.beyondsoft.ownchat.model;

import com.google.gson.annotations.SerializedName;

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

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

import java.util.ArrayList;

import io.realm.RealmList;

/**
 * Created by imarneanu on 2/21/17.
 */
public class Group implements Parcelable {

    @SerializedName("oid")
    public long id;
    @SerializedName("name")
    public String name;
    @SerializedName("avatar")
    public String avatar;
    @SerializedName("mobile")
    public String mobile;
    @SerializedName("lastmessage")
    public Message lastMessage;
    @SerializedName("users")
    public ArrayList<User> users;
    @SerializedName("onlyemployees")
    public boolean onlyEmployees;

    private boolean isRead;
    public long userId;

    public boolean isHelpGroup() {
        return !onlyEmployees;
    }

    public Group() {
    }

    public Group(long id, String name, String avatar, String mobile, MessageRealm lastMessage, RealmList<UserRealm> users, boolean onlyEmployees) {
        this.users = new ArrayList<>();
        this.id = id;
        this.name = name;
        this.avatar = avatar;
        this.mobile = mobile;
        this.lastMessage = Message.createFromMessageRealm(lastMessage);
        this.isRead = this.lastMessage != null && this.lastMessage.isRead;
        for (UserRealm user : users) {
            this.users.add(new User(user.getId(), user.getFirstName(), user.getLastName(), user.getAvatar(), user.getMobile(), user.isEmployee()));
        }
        this.onlyEmployees = onlyEmployees;
    }

    public String getContactsNames() {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < users.size(); i++) {
            stringBuilder.append(users.get(i).getName()).append(", ");
        }
        int length = stringBuilder.length();
        if (length > 1) {
            stringBuilder.delete(length - 2, length - 1);
        }
        return stringBuilder.toString();
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(this.id);
        dest.writeString(this.name);
        dest.writeByte(this.onlyEmployees ? (byte) 1 : (byte) 0);
        dest.writeList(this.users);
        dest.writeString(this.avatar);
        dest.writeString(this.mobile);
    }

    public boolean isRead() {
        return lastMessage != null && lastMessage.isRead;
    }

    public static Group createFromGroupRealm(GroupRealm groupRealm) {
        if (groupRealm == null) {
            return null;
        }
        return new Group(groupRealm.getId(),
                groupRealm.getName(),
                groupRealm.getAvatar(),
                groupRealm.getMobile(),
                groupRealm.getMessage(),
                groupRealm.getUserIdList(),
                groupRealm.isOnlyEmployees());
    }

    protected Group(Parcel in) {
        this.id = in.readLong();
        this.name = in.readString();
        this.onlyEmployees = in.readByte() != 0;
        this.users = new ArrayList<>();
        in.readList(this.users, User.class.getClassLoader());
        this.avatar = in.readString();
        this.mobile = in.readString();
    }

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

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

    @Override
    public String toString() {
        return "Group{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", avatar='" + avatar + '\'' +
                ", mobile='" + mobile + '\'' +
                ", lastMessage=" + lastMessage +
                ", users=" + users +
                ", onlyEmployees=" + onlyEmployees +
                ", isRead=" + isRead +
                '}';
    }

    public boolean isEmployee(ArrayList<User> users) {
        for (int i = 0; i < users.size(); i++) {
            if (name.equals(users.get(i).getName())) {
                return users.get(i).isEmployee;
            }
        }

        return false;
    }
}
