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



function Category() {
    this.transactions = Array();

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

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

    this.setDesc = function (desc) {
        this.desc = desc;
    };

    this.setGroupId = function (id) {
        this.groupId = id;
    };

    this.getId = function () {
        return this.id;
    };

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

    this.getDesc = function () {
        return this.desc;
    };

    this.getGroupId = function () {
        return this.groupId;
    };

    this.addTransaction = function (trans) {
        //perhaps see if it already exits...
        this.transactions.push(trans);
    };

    this.getTransactions = function () {
        return this.transactions;
    };

    this.getNumberOfTransactions = function () {
        return (this.transactions.length);
    };

    this.getLargestTransaction = function () {

        var max = 0;
        var trans;
        $(this.transactions).each(function () {
            if (this.getAmount() > max) {
                max = this.getAmount();
                trans = this;
            }
        });
        return trans;
    };

    this.getSmallestTransaction = function () {

        var min = 9999999999;
        var trans;
        $(this.transactions).each(function () {
            if (this.getAmount() < min) {
                min = this.getAmount();
                trans = this;
            }
        });

        return trans;
    };

    this.getTotalAmount = function () {

        var total = 0;
        $(this.transactions).each(function () {
            total = total + parseFloat(this.getAmount());
        });

        return Math.round(total);
    };

    this.getAverageAmount = function () {
        var average = 0;
        average = this.getTotalAmount() / this.getNumberOfTransactions();

        if (average > - 1) {
            return Math.round(average);
        } else {
            return 0;
        }
    };

    this.getTransactionsByVendor = function () {
        var a = Array();
        var returner = Array();
        var temp = $(this.transactions).sort("name", "asc");

        var tempName = "";

        //collect vendor names
        $(temp).each(function () {
            if (this.getName() !== tempName) {
                a.push(this.getName());
                tempName = this.getName();
            }
        });

        // compare vendor name with all transactions
        $(a).each(function () {
            var name = this;
            var vendors = Array();

            // push the matching transactions
            $(temp).each(function () {
                if (name === this.getName()) {
                    vendors.push(this);
                }
            });

            if (vendors.length > 0) {
                returner.push({
                    name: name,
                    vendors: vendors
                });
            }
        });

        return returner;
    };

    this.getPeriodDays = function () {
        
        var temp = $(this.transactions).sort("date", "asc");
        var diff = 0;
        var days = 1;

        if (temp.length > 0) {
            diff = $(temp).last()[0].getRealDate().getTime() - $(temp).first()[0].getRealDate().getTime();
            days = Math.round(diff / (1000 * 3600 * 24));
        }
        return days;
    };

    this.getTotalDaysByReport = function () {
        var days = this.totalDays;
        if (days > 0) {
            return days;
        }
        else {
            return -1;
        }

    };

    this.getAmountPerMonth = function () {
        var days = this.getTotalDaysByReport();
        var amount = this.getTotalAmount();

        if (days === 0) {
            return Math.round(amount);
        }
        else {
            return Math.round(30 * (amount / days));
        }
    };

    this.getPercentageOfTotal = function (total) {
        if (total === undefined) {
            return '';
        }
        var percentage = this.getTotalAmount() / total;

        return parseInt(percentage * 100, 10);
    };


    this.makeSortable = function () {
        this.totalAmount = this.getTotalAmount();
    };

    this.setTotalDays = function (days) {
        this.totalDays = days;
    };
}
