(function (paw, utils) {
    function PawIrisComponent($elt, popup) {
        this.irisId;
        this.irisUrl;
        this.irisDef;
        this.irisModel;
        this.localization;
        this.helpUrl;

        this.$elt = $elt;
        this.$content = null;
        this.irisModel = null;
        this.irisActions = {};
    }

    PawIrisComponent.prototype.markupDef = function(){
        var markup = [];
        markup.push('<div class="paw-iris-content"></div>');
        return markup;
    };
    
    PawIrisComponent.prototype.irisMarkupDef = function () {
        var self = this;
        var markup = [];
        var description = utils.value.isFunction(this.irisDef.compileDescription)? this.irisDef.compileDescription(this.irisModel.Description) : this.irisModel.Description;
        markup.push('<div>' +  description+ '</div>');
        markup.push('<div>');
        markup.push('   <ul class="paw-reset">');
        utils.iterable.iterate(this.irisModel.Items, function (irisItem) {
            var action = self.irisDef.getAction(irisItem);
            var irisActionId = 'iris-action-' + self.irisModel.HelpContext + '-' + irisItem.AnNo;
            self.irisActions[irisActionId] = action;

            if(!utils.value.isSet(action)){
                return;
            }
            markup.push('<li class="m-t-sm m-b-sm">');
            switch (action.type) {
                case 'button':
                    markup = markup.concat(self.irisActionMarkupDef(action, irisItem, irisActionId));
                    break;
                case 'link':
                    markup.push('<a id="' + irisActionId + '" href="' + action.link + '" class="paw-reset paw-width-fit">');
                    markup = markup.concat(self.irisActionMarkupDef(action, irisItem));
                    markup.push('</a>');
                    break;
                default:
                    markup = markup.concat(self.irisActionMarkupDef(action, irisItem));
                    break;
            }
            markup.push('</li>');
        });

        if(utils.value.isFunction(this.irisDef.getHelpURL)){
            markup = markup.concat(this.irisHelpActionMarkupDef(this.irisDef.getHelpURL()));
        }
        else if(utils.value.isSet(this.helpUrl)){
            markup = markup.concat(this.irisHelpActionMarkupDef(this.helpUrl));
        }        

        markup.push('   </ul>');
        markup.push('</div>');
        return markup;
    };

    PawIrisComponent.prototype.irisHelpActionMarkupDef = function (irisHelpUrl) {
        var irisActionId = 'iris-help-link-' + this.irisId;
        var action = {
            caption: this.localization.viewInstructions,
            description: this.localization.viewInstructionsDescription
        }
        var markup = [];
        markup.push('<a id="' + irisActionId + '" href="' + irisHelpUrl + '" target="_blank" class="paw-reset paw-width-fit">');
        markup = markup.concat(this.irisActionMarkupDef(action, null, irisActionId));
        markup.push('</a>');
        return markup;
    };

    PawIrisComponent.prototype.irisActionMarkupDef = function (action, irisItem, irisActionId) {
        var markup = [];
        markup.push('<button id="' + irisActionId + '" class="paw-btn paw-width-fit paw-text-left paw-iris-action">');
          
        markup.push('   <div class="well well-sm well-bg paw-well-no-b-m">');
        markup.push('   <div class="row">');
        markup.push('      <div class="col-xs-2">');
        markup.push('         <div class="icon-arrow-right"></div>');
        markup.push('      </div>');
        markup.push('      <div class="col-xs-10 well-text-margin">');
        markup.push('         <div>');
        markup.push('            <strong>' + (action.caption || irisItem.Caption) + '</strong>');
        markup.push('         </div>');
        markup.push('         <div>');
        markup.push('            <p class="no-margins">' + (action.description || irisItem.Description) + '</p>');
        markup.push('         </div>');
        markup.push('      </div>');
        markup.push('   </div>');
        markup.push('   </div>');
        markup.push('</button>');
        return markup;
    };

    PawIrisComponent.prototype.init = function(){
        var self = this;
        this.$content = this.$elt.find('.paw-iris-content');
        var markup = self.irisMarkupDef();
        self.$content.html(markup.join('\n'));
        this.$content.find('.paw-iris-action').on('click', function(e){
            var $elt = $(this);
            var action = self.irisActions[$elt.attr('id')];
            if(utils.value.isFunction(action.onSelected)){
                action.onSelected(e, action);
            }
        });
    };

    PawIrisComponent.prototype.destroy = function(){
        this.$content.find('.paw-iris-action').off();
    };

    paw.mod.iris.IrisComponent = PawIrisComponent;
})(paw, paw.utils);