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

export class PayInResimacXlsModel extends PayInLenderIncomeModel {
  public TRSTCD: number;
  public ORGNTR: number;
  public LOANNO: number;
  public PORTN: string;
  public BRNAMX: string;  // Brower
  public LNAMT: number; // Loan amount
  public INTRTE: number;
  public DELRTE: number;
  public MARGIN: number;
  public SETLDX: Date; // settled date
  public LNBAL: number;
  public MANFEE: number;
  public RSINCP: number;
  public RSSACT: number;
  public RSSACP: number;
  public SACAMT: number;
  public MOFEE: number;
  public MANTOT: number;
  public NEWLON: number;
  public REFADJ: number;
  public NETNEW: number;
  public FIXED: string;
  public LNFORT: string;
  public ORGNAM: string;
  public LNEOM: Date;

  // Derived attributes
  LoanNumber: string;
  Borrower: string;
  LoanAmount: number;
  Balance: number;
  IncomeAmount: number;


  constructor(payload: Partial<PayInResimacXlsModel>){
    super('Resimac', payload);
    this.TRSTCD = payload.TRSTCD || 0 ;
    this.ORGNTR = payload.ORGNTR || 0 ;
    this.LOANNO = payload.LOANNO || 0 ;
    this.PORTN  = payload.PORTN || '' ;
    this.BRNAMX = payload.BRNAMX || '';
    this.LNAMT  = payload.LNAMT || 0;
    this.INTRTE = payload.INTRTE || 0;
    this.DELRTE = payload.DELRTE || 0;
    this.MARGIN = payload.MARGIN || 0;
    if ( payload.SETLDX !== undefined && payload.SETLDX !== null){
      this.SETLDX = new Date(payload.SETLDX);
    }else{
      this.SETLDX = new Date('1800-01-01'); // some invalid date before 1900-01-01
    }
    this.LNBAL = payload.LNBAL || 0;
    this.MANFEE = payload.MANFEE || 0;
    this.RSINCP = payload.RSINCP || 0;
    this.RSSACT = payload.RSSACT || 0;
    this.RSSACP = payload.RSSACP || 0;
    this.SACAMT = payload.SACAMT || 0;
    this.MOFEE  = payload.MOFEE || 0;
    this.MANTOT = payload.MANTOT || 0;
    this.NEWLON = payload.NEWLON || 0;
    this.REFADJ = payload.REFADJ || 0;
    this.NETNEW = payload.NETNEW || 0;
    this.FIXED  = payload.FIXED || '';
    this.LNFORT = payload.LNFORT || '';
    this.ORGNAM = payload.ORGNAM || '';
    if ( payload.LNEOM !== undefined && payload.LNEOM !== null){
      this.LNEOM  = new Date (payload.LNEOM);
    }else{
      this.LNEOM = new Date('1800-01-01');
    }


    // Derived attributes, but provided by Server
    this.LoanNumber = payload.LoanNumber || '';
    this.Borrower = payload.Borrower || '';
    this.LoanAmount = payload.LoanAmount || 0;
    this.Balance = payload.Balance || 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.SETLDX;
    pi.IncomeAmount = this.IncomeAmount;
    pi.IncomeType = 'Trail';
    pi.Balance = this.Balance;
    pi.OffsetBalance = -1;
    pi.Ts = this.LNEOM;
    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;
  }
}
