/**
 * Keeps information about a transaction
 * @author bjora857
 */

function Transaction() {

    /**
     *parses Json and creates a transaction
     */
    this.parseJSON = function (json) {
        this.id = json.id;
        this.name = json.name;
        this.amount = json.amount;
        this.date = json.date;
        this.description = json.description;
        this.dataString = json.dataString;
        this.dataName = json.dataName;
        this.user = json.user;
        this.category = json.category;
        this.bankAccount = json.bankAccount;
    };

    /**
     *Takes a table row and creates a transaction
     */
    this.parseEditRow = function (row) {
        this.id = row.attr('id').substr(3);
        this.name = $(row).find('input[name=name]').val();
        this.amount = $(row).find('input[name=amount]').val();
        this.description = $(row).find('input[name=description]').val();
        this.date = $(row).find('input[name=date]').val();
        this.category = $(row).find('select[name=category]').val();
    };

    this.getId = function () {
        return this.id;
    };
    
    this.getType = function () {
      if (this.getAmount() >0) {
        return "expense";
    }else {
        return "income";
    }
    
        
        
      
        
    };

    this.getName = function () {
        return this.name;
    };

    this.getAmount = function () {
        return this.amount;
    };

    this.getDate = function () {
        return this.date;
    };

    this.getDescription = function () {
        return this.description;
    };

    this.getDataString = function () {
        return this.dataString;
    };

    this.getDataName = function () {
        return this.dataName;
    };

    this.getUser = function () {
        return this.user;
    };

    this.getCategory = function () {
        return this.category;
    };

    this.setId = function (id) {
        this.id = id;
    };

    this.setName = function (name) {
        this.name = name;
    };

    this.setAmount = function (amount) {
        this.amount = amount;
    };

    this.setDate = function (date) {
        this.date = date;
    };

    this.setDescription = function (description) {
        this.description = description;
    };

    this.setDataString = function (dataString) {
        this.dataString = dataString;
    };

    this.setDataName = function (dataName) {
        this.dataName = dataName;
    };

    this.setUser = function (user) {
        this.user = user;
    };

    this.setCategory = function (category) {
        this.category = category;
    };

    this.getRealDate = function () {
        var realDate = new Date(this.date);

        return realDate;
    };
    
    this.getBankAccount = function () {
        return this.bankAccount;
    };
    
    this.setBankAccount = function (bankAccount) {
        this.bankAccount = bankAccount;   
    };

    this.getBank = function () {
        var prefix = this.dataString.substring(0, 4);

        if (prefix === 1000) {
            return "Swedbank";
        }
        return "SEB";
    };

    /**
     * Returns a table row with the content in the transaction 
     * @returns json transaction
     * @type json
     */
    this.getJSON = function () {
        var json = {
            id: this.getId(),
            name: this.getName(),
            amount: this.getAmount(),
            date: this.getDate(),
            description: this.getDescription(),
            category: this.getCategory(),
            dataString: this.getDataString(),
            dataName: this.getDataName(),
            user: this.getUser(),
            bankAccount: this.getBankAccount()
        };
        return json;
    };
}
