import PaymentsPropertiesTI from './typings-ti'

import { injectEvent } from "../../core/injector"
import {
    DeferredPaymentProperties,
    PaymentMethodProperties,
    PaymentsTypes,
    PaymentMethod
} from './typings'
import { ContentBlock } from '../Navigation/typings'
import CfExceptionHandler from "../../core/CfExceptionHandler";


const moduleName = ContentBlock.Payment

/**
 *
 * @param properties
 * @param {string} userId use this parameter when the logger user is not the same than the user who has created this log
 * @param sendNow
 */
const logPaymentMethodEvent = async (properties: PaymentMethodProperties, userId = '', sendNow = false) => {

    if(properties.id == ''){
        new CfExceptionHandler().throwAndLogError(PaymentsTypes.PaymentMethod, 'empty payment method id. If there is no payment method id then provide payment_<user_id>')
    }else if(properties.action == undefined){
        new CfExceptionHandler().throwAndLogError(PaymentsTypes.PaymentMethod, 'invalid payment method action')
    }else if(properties.order_id == ''){
        new CfExceptionHandler().throwAndLogError(PaymentsTypes.PaymentMethod, 'empty payment method order id')
    }else if(properties.type == undefined){
        new CfExceptionHandler().throwAndLogError(PaymentsTypes.PaymentMethod, 'invalid payment method type')
    }else if(properties.payment_amount < 0){
        new CfExceptionHandler().throwAndLogError(PaymentsTypes.PaymentMethod, 'negative payment method payment amount')
    }else if(properties.currency == undefined){
        new CfExceptionHandler().throwAndLogError(PaymentsTypes.PaymentMethod, 'invalid payment method currency')
    }

    const internalProps = {
        ...properties
    }

    injectEvent(
        internalProps,
        [PaymentsPropertiesTI],
        PaymentsTypes.PaymentMethod,
        moduleName,
        userId,
        sendNow
    )

}

/**
 *
 * @param properties
 * @param {string} userId use this parameter when the logger user is not the same than the user who has created this log
 * @param sendNow
 */
const logDeferredPaymentEvent = async (properties: DeferredPaymentProperties, userId, sendNow = false): Promise<void> => {

    if(properties.id == ''){
        new CfExceptionHandler().throwAndLogError(PaymentsTypes.DeferredPayment, 'empty deferred payment id')
    }else if(properties.order_id == ''){
        new CfExceptionHandler().throwAndLogError(PaymentsTypes.DeferredPayment, 'empty deferred payment order id')
    }else if(properties.type == undefined){
        new CfExceptionHandler().throwAndLogError(PaymentsTypes.DeferredPayment, 'invalid deferred payment type')
    }else if(properties.action == undefined){
        new CfExceptionHandler().throwAndLogError(PaymentsTypes.DeferredPayment, 'invalid deferred payment action')
    }else if(properties.account_balance == undefined){
        new CfExceptionHandler().throwAndLogError(PaymentsTypes.DeferredPayment, 'invalid deferred payment account balance')
    }else if(properties.payment_amount == undefined){
        new CfExceptionHandler().throwAndLogError(PaymentsTypes.DeferredPayment, 'invalid deferred payment amount')
    }else if(properties.currency == undefined){
        new CfExceptionHandler().throwAndLogError(PaymentsTypes.DeferredPayment, 'invalid deferred payment currency')
    }else if(properties.is_successful == undefined){
        new CfExceptionHandler().throwAndLogError(PaymentsTypes.DeferredPayment, 'invalid deferred payment is_successful')
    }

    const internalProps = {
        ...properties,
        type: properties.type ? properties.type : PaymentMethod.BankTransfer
    }

    injectEvent(
        internalProps,
        [PaymentsPropertiesTI],
        PaymentsTypes.DeferredPayment,
        moduleName,
        userId,
        sendNow
    )
}


export default {
    logDeferredPaymentEvent,
    logPaymentMethodEvent,
}
