var Car = Namespace.declare("tla.EAM.layer3",
    class Car extends Interface {

        get infotainmentSystem() {
            if(this._private.infotainmentSystem == null) {
                try {
                   this._private.infotainmentSystem = CarInA.getInstance().init();
                   this.call(this.watsonIor, {say:"Nice to meet you, Carina",voice:"us"});
                   var speakerArray = this._private.infotainmentSystem.speakers.length;
                   this.call(this.watsonIor, {say:"Found "+speakerArray+" speakers controlled by Carina. trying to display them.",voice:"us"});

                   this.display = PageOverview.getInstance();
                   this.display.properties.url = "https://codeiot-carina.mybluemix.net/ui";
                   this.display.properties.css = "col-sm-12 fullHeight overflow-hidden";
                   this.display.defaultView.append()
                   this.call(this.watsonIor, {say:"Hi my friend BMW 325 i. I have setup a display for you. Have a look.",voice:"uk"});
                   this.display.properties.url = "https://codeiot-carina.mybluemix.net/ui";


                }
                catch (error) {
                    this.call(this.watsonIor, {say:"no Info Tainment system present",voice:"us"});
                }
            }
            return this._private.infotainmentSystem;
        }
    }
);

var Model325i = Namespace.declare("com.twitter.Bootstrap",
    class Model325i extends UcpComponent {
        static start() {
        }
        static dependencies() {
            return [
                "/EAMD.ucp/Components/com/ceruleanCircle/EAM/5_ux/WODA/PageOverview/1.0.0/PageOverview.component.xml"
            ];
        }

        setPosition(speaker) {
            switch(speaker.model.position) {
                case 'top_left_speaker' : {
                    speaker.model.properties.style.padding = '470px 0 0 520px';
                    break;
                }
                case 'top_right_speaker' : {
                    speaker.model.properties.style.padding = '0 0 0 520px';
                    break;
                }
                case 'rear_left_speaker' : {
                    speaker.model.properties.style.padding = '470px 0 0 930px;';
                    speaker.model.properties.style.speakerwidth = '2300px;'
                    break;
                }
                case 'rear_right_speaker' : {
                    speaker.model.properties.style.padding = '0 0 0 930px;';
                    speaker.model.properties.style.speakerwidth = '2300px;'
                    break;
                }
                default : 
                    break;
            }
        }

        constructor() {
            super();

            Thinglish.implement(this, Car, true);
            this.watsonIor = new IOR().init("http://codeiot-carina.mybluemix.net/call");
            ONCE.eventSupport.addEventListener("newThing", this, this.newThing.bind(this));
            this.model = {
                style: {
                    width : '90%'
                }
            }
        }

        init() {
            if (this.isInitialized)
                return;
            super.init();
            this.call(this.watsonIor, {say:"Hi I am a BMW 325 i",voice:"us"}).then(

            e => {
                this.initInfotainment();
            }).catch(
            e => {
                this.initInfotainment();
            }
            );

            return this;
        }

        newThing(event, thing) {
            try{
           if (Thinglish.isInstanceOf(thing, Infotainment) && !ONCE.foundInfotainmentSystem) {
               ONCE.foundInfotainmentSystem = true;
               this.call(this.watsonIor, {say:"Info Tainment system arrived",voice:"us"}).then(
               s => {
                        this.initInfotainment();
               }).catch(
               s => {
                        this.initInfotainment();
               });
           }}
           catch (error) {}   
        }

        initInfotainment() {
            if ( !ONCE.foundInfotainmentSystem) 
                this.call(this.watsonIor, {say:"Looking for an Info Tainment system",voice:"us"});
            var infotainmentSystem = this.infotainmentSystem;
            if (infotainmentSystem) {

            var speakerArray = infotainmentSystem.speakers;
            speakerArray.map(speaker => {
                this.setPosition(speaker);
                this.add(speaker);
            });
            }
        }

    }
);