class page_zone extends PageControls {
constructor() {
    super()
	//Properties
	this.ZoneTypes = {
	    0: {name: T("page-zone_type-entrance"), description: T("page-zone_type-entrance-desc")},
	    1: {name: T("page-zone_type-outer"), description: T("page-zone_type-outer-desc")},
	    2: {name: T("page-zone_type-inner"), description: T("page-zone_type-inner-desc")},
	    3: {name: T("page-zone_type-onsite"), description: T("page-zone_type-onsite-desc")},
	    4: {name: T("page-zone_type-24hr-sound"), description: T("page-zone_type-24hr-sound-desc")},
	    5: {name: T("page-zone_type-24hr-silent"), description: T("page-zone_type-24hr-silent-desc")},
	    6: {name: T("page-zone_type-armed"), description: T("page-zone_type-armed-desc")},
	    7: {name: T("page-zone_type-stay"), description: T("page-zone_type-stay-desc")},
	    8: {name: T("page-zone_type-no-use"), description: T("page-zone_type-no-use-desc")},
	    9: {name: T("page-zone_type-door-bell"), description: T("page-zone_type-door-bell-desc")}
    }
};

//---- METHODS ----
prepareUI() {
	this.Page = app.CreateLayout( "Linear", "FillXY,VCenter" );
	//prepare layout hidden but use animation
    this.Page.SetVisibility( "Hide" );
    layContent.AddChild( this.Page );
    
	this.txt = app.CreateText( "[fa-question-circle-o] {0}".format(T("page-zone_query-name")), -1, -1, "Multiline,FontAwesome" )
	this.txt.SetTextSize( this.OptionFontSize )
	this.txt.SetPadding(0, 0, 0, 0);
	this.txt.SetOnTouchUp( ()=>{
	    this.dlg = app.CreateDialog( T("page-zone_query-name"), "Modal" );
        this.dlg.SetOnBack( ()=>{
            this.dlg.Dismiss();
        });
        
        this.layDlg = app.CreateLayout( "linear", "Vertical,FillXY" );
        this.dlg.AddLayout( this.layDlg );
        
        //possition
        this.txt = app.CreateText( "{0} (01-99)".format(T("common_position-in-list")), -1, -1, "NoWrap" )
        this.layDlg.AddChild( this.txt );
        
        this.posSelector = app.CreateTextEdit( "", false, false, "AutoSelect,SigleLine,Right,Numbers" );
        this.posSelector.SetHint("01-99");
        this.posSelector.SetOnChange( ()=>{
            // two digits
            if(! /(^[0-9]{2}$)/.test(this.posSelector.GetText()) && this.posSelector.GetText().length >= 2) {
                this.posSelector.SetText("")
                app.ShowPopup( T("common_validation-error") );
                this.AnimateError(this.posSelector);
            } 
        });
        this.layDlg.AddChild( this.posSelector );
        
        //Submit
        this.btn = app.CreateButton( "[fa-check] {0}".format(T("common_submit")), -1,-1, "FontAwesome,SingleLine" ) ;
        this.btn.SetPadding(0.02, 0.02, 0.02, 0.02);
        this.btn.SetOnTouch( ()=>{
            if(this.posSelector.GetText().length == 2){
                this.dlg.Dismiss();
                PrepareSendMessage("B{0}#".format(this.posSelector.GetText()));
            }
            else{
            this.AnimateError(this.posSelector);
            }
        });
        this.layDlg.AddChild( this.btn );
        
        this.dlg.Show();
	});
	this.Page.AddChild( this.txt )
	
	this.txt = app.CreateText( "[fa-pencil-square-o] {0}".format(T("page-zone_change-name")), -1, -1, "Multiline,FontAwesome" )
	this.txt.SetTextSize( this.OptionFontSize )
	this.txt.SetPadding(0, this.paddingTop, 0, 0);
	this.txt.SetOnTouchUp( ()=>{
	    this.dlg = app.CreateDialog( T("page-zone_change-name"), "Modal" );
        this.dlg.SetOnBack( ()=>{
            this.dlg.Dismiss();
        });
        
        this.layDlg = app.CreateLayout( "linear", "Vertical,FillXY" );
        this.dlg.AddLayout( this.layDlg );
        
        //possition
        this.txt = app.CreateText( "{0} (01-99)".format(T("common_position-in-list")), -1, -1, "NoWrap" )
        this.layDlg.AddChild( this.txt );
        
        this.posSelector = app.CreateTextEdit( "", false, false, "AutoSelect,SigleLine,Right,Numbers" );
        this.posSelector.SetHint("01-99")
        this.posSelector.SetOnChange( ()=>{
            // two digits
            if(! /(^[0-9]{2}$)/.test(this.posSelector.GetText()) && this.posSelector.GetText().length >= 2) {
                this.posSelector.SetText("");
                app.ShowPopup( T("common_validation-error") );
                this.AnimateError(this.posSelector);
            } 
        });
        this.layDlg.AddChild( this.posSelector );
        
        //zone name
        this.zoneName = app.CreateTextEdit( "", false, false, "AutoSelect,SigleLine,Right" );
        this.zoneName.SetHint(T("page-zone_hint-name-40-chars"))
        this.zoneName.SetOnFocus( (state)=>{
            if (state) {
                // none
            }
            app.HideKeyboard()
        });
        this.layDlg.AddChild( this.zoneName );
        
        //Submit
        this.btn = app.CreateButton( "[fa-check] {0}".format(T("common_submit")), -1,-1, "FontAwesome,SingleLine" ) ;
        this.btn.SetPadding(0.02, 0.02, 0.02, 0.02);
        this.btn.SetOnTouch( ()=>{
            if(this.posSelector.GetText().length == 2 && this.zoneName.GetText().length <=40) {
                this.dlg.Dismiss();
                PrepareSendMessage("B{0}#{1}#".format(
                    this.posSelector.GetText(),
                    this.zoneName.GetText()
                ));
            }
            else {
                app.ShowPopup( T("common_validation-error") );
            }
            
        });
        this.layDlg.AddChild( this.btn );
        
        this.dlg.Show();
	});
	this.Page.AddChild( this.txt )
	
	this.txt = app.CreateText( "[fa-magic] {0}".format(T("page-zone_change-function")), -1, -1, "Multiline,FontAwesome" )
	this.txt.SetTextSize( this.OptionFontSize )
	this.txt.SetPadding(0, this.paddingTop, 0, 0);
	this.txt.SetOnTouchUp( ()=>{
	    this.dlg = app.CreateDialog( T("page-zone_change-function"), "Modal" );
        this.dlg.SetOnBack( ()=>{
            this.dlg.Dismiss();
        });
        
        this.layDlg = app.CreateLayout( "linear", "Vertical,FillXY" );
        this.dlg.AddLayout( this.layDlg );
        
        //possition
        this.txt = app.CreateText( "{0} (01-99)".format(T("common_position-in-list")), -1, -1, "NoWrap" )
        this.layDlg.AddChild( this.txt );
        
        this.posSelector = app.CreateTextEdit( "", false, false, "AutoSelect,SigleLine,Right,Numbers" );
        this.posSelector.SetHint("01-99")
        this.posSelector.SetOnChange( ()=>{
            // two digits
            if(! /(^[0-9]{2}$)/.test(this.posSelector.GetText()) && this.posSelector.GetText().length >= 2) {
                this.posSelector.SetText("")
                app.ShowPopup( T("common_validation-error") );
                this.AnimateError(this.posSelector);
            } 
        });
        this.layDlg.AddChild( this.posSelector );
        
        // function selector 
        this.funSelect = app.CreateSpinner();
        this.funSelect.SetOnChange( (value)=>{
            this.funSelectDesc.SetText(this.byName(value))
        });
        this.funSelect.SetList( this.prepareZoneFunctionList() )
        this.layDlg.AddChild( this.funSelect );

        this.funSelectDesc = app.CreateText( this.ZoneTypes[0].description, -1, -1, "Left,Multiline" )
        this.funSelectDesc.SetMargins(0.01, 0, 0.01, 0)
        this.layDlg.AddChild( this.funSelectDesc );
        //end function selector
        
        //Submit
        this.btn = app.CreateButton( "[fa-check] {0}".format(T("common_submit")), -1,-1, "FontAwesome,SingleLine" ) ;
        this.btn.SetPadding(0.02, 0.02, 0.02, 0.02);
        this.btn.SetOnTouch( ()=>{
            if(this.posSelector.GetText().length == 2) {
                this.dlg.Dismiss();
                PrepareSendMessage("D{0}#{1}#".format(
                    this.posSelector.GetText(),
                    this.byName(this.funSelect.GetText(), "index")
                ));
            }
            else {
                app.ShowPopup( T("common_validation-error") );
                this.AnimateError(this.posSelector)
            }
        });
        this.layDlg.AddChild( this.btn );
        
        this.dlg.Show();
	});
	this.Page.AddChild( this.txt )
};

prepareZoneFunctionList() {
    let lst = [];
    for (let key in this.ZoneTypes) {
        lst.push(this.ZoneTypes[key].name)
    }
    return lst.join(",");
};

byName(zoneName, obj="desc") {
    for (let key in this.ZoneTypes) {
        if (this.ZoneTypes[key].name ==  zoneName){
            if (obj == "desc")
                return this.ZoneTypes[key].description
            if (obj == "index")
                return key
        }
    }
}

}    // end of class
