package org.mercury_im.messenger.data.mapping;

import org.mercury_im.messenger.data.model.MessagePayloadModel;
import org.mercury_im.messenger.entity.message.content.Payload;
import org.mercury_im.messenger.entity.message.content.TextPayload;

import static org.mercury_im.messenger.data.enums.MessageContentType.body;

public class MessagePayloadMapping extends AbstractMapping<Payload, MessagePayloadModel> {

    @Override
    public Payload newEntity(MessagePayloadModel model) {
        switch (model.getType()) {
            case body:
                return new TextPayload();
            default:
                return null;
        }
    }

    @Override
    public MessagePayloadModel newModel(Payload entity) {
        return new MessagePayloadModel();
    }

    @Override
    public MessagePayloadModel mapToModel(Payload entity, MessagePayloadModel model) {
        if (entity instanceof TextPayload) {
            model.setType(body);
            model.setBody(((TextPayload) entity).getBody());
        }
        // else if (...)
        return model;
    }

    @Override
    public Payload mapToEntity(MessagePayloadModel model, Payload entity) {
        entity.setId(model.getId());
        switch (model.getType()) {
            case body:
                TextPayload body = (TextPayload) entity;
                body.setBody(model.getBody());
                break;
        }
        return entity;
    }
}
