import {UserExModel} from './userExModel';
import {PayoutModel} from './payout.model';
import {RewardExModel} from './reward-ex.model';

export class PayOutExModel{
    Id: number;
    Ts: Date;
    To: UserExModel;
    Prepared: UserExModel;
    Approved: UserExModel;
    Paid: UserExModel;
    PayDate: Date;
    PayAmount: number;
    Status: string;
    Rewards?: RewardExModel[];
    constructor( payload?: Partial<PayOutExModel>) {
      if (! payload ) { payload = {}; }
      this.Id = payload.Id || 0;
      if (payload.Ts){
        this.Ts = new Date( payload.Ts);
      }else{
        this.Ts = new Date();
      }

      this.To = new UserExModel(payload.To);
      this.Prepared = new UserExModel(payload.Prepared);
      this.Approved = new UserExModel(payload.Approved);
      this.Paid = new UserExModel(payload.Paid);

      if (payload.PayDate) {
        this.PayDate = new Date(payload.PayDate);
      }else{
        this.PayDate = new Date('1800-01-01');
      }
      this.PayAmount = payload.PayAmount || 0;
      this.Status = payload.Status || '';

      this.Rewards = [];
      if ( payload.Rewards && payload.Rewards.length){
        payload.Rewards.forEach( v => this.Rewards.push( new RewardExModel(v)  ) );
      }
    }

    public toPayOutModel(): PayoutModel {
      const ret = new PayoutModel();
      ret.Id = this.Id;
      ret.Ts = this.Ts;
      ret.To = this.To.Id;
      ret.Prepared = this.Prepared.Id;
      ret.Approved = this.Approved.Id;
      ret.Paid = this.Paid.Id;
      ret.PayDate = this.PayDate;
      ret.PayAmount = this.PayAmount;
      ret.Status = this.Status;
      ret.RewardIds = this.Rewards.map( v => v.Id);
      return ret;
    }
}
