/**
 * View for rendering html concerning categories
 * @author bjora857
 */

function Categoryview() {

    this.renderCategories = function () {

    };

    this.getTransactions = function () {
        var html = '';
        html += grayBoxStart("Transaktioner");
        html += '<table class="sumTrans">';

        var month = '';
        var sum = 0;
        $(this.transpack.transactions).each(function () {
            
            
            if(this.getRealDate().toString('MM') !== month) {
                // new month
                if(sum !== 0){
                html += '<tr><td></td><td></td><td class="amount"><b>'+displayAmount(sum)+'</b></td></tr>';
                html += '<tr><td colspan="3"><hr/></td></tr>';
            }
                sum = 0;

                html += '<tr><td colspan="3"><label>'+ucFirst(this.getRealDate().getMonthName())+'</label></td></tr>';
                
            }
            month = this.getRealDate().toString('MM');
            sum += parseFloat(this.getAmount(), 10);
            
            html += '<tr><td class="name">' + this.getName() + '</td><td class="date">' + this.getDate() + '</td><td class="amount">' + this.getAmount() + '</td></tr>';
        });
        
        html += '<tr><td></td><td></td><td class="amount"><b>'+displayAmount(sum)+'</b></td></tr>';
        
        html += '</table>';
        html += grayBoxEnd();

        return html;
    };

    this.getVendors = function () {
        var vendors = this.getTransactionsByVendorData();
        var html = grayBoxStart("Butiker");
        html += '<table class="vendors">';

        $(vendors).each(function () {

            if (this.vendors.length > 1) {
                html += '<tr><td class="name">' + this.name + '</td>';

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

                html += '<td class="num">' + num + '</td>';
                html += '<td class="amount">' + Math.round(total) + '</td></tr>';
            }
        });

        html += '</table>';
        html += grayBoxEnd();

        return html;
    };

    this.getSummary = function () {
           var html = grayBoxStart("Summering");
           
           //perhaps sort the transpack
           // Does it sort by reference???
           
           html += '<table>';
           
           html += '<tr><td><label>Antal</label></td><td>'+ this.transpack.getNumberOfTransactions()+'</td></tr>';
           html += '<tr><td><label>Summa</label></td><td>'+ this.transpack.getTotalAmount()+'</td></tr>';
           html += '<tr><td><label>Största transaktion</label></td><td>'+ this.transpack.getMaxAmount()+'</td></tr>';
           html += '<tr><td><label>Snitt summa</label></td><td>'+ this.transpack.getAverageAmount()+'</td></tr>';
           

           html += '</table>';
           html += grayBoxEnd();

           return html;
    };

    this.getTransactionsByVendorData = function () {
        var a = Array();
        var returner = Array();
        var temp = $(this.transpack.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.getMonthByMonth = function () {
           var html ='';
           
           var monthTranspacks = this.transpack.getMonthTranspacks();
           
           html += '<div id="monthByMonthGraph"></div>';
           
           
           html += '<table class="zebra">';
           
           $(monthTranspacks).each(function () {
                html += '<tr>';
                html += '<td>'+this.getFirstDate().getMonthName()+' '+this.getFirstDate().getFullYear()+'</td>';
                html += '<td>'+this.getTotalAmount()+'</td>';
                html += '</tr>';
                
           });
           
           html += '</table>';
           
           
           return html;
    };
    

    this.setTranspack = function (transpack) {
        this.transpack = transpack;
        return true;
    };

}
