import { Injectable } from '@angular/core';
import { ConfigService } from '@shared/config/config.service';
import { FleioApiService } from '../../fleio-api.service';
import { HttpClient } from '@angular/common/http';
import { IGatewayModel } from '../model/gateway.model';
import { Observable } from 'rxjs';
import {
  IRecurringPaymentOptionModelResponse
} from '@fleio-api/billing/model/recurring-payment-option.model';
import { FleioId } from '@fleio-api/base-model/base-fleio-object.model';


@Injectable({
  providedIn: 'root'
})
export class GatewaysApiService extends FleioApiService<IGatewayModel> {
  constructor(protected httpClient: HttpClient, protected config: ConfigService) {
    super();
    this.setEndpoint(config.getPanelApiUrl('billing/gateways'));
  }

  getRecurringPaymentOptions(): Observable<IRecurringPaymentOptionModelResponse> {
    return this.getAction('get_configured_recurring_payment_options');
  }

  removeCard(gatewayID: FleioId, gatewayDataID: FleioId): Observable<any> {
    return this.objectPostAction(gatewayID, 'remove_card', {
      gateway_data_id: gatewayDataID,
    });
  }

  updateCard(gatewayID: FleioId, gatewayDataID: FleioId, expMonth, expYear): Observable<any> {
    return this.objectPostAction( gatewayID,'update_card', {
      gateway_data_id: gatewayDataID,
      exp_month: expMonth,
      exp_year: expYear,
    });
  }

  deactivateRecurringPaymentOption(gatewayID: FleioId, gatewayDataID: FleioId): Observable<any> {
    return this.objectPostAction(gatewayID, 'deactivate_recurring_payment_option', {
      gateway_data_id: gatewayDataID,
    });
  }

  reactivateRecurringPaymentOption(gatewayID: FleioId, gatewayDataID: FleioId): Observable<any> {
    return this.objectPostAction(gatewayID,'reactivate_recurring_payment_option', {
      gateway_data_id: gatewayDataID,
    });
  }

  changeRecurringPaymentOptionOrder(gatewayID: FleioId, gatewayDataID: FleioId, newOrder: number): Observable<any> {
    return this.objectPostAction(gatewayID, 'change_recurrent_payments_order', {
      gateway_data_id: gatewayDataID,
      new_ordering: newOrder
    });
  }

}
