package com.brilliance.mda.runtime.mda.impl;

import com.brilliance.mda.runtime.mda.*;
import com.brilliance.mda.runtime.mda.util.RuleUtils;

import java.util.ArrayList;
import java.util.List;

public class PanelImpl extends AbstractBaseObject implements IPanel {
    protected final transient List<IEventRule> eventRules = new ArrayList<IEventRule>();

    protected transient Object window;

    private String titleI18nKey;

    public PanelImpl(IParent parent, String name, String titleI18nKey) {
        super(parent);
        this.name = name;
        this.titleI18nKey = titleI18nKey;
    }

    public synchronized void dispose() {
        this.eventRules.clear();
        this.window = null;
        super.dispose();
    }

    @Override
    public String getPath() {
        return this.name;
    }

    @Override
    public void clear() {
    }

    @Override
    public String getLanguage() {
        return null;
    }

    public IModule getModule() {
        return (IModule) this.parent;
    }

    public String getTitleI18nKey() {
        return this.titleI18nKey;
    }

    public void setWindow(Object window) {
        this.window = window;
    }

    public Object getWindow() {
        return this.window;
    }

    public void addEventRule(IEventRule rule) {
        this.eventRules.add(rule);
    }


    protected void sort() {
        super.sort();
        RuleUtils.sort2(this.eventRules);
    }

    public boolean invokeEventRules(IContext ctx, Event event) {
        if (!this.sorted)
            sort();
        return RuleUtils.invoke(this.eventRules, ctx, event, new int[0]);
    }
}
