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

export class PayInPepperModel extends PayInLenderIncomeModel {
  public InvoiceDate: Date;

  // Shared Fields for both upfront and Trailer
  public LoanNumber: string;
  public Customer: string;
  public PrimaryIntroducer: string;
  public NextGenNumber: string;
  public Settlement: Date;
  public NetAmount: number;
  public GST: number;
  public GrossAmount: number;


  // Trailer Only
  public Period: Date;
  public Balance: number;
  public SettlementAmount: number;
  public TrailerMargin: number;


  // Upfront only
  public UpfrontRate: number;

  // Derived
  public IncomeType: string;
  public IncomeAmount: number;

  constructor(payload: Partial<PayInPepperModel>) {
    super('Pepper', payload);
    if ( payload.InvoiceDate !== undefined && payload.InvoiceDate !== null){
      this.InvoiceDate = new Date(payload.InvoiceDate);
    }else{
      this.InvoiceDate = new Date('1800-01-01'); // some invalid date before 1900-01-01
    }
    this.LoanNumber = payload.LoanNumber || '';
    this.Customer = payload.Customer || '';
    this.PrimaryIntroducer = payload.PrimaryIntroducer || '';
    this.NextGenNumber = payload.NextGenNumber || '';

    if ( payload.Settlement !== undefined && payload.Settlement !== null){
      this.Settlement = new Date(payload.Settlement);
    }else{
      this.Settlement = new Date('1800-01-01'); // some invalid date before 1900-01-01
    }

    this.NetAmount = payload.NetAmount || 0;
    this.GST = payload.GST || 0;
    this.GrossAmount = payload.GrossAmount || 0;

    // Trailer only
    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.Balance = payload.Balance || 0;
    this.SettlementAmount = payload.SettlementAmount || 0;
    this.TrailerMargin = payload.TrailerMargin || 0;

    // Upfront only
    this.UpfrontRate = payload.UpfrontRate || 0;

    // Derived
    this.IncomeType = payload.IncomeType || '';
    this.IncomeAmount = payload.IncomeAmount || 0;
  }

  public convertToPayIn(): PayInModel {
    const pi = new PayInModel({});
    pi.Lender = 'Pepper';
    pi.LoanNumber = this.LoanNumber;
    pi.Amount = this.SettlementAmount;
    pi.Settlement = this.Settlement;
    pi.IncomeAmount = this.IncomeAmount;
    pi.IncomeType = this.IncomeType;
    pi.Balance = this.Balance;
    pi.OffsetBalance = -1;
    if ( this.IncomeType.toLowerCase() === 'upfront' ){
      pi.Ts = this.InvoiceDate;
    }else{
      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 &&
      this.IncomeType.toLowerCase() === pi.IncomeType.toLowerCase();
  }
}
