import {LoanModel} from './loan.model';
import {PayOutExModel} from './payout.ex.model';
import {UserExModel} from './user-ex.model';

export class RewardExModel {
  public Id: number;
  public Ts: Date;
  public ToId: string;
  public Amount: number;
  public Description: string;
  public LoanId: string;
  public FromId: string;
  public PayOutId: number;

  public Beneficiary?: UserExModel;
  public Loan?: LoanModel;
  public From?: UserExModel;

  // derived content, for compatible with reward-by-user purpose
  public ToFirst: string;
  public ToMiddle: string;
  public ToLast: string;
  public ToTitle: string;
  public ToDisplay: string;
  public ToNick: string;

  public FromFirst: string;
  public FromMiddle: string;
  public FromLast: string;
  public FromTitle: string;
  public FromDisplay: string;
  public FromNick: string;

  // Loan related derived property
  public Item: string;
  public Status: string;
  public Settlement: Date;

  constructor(payload?: Partial<RewardExModel>) {
    if ( !payload ) { payload = {}; }

    this.Id = payload.Id || 0;
    if ( payload.Ts ) {
      this.Ts = new Date(payload.Ts);
    }else{
      this.Ts = new Date('1800-01-01');
    }
    this.ToId = payload.ToId || '';
    this.Amount = payload.Amount || 0;
    this.Description = payload.Description || '';
    this.LoanId = payload.LoanId || '';
    this.FromId = payload.FromId || '';
    this.PayOutId = payload.PayOutId || 0;

    // derived content
    // this.ToFirst = payload.ToFirst || '';
    // this.ToMiddle = payload.ToMiddle || '';
    // this.ToLast = payload.ToLast || '';
    // this.ToTitle = payload.ToTitle || '';
    // this.ToDisplay = payload.ToDisplay || '';
    // this.ToNick = payload.ToNick || '';
    // this.Item = payload.Item || '';
    // this.Status = payload.Status || '';
    // this.Settlement = new Date(payload.Settlement);

    if (payload.Beneficiary) {
      this.Beneficiary = new UserExModel(payload.Beneficiary);
      this.ToFirst = this.Beneficiary.First || '';
      this.ToMiddle = this.Beneficiary.Middle || '';
      this.ToLast = this.Beneficiary.Last || '';
      this.ToTitle = this.Beneficiary.Title || '';
      this.ToDisplay = this.Beneficiary.Display || '';
      this.ToNick = this.Beneficiary.Nick || '';
    }

    if (payload.Loan) {
      this.Loan = new LoanModel(payload.Loan);
      this.Item = this.Loan.Item || '';
      this.Status = this.Loan.Status || '';
      if ( this.Loan.Settlement ){
        this.Settlement = new Date(payload.Settlement);
      }else{
        this.Settlement = new Date('1900-01-01');
      }
    }

    if ( payload.From) {
      this.From = new UserExModel(payload.From);
      this.FromFirst = this.From.First || '';
      this.FromMiddle = this.From.Middle || '';
      this.FromLast = this.From.Last || '';
      this.FromTitle = this.From.Title || '';
      this.FromDisplay = this.From.Display || '';
      this.FromNick = this.From.Nick || '';
    }

  }

  get Paid(): boolean{
    return this.PayOutId > 0 ;
  }

  public Equal(v: RewardExModel): boolean{
      if ( this.Id !== v.Id) { return false; }
      if ( this.Ts !== v.Ts ) { return false; }
      if ( this.ToId !== v.ToId)  { return false; }
      if ( this.Amount !== v.Amount ) { return false; }
      if ( this.Description !== v.Description ) { return false; }
      if ( this.LoanId !== v.LoanId ) { return false; }
      if ( this.FromId !== v.FromId ) { return false; }
      if ( this.PayOutId !== v.PayOutId ) { return false; }
      return true;
  }
}
