//  @import com.hedgehoglab.application.controller.BaseController;
//  @import com.hedgehoglab.utils.Async;
/**
 *
 * @param scope
 */



function TagDialogController(scope){
    BaseController.call(this,scope);
    var _this=this;
    var arg=new ARGH();

    var _value=null;

    scope.contact_edit_view.subscribe_to('on_tag_contacts_clicked',function(e){
        this.arg = new ARGH();
        scope.tag_dialog_view.show({
            contacts:scope.contacts_model.model().contact
            ,indices:scope.selected_contacts_model.model()
        });
    });

    /**
     * Hook onto the dialog view when tag has changed and perform a service query
     */
    scope.tag_dialog_view.subscribe_to('on_tag_changed',function(ee){
        arg.value=ee.value;
        scope.tag_service.get_tags_by_search_term(ee.term,function(e){
            var dat="";
            if(e!=null){

                if(!$.isArray(e.tag)) e.tag=[e.tag];

                $.each(e.tag,function(i,n){
                    dat+=n.label+"\n";
                    arg.tag_lookup[n.label]=n.label;
                });
            }
            scope.tag_dialog_view.update_matches(dat);
        });
    });


    scope.tag_dialog_view.subscribe_to('on_tag_selected',function(ee){
        arg.possible_chosen[ee.value]=true;
    });


    this.on_tagging_complete = function(count){
        if(count == scope.selected_contacts_model.model().length){
            // scope.contacts_list_view.raise_event('on_selected_contacts_changed',{model:scope.selected_contacts_model.model()});
        }
    };

    //  On confirm tag the contacts!
    scope.tag_dialog_view.subscribe_to('on_confirm_clicked',function(e){
        var tags=[];
        if(e.value.indexOf(",") > 0){
            var splitValues = e.value.split(",");
            for(var index in splitValues){
                if(splitValues[index].length > 0){
                    tags.push({label:splitValues[index]});
                }
            }
        } else if(e.value.length > 0){
            tags.push({label:arg.value});
        }

        if(tags.length > 0){
            var count = 1;
            $.each(scope.selected_contacts_model.model(),function(i,n){
                var contact=scope.contacts_model.contact(n);
                scope.contacts_service.add_tags_to_contact(contact.id,tags,function(e){
                    scope.contacts_service.get_contact_by_id(e.contact,function(contact){
                        scope.contacts_model.contact(n,contact);
                        _this.on_tagging_complete(i+1);
                    });
                });
            });
            scope.message_service.show_message({message:"Tags added to contacts",duration:"5000",type:"info"});
        } else {
            scope.message_service.show_message({message:"Nothing to tag as no tags were entered",duration:"5000",type:"info"});
        }

        e.dialog.hide();
    });

    scope.tag_dialog_view.subscribe_to('on_cancel_clicked',function(e){
        e.hide();
    });

    function ARGH(){
        this.tag_lookup={};
        this.possible_chosen={};
        this.value="";
    };
};