﻿/// <summary>
/// Clover ui box, regiser's a jquery box wiget that implements expand, load, refresh, state, settings, search, footer actions
/// </summary>
var iButton = (function () {

    //#region constructor
    var iButton = function () { };
    //#endregion

    //#region options
    /// <summary>
    /// widget options default options
    /// </summary>
    iButton.prototype.options = {
        widgetName: 'iButton',
        version: '0.1b',
        json: '',
        type: '',
        txtOn: INDACO.resources.buy,
        txtWait: INDACO.resources.wait,
        txtError: INDACO.resources.attention,
        txtOff: INDACO.resources.inCart,
        urls: {
            indexUrl: '',   // the index url, we append this index to all urls if defined
            ajaxUrl: '', // save action address
            redirectUrl: '' // does redirect when the ajax finished ok
        },
        updateHtml: '', // html element id if respond must update ui
        initialState: 'On',
        _state: ''  // current state
    };
    //#endregion

    //#region init
    /// <summary>
    /// _init method, called after construct and re-init
    /// </summary> 
    iButton.prototype._init = function () {
        this.options._state = this.options.initialState;

        this.element.addClass('iButton');
        if (this.element.hasClass('disabled')) {
            this.options._state = "Off";
        }
        else {
            this.options._state = "On";
            this.options.txtOn = this.element.text();
            this.options.type = this.element.data('type');
        }

        if (!this.options.json) {
            this.options.json = this.element.attr("id").split('-')[1]; //based on #btn-1312 pattern
        }

        this.element.on('click', $.indaco.__bind(
                this._callServer
            , this));
    };
    //#endregion

    iButton.prototype.changeState = function (state) {
        if (state) {
            this.options._state = state;
        }

        if (this.element.hasClass('error')) {
            this.element.next().remove();
        }

        this.element.removeClass('loader');
        this.element.removeClass('error');
        this.element.removeClass('disabled');
        switch (this.options._state) {
            case 'On':
                this.element.text(this.options.txtOn);
                this.element.prop('disabled', false);
                break;
            case 'Error':
                this.element.text(this.options.txtError);
                this.element.addClass('error');
                this.element.prop('disabled', false);
                this.element.after('<span class="button-error-message"><span>' + INDACO.resources.fatalErrorTryAgain + '<span></span></span></span>');
                break;
            case 'Wait':
                this.element.text(this.options.txtWait);
                this.element.addClass('loader');
                this.element.removeClass('disabled');
                break;
            case 'Off':
                this.element.text(this.options.txtOff);
                this.element.addClass('disabled');
                break;
            default:
                this.element.text(this.options.txtOn);
        }
    };

    iButton.prototype._callServer = function (event) {
        if (event) {
            event.preventDefault();
        }

        if (this.options._state == 'Wait' || this.options._state == 'Off') {
            return false;
        }

        this.changeState('Wait');

        var ajaxOptions = $.extend(true, {}, $.indaco.__ajax);
        ajaxOptions.url = this.options.urls.ajaxUrl;
        ajaxOptions.data = 'id=' + this.options.json + '&type=' + this.options.type;

        //success
        ajaxOptions.success = $.indaco.__bind(
                this._ajaxSuccess
            , this);

        // error
        ajaxOptions.error = $.indaco.__bind(
                this._ajaxError
            , this);

        // complete
        ajaxOptions.complete = $.indaco.__bind(
                this._ajaxComplete
            , this);

        // ajax call
        $.ajax(ajaxOptions);

    };

    iButton.prototype._ajaxSuccess = function (data) {

        if (data && data.obj && data.obj.length > 0) {
            if (this.options.urls.redirectUrl && this.options.urls.redirectUrl.length > 0) {
                window.location.href = this.options.urls.redirectUrl;
                return;
            }
            if (this.options.updateHtml && this.options.updateHtml.length > 0) {
                $(this.options.updateHtml).html(data.obj);
                $(this.options.updateHtml).parent().show();
            }
            this.changeState('Off');
            return;
        }
        this._ajaxError(data);
    };

    iButton.prototype._ajaxError = function (data) {
        this.changeState('Error');
        if (data.status) {
            // append error message tooltip
        } else {
            // network error
        }
    };

    iButton.prototype._ajaxComplete = function (data) {
    };

    return iButton;
})();

// register the widget
$.widget('indaco.iButton', new iButton());
