wau.isBackground = true;
wau.isCordovaApplication = false;
wau.isAndroidBackground = true;

actions = {
    error: -2,
    exit: -1,
    readyToStart: 0,
    localNotification: 1,
    executeQuery: 2,
    log: 3,
    dbInsert: 4,
    dbUpdate: 5
};

function commWithAndroid(message) {
    if (typeof message !== 'string')
        message = JSON.stringify(message);
    console.log(wau.JAVA_COMM_PREFIX + message)
}

function bgNotification(args) {
    commWithAndroid({
        action: actions.localNotification,
        args: args
    })
}

function log(message) {
    message = {action: actions.log, "message": message};
    commWithAndroid(message);
}

window.onerror = function myErrorHandler(errorMsg, url, lineNumber) {
    commWithAndroid({action: actions.error, message: errorMsg, url: url, lineNumber: lineNumber});
    return false;
};

WAUModel.db = {
    transaction: function (query, args) {
        commWithAndroid({
            action: actions.executeQuery,
            query: query,
            args: args
        });
    }
};

WAUModel.prototype.bg_modifyModel = function (callback, action) {
    callback = callback || wau.getEmptyFunction();
    this.__Updated = 1;
    var genericInfo = this.getGenericInfo();
    window[this.tableName].prototype.cache[this.id] = this;
    commWithAndroid({
        action: action,
        table: genericInfo.tableName,
        values: genericInfo.values,
        columns: genericInfo.columns
    });
    callback();
};
WAUModel.prototype.insert = function (callback) {
    this.createdOn = this.createdOn || moment.utc().format(wau.dateFormat);
    this.updatedOn = this.updatedOn || moment.utc().format(wau.dateFormat);
    this.bg_modifyModel(callback, actions.dbInsert);
};

WAUModel.prototype.update = function (callback) {
    this.bg_modifyModel(callback, actions.dbUpdate);
};

WAUModel.prototype.execute = function (query, args, callback) {
    args = args || [];
    callback = callback || wau.getEmptyFunction();
    WAUModel.db.transaction(query, args);
    callback();

};

WAUModel.getAndroidCache();

var WSHubsAPI;

function backgroundInit(user) {
    if (WSHubsAPI !== undefined)return;
    User.mySelf = user;
    WSHubsAPI = new HubsAPI(wau.serverInfo.wsUrl + user.id + "/background");
    WAUModel.injection = {WSHubsAPI: WSHubsAPI};

    WSHubsAPI.connect(10);

    WSHubsAPI.callbacks.onReconnecting = function () {
        log("Reconnecting...");
    };

    WSHubsAPI.callbacks.onClose = function (error) {
        log("closed");
        if (error.code === 9999)
            commWithAndroid({action: actions.exit});
    };

    WSHubsAPI.callbacks.onOpen = function () {
        Task.prototype.syncModel(function () {
            notificationManager.notifyTasks();
        })
    };

    WSHubsAPI.UtilsHub.client.messageReceiver = function (message) {
        Task.prototype.select(message);
    };

    WSHubsAPI.TaskHub.client.taskUpdated = function (task) {
        taskEventHandler.handleTaskUpdated(task)
    };

    WSHubsAPI.TaskHub.client.newTask = function (task) {
        taskEventHandler.handleNewTask(task, WSHubsAPI)
    };

}

function setTasks(tasks) {
    log("getting tasks");
    tasks.forEach(function (task) {
        task = Task.prototype.fromJson(task);
        Task.prototype.cache[task.id] = task;
    })
}

function setUsers(users) {
    log("getting Users");
    users.forEach(function (user) {
        user = User.prototype.fromJson(user);
        User.prototype.cache[user.id] = user;
    })
}

function setPlaces(places) {
    log("getting Places");
    places.forEach(function (place) {
        place = Place.prototype.fromJson(place);
        Place.prototype.cache[place.id] = place;
    })
}

function readyToStart() {
    commWithAndroid({action: actions.readyToStart});
    //setTimeout(readyToStart, 3000);
}
setTimeout(readyToStart, 3000);
