class page_settings extends PageControls {
constructor() {
    super();
	//Properties
};

//---- METHODS ----
prepareUI() {
	this.Page = app.CreateLayout( "Linear", "FillXY,VCenter" );
	//prepare layout hidden but use animation
    this.Page.SetVisibility( "Hide" );
    layContent.AddChild( this.Page );

//      |-----------layHoriz------------|
//      ||-layVertL---| |---layVertR---||
//      || ttl       || |              ||
//      |-------------| |      opt     ||
//      || dsc       || |              ||
//      ||------------| |--------------||
//      |-------------------------------|
    this.makeOption = function(ttl, dsc, opt, first=false){
        this.layHoriz = app.CreateLayout( "Linear", "Horizontal,FillXY" );
        this.layHoriz.SetSize(1, -1);
        // make space on top if element is not first
        this.layHoriz.SetPadding(0, (first)?0:0.05, 0, 0);
        
        this.layVertL = app.CreateLayout( "Linear", "Left,Vertical" );
        this.layVertL.SetSize(0.68, -1);
        
        this.layVertR = app.CreateLayout( "Linear", "Right,Vertical" );
        this.layVertR.SetSize(0.28, -1);

        this.ttl = app.CreateText( ttl, -1, -1, "Multiline,Bold,Left" );
        this.ttl.SetTextSize( this.OptionFontSize )
        if(dsc) 
            this.dsc = app.CreateText( dsc, -1, -1, "Multiline,Left");

        
        this.Page.AddChild( this.layHoriz )
        this.layHoriz.AddChild( this.layVertL )
        this.layVertL.AddChild(this.ttl)
        if(dsc)
            this.layVertL.AddChild(this.dsc)
        this.layHoriz.AddChild( this.layVertR )
        this.layVertR.AddChild( opt )
    };

    // alarm phone number
    this.phoneInput = app.CreateTextEdit( "", false, false, "AutoSelect,SingleLine,Right,Phone" );
    if(g_global.Settings.Phone)
        this.phoneInput.SetText(g_global.Settings.Phone);
    this.phoneInput.SetHint("+3706#######");
    this.phoneInput.SetOnFocus( (state)=>{
        if (state) {
            app.ChooseContact( "phone", (name, number)=>{
                app.ShowPopup( name + " " + number );
                this.phoneInput.SetText( number.replaceAll(" ", "") );
            })
        }
        else {
            g_global.Settings.Phone = this.phoneInput.GetText();
            WriteSettingsFile();
        }
        app.HideKeyboard();
        })
    this.makeOption(T("page-settings_phone-number"), T("page-settings_phone-number-desc"), this.phoneInput, true);


    // pin code
    this.codeInput = app.CreateTextEdit( "", false, false, "AutoSelect,SingleLine,Right,Numbers" );
    if(g_global.Settings.PinCode)
        this.codeInput.SetText(g_global.Settings.PinCode);
    this.codeInput.SetHint("####")
    this.codeInput.SetOnChange( ()=>{
            // four digits
            if(! /(^[0-9]{4}$)/.test(this.codeInput.GetText()) && this.codeInput.GetText().length >= 4){
                this.codeInput.SetText("");
                app.ShowPopup( T("common_validation-error") );
                this.AnimateError(this.codeInput);
            } 
        });
    this.codeInput.SetOnFocus( (state)=>{
        if(state){
            //pass
        }
        else { // do on lost focus
            g_global.Settings.PinCode = this.codeInput.GetText();
            WriteSettingsFile();
        }
    });
    this.codeInput.SetOnEnter( ()=>{
        g_global.Settings.PinCode = this.codeInput.GetText();
        WriteSettingsFile();
        this.codeInput.ClearFocus();
        app.HideKeyboard();
    });
    this.makeOption(T("page-settings_pin-code"), T("page-settings_pin-code-desc"), this.codeInput);


    // language
    this.langSelector = app.CreateSpinner();
    this.langSelector.SetOnChange( (value)=>{
        app.SetAppLanguage(value);
        g_global.Settings.Language = value;
        WriteSettingsFile();
        var ynd = app.CreateYesNoDialog("Application needs to be restarted to take effect");
        ynd.SetOnTouch((ans)=>{
            if(ans=="Yes"){
                OnConfig()
                // var now = new Date().getTime();
                // app.SetAlarm( "Set", 7985, null, now + 1000 );
                // app.Exit( );
            }
        })
        ynd.Show();
    });
    this.langSelector.SetList(app.GetAppLanguages())
    this.langSelector.SelectItem( (g_global.Settings.Language) ? g_global.Settings.Language : app.GetLanguage() );
    this.makeOption(T("page-settings_app-lang"), T("page-settings_app-lang-desc"), this.langSelector);

};

}   // end of class

