
//                                                        js    src  version component
var Flot = Namespace.declare("org.flotcharts",
class Flot extends UcpComponent {
    static get implements() {
        return [];
    }

    static get overwriteServerDescriptor() {
        return true; //for offline work
    }

    static get dependencies() {
        return [
            "/EAMD.ucp/Components/org/flotcharts/Flot/1.0.0/src/flot/jquery.flot.js",
        ];
    }

    static get weBeanUnitPaths() {
        return [
            "./src/html/weBeans/Flot.weBean.html",
            //"./src/html/weBeans/FlotItemView.weBean.html",
            //"./src/html/weBeans/FlotDetailsView.weBean.html",
        ];
    }

    static start() {}

    constructor() {
        super();
    }

    init() {
        super.init();

        const demoModel = {
            itemViewModel: {
                name: "Flot",
                description: "plotting lib",
                badge: "cool" //"has to be copied on UcpView.init"
            },
            cssClasses: "",
            width: "400px",
            height: "300px",

            data: [    [[0, 3], [4, 8], [8, 5], [9, 13]]    ]
        };
        this.model = demoModel;
        this.model.autoUpdate = true;

        Thinglish.defineAlias(this.model, "itemViewModel.name");
        Thinglish.defineAlias(this.model, "itemViewModel.description");
        Thinglish.defineAlias(this.model, "itemViewModel.badge");
        //inherit superModel ... until ManagedProperties will suport nested models
        //Object.keys(super.model).forEach(k => Thinglish.defineAlias(model, "defaultViewModel." + k));

        this.actionIndex = Thinglish.createActionIndexOf(Flot, this);

        return this;
    }




    onDomReady(view) {
        if (!(view instanceof DefaultView)) return;
        $.plot(view.documentElement, this.model.data);
     }


    /*
    loadWeBean() {
        if (Thinglish.lookupInObject(Relationship, "type.ucpComponentDescriptor.webBeanUnits"))
            return Relationship.type.ucpComponentDescriptor.webBeanUnits.find(u => u.model.name.startsWith(Relationship.name)).weBean;

        return null;
    }
    */

    updateView(view, updateObject) {
        console.info("ucpComponent updateView", this.id, ...arguments);

        console.info(`
       notifies you which view has to be rerendered.
       calling this.update() will force a complete rerendering.

       this implies loosing all DOM listeners. after rerendering onDomReady(view) will be called again to reinatialize DOM listeners

       calling tis.documentElement does the first rendering.
       the framework tries to postpone this step to the last possible point so that you can initialize the model first and 
       do not have to rerender many times. the last possible point to render, is if the element will be appended to to an element
       that is already in the DOM. The UcpView method checkTargetElement finds out if an element isInDom and ataches the boolean to the element.

       alternatively you can check the updateObject to see what in the model did change
       and update the dom manually here on view.documentElement or view.subViewElement

       view.subView gives you access to ucpViews declared as webeanProperties in the weBean
       `, this.id, ...arguments);

        if (!updateObject) {
            return;
        }
        Object.keys(updateObject).forEach(
            member => {
                switch (member) {
                    case "attribute1":
                        //update something for attribute1
                        break;
                    case "property1":
                        //update something for property1
                        break;
                    default:
                        //update something in any case
                }
            }
        );
    }

    handleSelection(event, item) {
        console.info(this.constructor.name, "selected", item.ucpComponent);
        Action.do(DetailsView.ACTION_SHOW, this);
        //Action.do(Workflow.ACTION_SELECT, item);
    }

    /*
        createOverView() {
            return new FlotItemView();
        }
        createDetailsView() {
            return new FlotDetailsView();
        }
    */
    startSelectWorkflow() {
        console.info(this.constructor.name, "startSelectWorkflow");
    }

    assign() {
        console.info(this.constructor.name, "assign");
    }

    setEmpty() {
        console.info(this.constructor.name, "setEmpty");
    }
}
);

/*
var FlotItemView = Namespace.declare("tla.EAM.layer5",
class FlotItemView extends UcpView {
    static get implements() { return [ItemView]; }

    init(ucpComponent) {
        super.init(ucpComponent);
        this.badge = "some Value";
        return this;
    }

    update(updateObject) {
        console.info("ucpView update", this.id, ...arguments);
        if (!updateObject) {
            return;
        }
        Object.keys(updateObject).forEach(
            member => {
                switch (member) {
                    case "badge":
                        //update something for badge
                        break;
                    case "attribute1":
                        //update something for attribute1
                        break;
                    default:
                        //update something in any case
                }
            }
        );
    }

}
);


var FlotDetailsView = Namespace.declare("tla.EAM.layer5",
class FlotDetailsView extends UcpView {
    static get implements() { return [DetailsView]; }

    init(ucpComponent) {
        super.init(ucpComponent);

        return this;
    }

    update(updateObject) {
        console.info("ucpView update", this.id, ...arguments);
    }

    onInitDetails() {
        console.info("ucpView onInitDetails", this.id, ...arguments);
        this.details.model.heading += " also from DetailsView";

    }

}
);

*/