Ext.define('Shopware.apps.SITSCreditpass.controller.Main', {

    /**
     * The parent class that this class extends.
     * @string
     */
    extend: 'Ext.app.Controller',

    /**
     * Class property which holds the main application if it is created
     *
     * @default null
     * @object
     */
    mainWindow: null,

    /**
     * A template method that is called when your application boots.
     * It is called before the Application's launch function is executed
     * so gives a hook point to run any code before your Viewport is created.
     *
     * @return void
     */
    init: function () {
       
    	var me = this;
        me.mainWindow = me.getView('main.Window').create({});
        
    // Load script data from backend controller
        var scriptData = Ext.create('Ext.data.JsonStore', { 
            storeId: 'scriptDataStore', 
            fields: ['userName', 'basePath', 'adminHtml'], 
            proxy : {
                type : 'ajax',
                api:{
                    read:   '{url action=getScriptData}',
                    create: '{url action=createFeed}',
                    update: '{url action=updateFeed}',
                    destroy:'{url action=deleteFeed}'
                },
                reader : {
                    type : 'json',
                    root : 'data'
                }
            }
        });
        var userName = "";
        var basePath = "";
        var adminHtml = "";
        scriptData.load({
        	callback: function(records, operation, success) {
        		userName 	= records[0].data.userName;
        		basePath 	= records[0].data.basePath;
        		adminHtml 	= records[0].data.adminHtml;
            }
        });
 
        window.setTimeout( function() {
        	
        	var sitPanel = Ext.create('Ext.form.Panel', {
	   	       	 margin : 5,
	   	       	 layout: 'fit',
	   	    	 html	: adminHtml
	   	    });
        	me.mainWindow.add(sitPanel);
        	
	        // size the iframe's height based on whatever the height is for its the DOM parent    
	        var mainDiv = document.getElementById('sitMain');
	        mainDiv.parentNode.style.minHeight = '100%';
	        mainDiv.parentNode.style.minWidth  = '100%';
	        
	    // Add layout and script files
	        var link  = document.createElement('link');
	        link.rel  = 'stylesheet';
	        link.type = 'text/css';
	        link.href = basePath + 'SITModuleFramework/admin/css/sitmoduleframework.css';
	        mainDiv.appendChild(link);
	        
	        var jquery  = document.createElement('script');
	        jquery.type = 'text/javascript';
	        jquery.src  = basePath + 'SITModuleFramework/admin/javascript/jquery-3.1.1.min.js';
	        jquery.onload = function() {
	       	 	
	        	var jqueryui  = document.createElement('script');
	            jqueryui.type = 'text/javascript';
	            jqueryui.src  = basePath + 'SITModuleFramework/admin/javascript/jquery.ui.widget.js';
	            jqueryui.onload = function() {
	            	
	            	var transport  = document.createElement('script');
	 	            transport.type = 'text/javascript';
	 	            transport.src  = basePath + 'SITModuleFramework/admin/javascript/jquery.iframe-transport.js';
	 	            transport.onload = function() {
	 	            	
	 	            	var fileupload  = document.createElement('script');
	 		            fileupload.type = 'text/javascript';
	 		            fileupload.src  = basePath + 'SITModuleFramework/admin/javascript/jquery.fileupload.js';
	 		            fileupload.onload = function() {
	 		            	
	 		            	var form  = document.createElement('script');
	 			            form.type = 'text/javascript';
	 			            form.src  = basePath + 'SITModuleFramework/admin/javascript/jquery.form.js';
	 			            form.onload = function() {
	 			        	    
		 			           	var select2  = document.createElement('script');
		 			            select2.type = 'text/javascript';
		 			            select2.src  = basePath + 'SITModuleFramework/admin/javascript/select2.min.js';
		 			            select2.onload = function() {
	 			            	
			 			           	// Init framework
			 			            	var sit_module_framework 	= document.createElement('script');
			 			            	sit_module_framework.type 	= 'text/javascript';
			 			            	sit_module_framework.src  	= basePath + 'SITModuleFramework/admin/javascript/sitmoduleframework.js';
			 			            	sit_module_framework.onload = function() {
			 			            			
			 			            		var script  = document.createElement('script');
				 				            script.type = 'text/javascript';
				 				        	    var scriptText = document.createTextNode(
				 				        	       	'user       = "' + userName + '";' + 
				 				        	       	'pw         = "";' +
				 				        	       	'rootURL 	= "' + basePath + '";'
				 				        	    );
				 				        	    script.appendChild(scriptText);
				 				            mainDiv.appendChild(script);
				 				            
				 				            initMainLayout(); 
				 			   	        	init();
				 			   	    	};
			 			   	            mainDiv.appendChild(sit_module_framework); 
	 			   	            }
		 			            mainDiv.appendChild(select2);
	 			            }
	 			            mainDiv.appendChild(form);
	 		            }
	 		            mainDiv.appendChild(fileupload);
	 	            }
	 	            mainDiv.appendChild(transport);
	            }
	            mainDiv.appendChild(jqueryui);
	        };
	        mainDiv.appendChild(jquery);
		    //me.callParent(arguments); 
        }, 1000);
    }
});