const MYSHOWS_SUCCESS = 1;
const MYSHOWS_ACCESSDENIED = 2;
const MYSHOWS_NETWORKERROR = 3;

/**
 * MyShows API object
 */
function MyShowsAPI ( ) {
    this.host = 'http://api.myshows.ru/';
    this.lastError = MYSHOWS_SUCCESS;
};

/**
 * Simple ajax function to work with myshows
 * @param url
 * @param params
 * @param callback
 */
MyShowsAPI.prototype.ajax = function( url, params, callback ) {
    var httpRequest = new XMLHttpRequest();
    var self = this;
    var paramString = '';
    if ( typeof( params ) != 'undefined' ) {
        var paramArray = [ ];
        for ( var key in params ) {
            paramArray[ paramArray.length ] = encodeURIComponent(key) + '=' + encodeURIComponent(params[key]);
        }
        paramString = paramArray.join("&").replace(/%20/g, "+");
    }
    if ( paramString != '' ) {
        url += '?' + paramString;
    }
    httpRequest.open( "GET", url, true );
    httpRequest.send( );
    httpRequest.onreadystatechange = function () {
        if ( httpRequest.readyState == 4 ) {
            callback.call( self, httpRequest );
        }
    }
};

/**
 * Get last error
 * @return int
 */
MyShowsAPI.prototype.getLastError = function () {
    return this.lastError;
};

/**
 * Authentification
 * @param login
 * @param md5password
 * @param callback
 */
MyShowsAPI.prototype.auth = function ( login, md5password, callback ) {
    this.ajax( this.host + 'profile/login', {'password': md5password, 'login': login }, function ( httpRequest ) {
        if ( httpRequest ) {
            switch ( httpRequest.status ) {
                case 200:
                    this.lastError = MYSHOWS_SUCCESS;
                    callback.call( this );
                    return;
                break;
                case 401: case 403: case 404:
                    this.lastError = MYSHOWS_ACCESSDENIED;
                    callback.call( this );
                    return;
                break;
            }
        }
        this.lastError = MYSHOWS_NETWORKERROR;
        callback.call( this );
    } );
}

/**
 * Get all user shows
 * @param callback
 */
MyShowsAPI.prototype.getShows = function ( callback ) {
    this.ajax( this.host + 'profile/shows/', {}, function ( httpRequest ) {
        if ( httpRequest ) {
            switch ( httpRequest.status ) {
                case 200:
                    var responseObject = eval( '(' + httpRequest.responseText + ')' );
                    this.lastError = MYSHOWS_SUCCESS;
                    callback.call( this, responseObject );
                    return;
                break;
                case 401:
                    this.lastError = MYSHOWS_ACCESSDENIED;
                    callback.call( this, false );
                    return;
                break;
            }
        }
        this.lastError = MYSHOWS_NETWORKERROR;
        callback.call( this, false );
    } );
}

/**
 * Get all unwatched episodes
 * @param callback
 */
MyShowsAPI.prototype.getAllUnwatchedEpisodes = function ( callback ) {
    this.ajax( this.host + 'profile/episodes/unwatched/', {}, function ( httpRequest ) {
        if ( httpRequest ) {
            switch ( httpRequest.status ) {
                case 200:
                    var responseObject = eval( '(' + httpRequest.responseText + ')' );
                    this.lastError = MYSHOWS_SUCCESS;
                    callback.call( this, responseObject );
                    return;
                break;
                case 401:
                    this.lastError = MYSHOWS_ACCESSDENIED;
                    callback.call( this, false );
                    return;
                break;
            }
        }
        this.lastError = MYSHOWS_NETWORKERROR;
        callback.call( this, false );
    } );
}

/**
 * Get all next episodes
 * @param callback
 */
MyShowsAPI.prototype.getAllNextEpisodes = function ( callback ) {
    this.ajax( this.host + 'profile/episodes/next/', {}, function ( httpRequest ) {
        if ( httpRequest ) {
            switch ( httpRequest.status ) {
                case 200:
                    var responseObject = eval( '(' + httpRequest.responseText + ')' );
                    this.lastError = MYSHOWS_SUCCESS;
                    callback.call( this, responseObject );
                    return;
                break;
                case 401:
                    this.lastError = MYSHOWS_ACCESSDENIED;
                    callback.call( this, false );
                    return;
                break;
            }
        }
        this.lastError = MYSHOWS_NETWORKERROR;
        callback.call( this, false );
    } );
}

MyShowsAPI.prototype.check = function ( episodeId, callback ) {
    this.ajax( this.host + 'profile/episodes/check/' + episodeId, {}, function ( httpRequest ) {
        if ( httpRequest ) {
            switch ( httpRequest.status ) {
                case 200:
                    callback.call( this );
                    this.lastError = MYSHOWS_SUCCESS;
                    return;
                break;
                case 401:
                    callback.call( this );
                    this.lastError = MYSHOWS_ACCESSDENIED;
                    return;
                break;
            }
        }
        callback.call( this );
        this.lastError = MYSHOWS_NETWORKERROR;
    } );
}

MyShowsAPI.prototype.uncheck = function ( episodeId, callback ) {
    this.ajax( this.host + 'profile/episodes/uncheck/' + episodeId, {}, function ( httpRequest ) {
        if ( httpRequest ) {
            switch ( httpRequest.status ) {
                case 200:
                    callback.call( this );
                    this.lastError = MYSHOWS_SUCCESS;
                    return;
                break;
                case 401:
                    callback.call( this );
                    this.lastError = MYSHOWS_ACCESSDENIED;
                    return;
                break;
            }
        }
        callback.call( this );
        this.lastError = MYSHOWS_NETWORKERROR;
    } );
}
