import { getLatestABI } from './AbiService';
import Promisify from '../utils/Promisify';
import Services from '../Services';

export class WithdrawService extends Services {
  private address: string;
  private _contract;
  private abi;

  constructor(address: string, abi?: any) {
    super();
    this.address = address;
    this.abi = abi;
  }

  public async contract(): Promise<any> {
    this._contract = await this.rpcService.contract(this.abi || await getLatestABI('WITHDRAW_ABI'), this.address);
    return this._contract;
  }

  public async needToWithdraw(productAddress: string, withdrawAddress: string) {
    this._contract = this._contract ||
      await this.rpcService.contract(this.abi || await getLatestABI('WITHDRAW_ABI'), this.address);
    const result = await Promisify(async (cb) =>
      await this._contract(withdrawAddress).getContractInfo(productAddress, cb));
    // The value of index 2 is the totalWithdrawAmount
    return result[2].toNumber() !== 0;
  }
}
