/*
 * Loan.java
 *
 * Created on January 15, 2002, 10:41 AM
 */
package com.imsl.demo.jsp;
import com.imsl.chart.*;
import com.imsl.math.PrintMatrix;
import com.imsl.math.PrintMatrixFormat;
import com.imsl.finance.Finance;
import java.text.*;
import java.io.*;
import javax.servlet.http.*;

/**
 *  Loan amortization demo
 */
public class Loan extends JspBean {
    static private final NumberFormat  DOLLAR_FORMAT = NumberFormat.getCurrencyInstance();
    static private String[] columnLabels = {"Principal", "Interest", "Interest Cost", "Total Payments"};
    
    private Chart   principalChart;
    private Chart   interestChart;
    private JspBean beanPrincipalChart;
    private JspBean beanInterestChart;
    private double  amount;
    private double  rate;
    private int     years;

    /** Creates new Loan */
    public Loan() {
        amount = 100000.;
        rate = 8.0;
        years = 10;
        beanPrincipalChart = new JspBean();
        beanPrincipalChart.setSize(200, 200);
        beanInterestChart = new JspBean();
        beanInterestChart.setSize(200, 200);
    }
    
    public void setAmount(double amount) {
        this.amount = amount;
    }
    
    public double getAmount() {
        return amount;
    }

    
    public void setRate(double rate) {
        this.rate = rate;
    }
    
    public double getRate() {
        return rate;
    }
    
    
    public void setYears(int years) {
        this.years = years;
    }
    
    public int getYears() {
        return years;
    }
    
    public String getPayment() {
        double payment = -Finance.pmt(0.01*rate/12., 12*years, amount, 0, Finance.AT_END_OF_PERIOD);
        return DOLLAR_FORMAT.format(payment);
    }
    
    public String getTotalInterest() {
        double r = 0.01*rate/12.;
        double interest = -Finance.cumipmt(r, 12*years, amount, 1, 12*years, Finance.AT_END_OF_PERIOD);
        return DOLLAR_FORMAT.format(interest);
    }
    
    public String getTable() {
        int nper = 12*years;
        double table[][] = new double[nper-1][4];
        double ppmt[] = new double[nper-1];
        double ipmt[] = new double[nper-1];
        double cumppmt[] = new double[nper-1];
        double cumipmt[] = new double[nper-1];
        double r = 0.01*rate/12.;
        for (int iper = 1;  iper < nper;  iper++) {
            double row[] = table[iper-1];
            row[0] = -Finance.ppmt(r, iper, nper, amount, 0, Finance.AT_END_OF_PERIOD);
            row[1] = -Finance.ipmt(r, iper, nper, amount, 0, Finance.AT_END_OF_PERIOD);
            row[2] = -Finance.cumprinc(r, nper, amount, 1, iper, Finance.AT_END_OF_PERIOD);
            row[3] = -Finance.cumipmt(r, nper, amount, 1, iper, Finance.AT_END_OF_PERIOD);
        }
        PrintMatrixFormat pmf = new MyPrintMatrixFormat();
        pmf.setColumnLabels(columnLabels);
        pmf.setNumberFormat(DOLLAR_FORMAT);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(bos);
        new PrintMatrix(ps, "Amortization Table").printHTML(pmf, table, table.length, table[0].length);
        return bos.toString();
    }
    
    /**
     *  PrintMatrix format with right-aligned entries.
     */
    static private class MyPrintMatrixFormat extends PrintMatrixFormat {
        public String format(int type, Object entry, int row, int col, ParsePosition pos) {
            switch (type) {
                case PrintMatrixFormat.BEGIN_ENTRY:
                    return "<TD align='right'>";
                default:
                    return super.format(type, entry, row, col, pos);
            }
        }
    }
    
    /**
     *  Create the charts
     */
    public void createCharts(HttpServletRequest request) {
        createPrincipalChart(request);
        createInterestChart(request);
    }
    
    /**
     *  Create the principal paid chart
     */
    private void createPrincipalChart(HttpServletRequest request) {
        principalChart = new Chart((java.awt.Component)null);
        beanPrincipalChart.registerChart(principalChart, request);
        principalChart.getChartTitle().setTitle("Principal Paid");
        AxisXY axis = new AxisXY(principalChart);
        axis.setTextFormat("0");
        axis.getAxisX().getAxisTitle().setTitle("Months");
        axis.getAxisY().getAxisTitle().setTitle("Dollars (x1000)");
        
        int nper = 12*years;
        double x[] = new double[nper-1];
        double y[] = new double[nper-1];
        for (int iper = 1;  iper < nper;  iper++) {
            double r = 0.01*rate/12.;
            x[iper-1] = iper;
            y[iper-1] = -0.001*Finance.cumprinc(r, nper, amount, 1, iper, Finance.AT_END_OF_PERIOD);
        }
        Data data = new Data(axis, x, y);
        data.setDataType(Data.DATA_TYPE_FILL);
        data.setFillColor("green");
    }
    
    /**
     *  Create the interest costs chart
     */
    private void createInterestChart(HttpServletRequest request) {
        interestChart = new Chart();
        beanInterestChart.registerChart(interestChart, request);
        interestChart.getChartTitle().setTitle("Interest Costs");
        AxisXY axis = new AxisXY(interestChart);
        axis.setTextFormat("0");
        axis.getAxisX().getAxisTitle().setTitle("Months");
        axis.getAxisY().getAxisTitle().setTitle("Dollars (x1000)");
        
        int nper = 12*years;
        double x[] = new double[nper-1];
        double y[] = new double[nper-1];
        for (int iper = 1;  iper < nper;  iper++) {
            double r = 0.01*rate/12.;
            x[iper-1] = iper;
            y[iper-1] = -0.001*Finance.cumipmt(r, nper, amount, 1, iper, Finance.AT_END_OF_PERIOD);
        }
        Data data = new Data(axis, x, y);
        data.setDataType(Data.DATA_TYPE_FILL);
        data.setFillColor("red");
    }
    
    
    public String getPrincipalChartImageTag() {
        return beanPrincipalChart.getImageTag();
    }
    
    public String getInterestChartImageTag() {
        return beanInterestChart.getImageTag();
    }
}