import {PayInLenderIncomeModel} from '../abstract/pay-in-lender.income.model';
import {PayInModel} from './pay-in.model';
import {PayInResimacXlsModel} from './pay-in-resimac-xls.mode';

export class PayInResimacPdfModel extends PayInLenderIncomeModel {

  public Period: Date;
  public LoanNumber: string;
  public Borrower: string;
  public LoanAmount: number;
  public InterestRate: number;
  public Margin: number;
  public Settlement: Date;
  public Balance: number;
  public OffsetBalance: number;
  public GrossManFee: number;
  public IncentiveP: number;
  public Months: number;
  public SacrificeP: number;
  public Fee: number;
  public MoFee: number;
  public NetManFee: number;
  public IncomeAmount: number;

  constructor(payload: Partial<PayInResimacPdfModel>) {
    super('Resimac', payload);
    if ( payload.Period !== undefined && payload.Period !== null){
      this.Period = new Date(payload.Period);
    }else{
      this.Period = new Date('1800-01-01'); // some invalid date before 1900-01-01
    }
    this.LoanNumber = payload.LoanNumber || '';
    this.Borrower = payload.Borrower || '';
    this.LoanAmount = payload.LoanAmount || 0;
    this.InterestRate = payload.InterestRate || 0;
    this.Margin = payload.Margin || 0;
    if ( payload.Settlement !== undefined && payload.Settlement !== null){
      this.Settlement  = new Date (payload.Settlement);
    }else{
      this.Settlement = new Date('1800-01-01');
    }
    this.Balance = payload.Balance || 0;
    this.OffsetBalance = Math.abs(payload.OffsetBalance || 0);
    this.GrossManFee = payload.GrossManFee || 0;
    this.IncentiveP = payload.IncentiveP || 0;
    this.Months = payload.Months || 0;
    this.SacrificeP = payload.SacrificeP || 0;
    this.Fee = payload.Fee || 0 ;
    this.MoFee = payload.MoFee || 0;
    this.NetManFee = payload.NetManFee || 0;
    this.IncomeAmount = payload.IncomeAmount || 0;

  }

  public convertToPayIn(): PayInModel {
    const pi = new PayInModel({});
    pi.Lender = 'Resimac';
    pi.LoanNumber = this.LoanNumber;
    pi.Amount = this.LoanAmount;
    pi.Settlement = this.Settlement;
    pi.IncomeAmount = this.IncomeAmount;
    pi.IncomeType = 'Trail';
    pi.Balance = this.Balance;
    pi.OffsetBalance = -1;
    pi.Ts = this.Period;
    return pi;
  }

  public isMatch(pi: PayInModel): boolean{
    return  this.LoanNumber === pi.LoanNumber &&  this.LoanNumber !== '' &&
      this.AgencyName === pi.Lender && this.AgencyName !== '' &&
      this.IncomeAmount === pi.IncomeAmount &&
      this.Balance === pi.Balance &&
      'Trail' === pi.IncomeType;
  }
}
