/*
 * Copyright 2017 Young Digital Planet S.A.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package eu.ydp.empiria.player.client.controller.events.delivery;

import com.google.gwt.core.client.JavaScriptObject;
import eu.ydp.empiria.player.client.controller.flow.processing.events.ActivityProcessingEvent;
import eu.ydp.empiria.player.client.controller.flow.processing.events.FlowProcessingEvent;
import eu.ydp.empiria.player.client.module.core.base.IInteractionModule;
import eu.ydp.empiria.player.client.module.containers.group.GroupIdentifier;
import eu.ydp.empiria.player.client.util.js.JSArrayUtils;

import java.util.HashMap;
import java.util.Map;

public class DeliveryEvent {

    protected DeliveryEventType type;
    protected Map<String, Object> params;

    public DeliveryEvent(DeliveryEventType type) {
        this.type = type;
        params = new HashMap<String, Object>();
    }

    public DeliveryEvent(DeliveryEventType type, Map<String, Object> params) {
        this.type = type;
        this.params = params;
    }

    public DeliveryEventType getType() {
        return type;
    }

    public Map<String, Object> getParams() {
        return params;
    }

    public JavaScriptObject toJsObject() {
        JavaScriptObject paramsArr = JavaScriptObject.createArray();
        for (String key : params.keySet()) {
            Object currParam = params.get(key);
            if (currParam instanceof String) {
                JSArrayUtils.fillArray(paramsArr, key, (String) currParam);
            } else if (currParam instanceof Boolean) {
                JSArrayUtils.fillArray(paramsArr, key, ((Boolean) currParam).booleanValue());
            } else if (currParam instanceof IInteractionModule) {
                JSArrayUtils.fillArray(paramsArr, key, ((IInteractionModule) currParam).getJsSocket());
            }
        }
        return createJsObject(type.toString(), paramsArr);
    }

    private native JavaScriptObject createJsObject(String type, JavaScriptObject params)/*-{
        var obj = [];
        obj.type = type;
        obj.params = params;
        return obj;
    }-*/;

    public static DeliveryEvent fromFlowProcessingEvent(FlowProcessingEvent event) {
        DeliveryEvent de = null;

        DeliveryEventType type = null;

        for (DeliveryEventType currType : DeliveryEventType.values()) {
            if (currType.toString().equals(event.getType().toString())) {
                type = currType;
                break;
            }
        }

        if (type == null) {
            return null;
        }

        if (event instanceof ActivityProcessingEvent) {
            GroupIdentifier gi = ((ActivityProcessingEvent) event).getGroupIdentifier();
            if (gi != null) {
                Map<String, Object> params = new HashMap<String, Object>();
                params.put("groupIdentifier", gi);
                de = new DeliveryEvent(type, params);
            }
        }
        if (de == null) {
            de = new DeliveryEvent(type);
        }

        return de;
    }
}
