(function (paw, utils, Popup, IrisComponent) {

    function PawIris(irisDef, settings, localization) {
        this.irisDef = irisDef;

        if(!utils.value.isSet(this.irisDef.irisId)){
            throw "[PAW IRIS] Iris def. is missing IRIS ID (property irisId)";
        }
        this.irisId = irisDef.irisId;
        
        this.settings = paw.lang.append({
            irisUrl: '@Url.Action("Get", "Iris)',
            qParamTrigger: null,
            helpUrl: undefined,
            helpId: undefined
        }, settings || {});
        this.localization = paw.lang.append({
            iris: '@Loc.IRIS',
            viewInstructions: '@Loc.ViewInstructions',
            viewInstructionsDescription: '@Loc.ViewInstructionsDescription'
        }, localization || {});

        this.popup = null;
    }

    PawIris.prototype.init = function () {
        if (utils.value.isSet(this.settings.qParamTrigger)) {
            var qparams = utils.value.getQparams(document.location, ['iris']);
            utils.value.deleteQparams(document.location, ['iris'], true);
            if(qparams.iris === this.irisId){
                this.open();
                return;
            }
        }
    };

    PawIris.prototype.load = function () {
        var self = this;
        return utils.async.get(this.settings.irisUrl + qparams({
            irisId: this.irisId
        })).then(function(irisModel){
            self.irisModel = irisModel;
            return 
        });
    };

    PawIris.prototype.open = function(){
        var self = this;
        return this.load().then(function(){
            self.popup = new Popup('iris-popup-' + self.irisId, {
                title: self.localization.iris + ' ' + self.irisModel.HelpContext + " - " + self.irisModel.Caption,
                Component: IrisComponent,
                bind: {
                    irisId: self.irisId,
                    irisDef: self.irisDef,
                    irisModel: self.irisModel,
                    localization: self.localization,
                    helpUrl: utils.value.isSet(self.settings.helpUrl) && utils.value.isSet(self.settings.helpId)? self.settings.helpUrl + self.settings.helpId : null
                },
                maximizable: false,
                draggable: true,
                type: 'modal',
                styleClass: 'paw-iris-popup modal-body modal-dialog',
            });
            self.irisDef.popup = self.popup;
            self.popup.show();
            self.popup.$popup.find('.custom-modal-header').addClass('iris-title m-b-sm');
        });
    };

    paw.mod.iris.Iris = PawIris;
})(paw, paw.utils, paw.com.Popup, paw.mod.iris.IrisComponent);