class page_contact 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 );

    // query device for numbers
	this.txt = app.CreateText( "[fa-address-book-o] {0}".format(T("page-contacts_query")), -1, -1, "Multiline,FontAwesome" )
	this.txt.SetTextSize( this.OptionFontSize )
	this.txt.SetPadding(0, 0, 0, 0);
	this.txt.SetOnTouchUp( function(){PrepareSendMessage("A#")} );
	this.Page.AddChild( this.txt )
	
	//add new number into device
	this.txt = app.CreateText( "[fa-plus-square-o] {0}".format(T("page-contacts_add-new")), -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-contacts_add-new"), "Modal" );
        this.dlg.SetOnBack( ()=>{
            this.dlg.Dismiss();
        });
        
        this.layDlg = app.CreateLayout( "linear", "Vertical,FillXY" );
        this.dlg.AddLayout( this.layDlg );
        //possition in list
        this.horLay = app.CreateLayout( "linear", "Horizontal,FillXY" );
        this.layDlg.AddChild( this.horLay );
        
        this.txt = app.CreateText( T("common_position-in-list"), -1, -1, "Multiline," )
        // this.txt.SetTextSize( this.OptionFontSize-20 );
        this.horLay.AddChild( this.txt );
        
        this.posSelector = app.CreateTextEdit( "", false, false, "AutoSelect,SingleLine,Right,Numbers" );
        this.posSelector.SetHint("1-9")
        // this.posSelector.SetTextSize( this.OptionFontSize-20 );
        this.posSelector.SetOnChange( ()=>{
            // single character
            if(! /(^[1-9]$)/.test(this.posSelector.GetText())){
                this.posSelector.SetText("")
                app.ShowPopup( T("common_validation-error") );
                this.AnimateError(this.posSelector);
            }     
        });

        this.horLay.AddChild( this.posSelector );
        //Function 1
        this.txt = app.CreateText( T("page-contacts_how-to-inform-on-alarm"), -1, -1, "Multiline" )
        // this.txt.SetTextSize( this.OptionFontSize-20 );
        this.layDlg.AddChild( this.txt );
        
        this.f1Selector = app.CreateSpinner("1 {0},2 {1},3 {2}".format(
                T("page-contacts_inform-on-alarm_call-and-sms"),
                T("page-contacts_inform-on-alarm_sms"),
                T("page-contacts_inform-on-alarm_call")
        ));
        // this.f1Selector.SetTextSize( this.OptionFontSize-20 );
        this.layDlg.AddChild( this.f1Selector );

        //function 2
        this.txt = app.CreateText( T("page-contacts_device-response-on-call"), -1, -1, "Multiline" )
        this.txt.SetMargins( 0.01, 0, 0.01, 0 )
        this.layDlg.AddChild( this.txt );
        
        this.f2Selector = app.CreateSpinner("1 {0},2 {1},3 {2}".format(
            T("page-contacts_device-response-on-call_change-status"),
            T("page-contacts_device-response-on-call_auto-pick-up"),
            T("page-contacts_device-response-on-call_ring")
        ));
        // this.f2Selector.SetTextSize( this.OptionFontSize-20 );
        this.layDlg.AddChild( this.f2Selector );

        //phone number
        this.phoneInput = app.CreateTextEdit( "", false, false, "AutoSelect,SingleLine,Right,Phone" );
        this.phoneInput.SetHint("+370 6## #####")
        this.phoneInput.SetOnFocus( (state)=>{
            if (state) {
                app.ChooseContact( "phone", (name, number)=>{
                    app.ShowPopup( name + " " + number );
                    this.phoneInput.SetText( number.replaceAll(" ", "") );
                });
            }
            app.HideKeyboard()
        });
        
        this.layDlg.AddChild( this.phoneInput );
        
        //submit button
        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()!="" && this.phoneInput.GetText()!=""){
                this.dlg.Dismiss();
                PrepareSendMessage("{0}A{1}#{2}#{3}#".format(
                    this.posSelector.GetText(),
                    this.f1Selector.GetText()[0],
                    this.f2Selector.GetText()[0],
                    this.phoneInput.GetText()
                ));
            }else if(this.posSelector.GetText()==""){
                this.AnimateError(this.posSelector);
            }else if(this.phoneInput.GetText()==""){
                this.AnimateError(this.phoneInput);
            }
        });
        this.layDlg.AddChild( this.btn );
        
        this.dlg.Show();
	});
	this.Page.AddChild( this.txt )
	
	// remove number from device
	this.txt = app.CreateText( "[fa-minus-square-o] {0}".format(T("page-contacts_remove-number")), -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-contacts_remove-number"), "Modal" );
        this.dlg.SetOnBack( ()=>{
            this.dlg.Dismiss();
        });
        
        this.layDlg = app.CreateLayout( "linear", "Vertical,FillXY" );
        this.dlg.AddLayout( this.layDlg );
        
        //possition in list
        this.txt = app.CreateText( "{0} (1-9)".format(T("common_position-in-list")), -1, -1, "NoWrap" )
        this.layDlg.AddChild( this.txt );
        
        this.posSelector = app.CreateTextEdit( "", false, false, "AutoSelect,SingleLine,Right,Numbers" );
        this.posSelector.SetHint("1-9")
        this.posSelector.SetOnChange( ()=>{
            // single character
            if(! /(^[1-9]$)/.test(this.posSelector.GetText())){
                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()!=""){
                this.dlg.Dismiss();
                PrepareSendMessage(this.posSelector.GetText()+"A#");
            } else {
                this.AnimateError(this.posSelector);
            }
        });
        this.layDlg.AddChild( this.btn );
        
        this.dlg.Show();
	});
	this.Page.AddChild( this.txt )
	
	this.txt = app.CreateText( "[fa-bell-o] {0}".format(T("page-contacts_arm-notifications")), -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-contacts_arm-notifications"), "Modal" );
        this.dlg.SetOnBack( ()=>{
            this.dlg.Dismiss();
        });

        this.layDlg = app.CreateLayout( "linear", "Horizontal,FillXY" );
        this.layDlg.SetPadding(0.01, 0.01, 0.01, 0.01);
        this.dlg.AddLayout( this.layDlg );
    
        this.btn = app.CreateButton( "[fa-commenting-o] {0}".format(T("page-contacts_arm-notifications-sms")), -1,-1, "FontAwesome,SingleLine" ) ;
        // this.btn.SetPadding(0.02, 0.02, 0.02, 0.02);
        this.btn.SetOnTouch( ()=>{
            this.dlg.Dismiss();
            PrepareSendMessage("H#")
        });
        this.layDlg.AddChild( this.btn );
        
        this.btn = app.CreateButton( "[fa-ban] {0}".format(T("common_disable")), -1,-1, "FontAwesome,SingleLine" );
        // this.btn.SetPadding(0.02, 0.02, 0.02, 0.02);
        this.btn.SetOnTouch( ()=>{
            this.dlg.Dismiss();
            PrepareSendMessage("J#")
        });
        this.layDlg.AddChild( this.btn );
    
        this.dlg.Show();
	});
	this.Page.AddChild( this.txt )
};


}   // end of class
