app.LoadScript( "page.js" );    // base class for all pages classes
    app.LoadScript( "page_alarm.js" );
    app.LoadScript( "page_contact.js" );
    app.LoadScript( "page_zone.js" );
    app.LoadScript( "page_admin.js" );
    app.LoadScript( "page_settings.js" );


var g_global = {
    "Runtime":{
        "PagesList":[],
        "CurPage":"Current page name"
    },
    "Constants": {
        "SettingsFilePath": "conf",
        "SettingsFileName": "settings.json"
    },
    "Settings":{}
}

function OnStart(){    
    ReadParseSettingsFile()
    app.SetAppLanguage((g_global.Settings.Language) ? g_global.Settings.Language : app.GetLanguage())
    
    drawGUI()
    ChangePageByName("page_alarm")
}


function drawGUI() {
    lay = app.CreateLayout( "Absolute", "Vertical,FillXY" );
    lay.SetBackground("./Img/BlackBack.jpg");
    // create header text box

    
    layHeader = app.CreateLayout( "Absolute", "Vertical");
    
    headTitle = app.CreateText(" ",1,0.1,"Center");
    headTitle.SetPadding(0, 0, 0, 0);
    headTitle.SetTextSize(25);
    headTitle.SetBackground("./Img/BlackBack.jpg");
    layHeader.AddChild(headTitle);
    
    // btn_back = app.CreateText("[fa-arrow-left]", -1, -1, "FontAwesome");
    // btn_back.SetOnTouchUp( function(){alert("Not implemented yet")} );
    // btn_back.SetTextSize(35)
    // btn_back.SetPosition(0.02, 0.02, 0.18, 0.10);
    // layHeader.AddChild(btn_back);
    
    lay.AddChild(layHeader);

    scroll = app.CreateScroller( 1, 0.77, "FillXY" )
    scroll.SetPosition(0, 0.12);
    lay.AddChild( scroll );

    layContent = app.CreateLayout( "Linear", "VCenter,FillXY" );
    layContent.SetSize( 1, 1 );
    scroll.AddChild( layContent );
    
    g_page_alarm = new page_alarm();
    g_page_contact = new page_contact();
    g_page_zone = new page_zone();
    g_page_admin = new page_admin();
    
    g_page_settings = new page_settings();
	
	btn_options = app.CreateText("[fa-cogs]", -1, -1, "FontAwesome");
    btn_options.SetOnTouchUp( function(){ ChangePageByName("page_settings") });
    btn_options.SetTextSize(30)
    btn_options.SetPosition(0.82, 0.02, 0.18, 0.10);
    layHeader.AddChild(btn_options);
   
    // footer
    layFooter = app.CreateLayout("Linear","Horizontal,FillX");
    layFooter.SetPosition(0,0.9,1,0.11);
    layFooter.SetBackground("./Img/BlackBack.jpg");
    
    // alarm
	txt = app.CreateText( "[fa-shield]", 0.25, -1, "FontAwesome" );
	txt.SetOnTouchUp( function(){ ChangePageByName("page_alarm") });
	txt.SetTextSize( 35 );
	layFooter.AddChild( txt );
	
	// contacts
	txt = app.CreateText( "[fa-address-card-o]", 0.25, -1, "FontAwesome" );
	txt.SetOnTouchUp( function(){ ChangePageByName("page_contact") });
	txt.SetTextSize( 35 );
	layFooter.AddChild( txt );
	
	// wireles zones
	txt = app.CreateText( "[fa-gg]", 0.25, -1, "FontAwesome" );
	txt.SetOnTouchUp( function(){ ChangePageByName("page_zone") });
	txt.SetTextSize( 35 );
	layFooter.AddChild( txt );
	
	// Administration
	txt = app.CreateText( "[fa-wrench]", 0.25, -1, "FontAwesome" );
	txt.SetOnTouchUp( function(){ ChangePageByName("page_admin") });
	txt.SetTextSize( 35 );
	layFooter.AddChild( txt );
    
    lay.AddChild(layFooter); 
    app.AddLayout( lay );
}

//Called when configuration changes (eg. When user rotates phone)
function OnConfig() {
// 	orient = app.GetOrientation();
// 	app.ShowPopup( "OnConfig - " + orient );
	app.DestroyLayout(lay);
	g_global.Runtime.PagesList = [];        //clear list of pages, as we reload them
	drawGUI();
	// Find out witch pagee was opened and force open it again
	ChangePageByName( g_global.Runtime.CurPage, true)
}

//Swap the page content.
function ChangePage( page, force=false ) {
    if ((typeof page == "" || g_global.Runtime.CurPage == page.GetName()) && !force)
        return;
    //Fade out current content.
    g_global.Runtime.PagesList.forEach(function (item, index) {
        if( item.IsVisible())
            item.Hide()
    });
    
    //Fade in new content and set current page and remember it
    page.Show();
    g_global.Runtime.CurPage = page.GetName();
}

function ChangePageByName( pageName, force=false ){
	g_global.Runtime.PagesList.forEach(function (item, index) {
        if (pageName == item.GetName()){
            ChangePage(item, force)
        }
    });
}

function PrepareSendMessage(msg){
    app.SendSMS( "{0}{1}".format(g_global.Settings.PinCode, msg), g_global.Settings.Phone );
}

function ReadParseSettingsFile() {
    // var filepath = app.GetPrivateFolder(g_global.Constants.SettingsFilePath) + "/" + g_global.Constants.SettingsFileName;
    // if (app.FileExists( filepath )){
    //     var fileContents = app.ReadFile( filepath );
    //     var settings = JSON.parse(fileContents);
    //     g_global.Settings = JSON.parse(fileContents);
    // }
    // else
    //     // file does not exist, redirect user to settings page
    //     ChangePageByName("page_settings")
    
    
    var fileContents = app.LoadText("settings", "{}", "settings.json" );
    // console.log(var_dump(fileContents));
    var settings = JSON.parse(fileContents);
    g_global.Settings = JSON.parse(fileContents);
}

function WriteSettingsFile(){
    // var filepath = app.GetPrivateFolder(g_global.Constants.SettingsFilePath) + "/" + g_global.Constants.SettingsFileName;
    // app.WriteFile(filepath, JSON.stringify(g_global.Settings));
    
    app.SaveText("settings", JSON.stringify(g_global.Settings), "settings.json");
    app.ShowPopup(T("common_data-saved"));
}

