/*
    Exchange
    
    currencyRateData    - массив данных по обмену валют
*/
function Exchange(pmCommissionData, currencyRateData){
    this.currencyRateData = currencyRateData;
    this.pmCommissionData = pmCommissionData;
}

/**
* Создает заявку на обмен
* @param decimal amountIn Сумма на входе
* @param ExchangeWay exchangeWay ID направление обмена
* @return array [amount_in: "", amount_out: "", system_commission: "", rate: "", from_pm_commission: "", out_pm_commission: ""]
*/                         
Exchange.prototype.getCurrencyRate = function(currencyFromId, currencyToId){
    return this.currencyRateData[currencyFromId][currencyToId];
    
}

/**
* Создает заявку на обмен
* @param decimal amountIn Сумма на входе
* @param ExchangeWay exchangeWay ID направление обмена
* @return array [amount_in: "", amount_out: "", system_commission: "", rate: "", in_pm_commission: "", out_pm_commission: ""]
*/                         
Exchange.prototype.calcExchangeIn = function(amountIn, exchangeWay, countriesGroupId, isAmountOutToPay){        
    var amountInInit;
    var inPmCommission = 0;   
    var systemCommission = 0;
    var rate = 1;
    var amountOut = 0;
    var outPmCommission = 0;       
    
    if(isAmountOutToPay == undefined){
        isAmountOutToPay = false;
    }    
        
    // check bound restriction   
    if(amountIn < exchangeWay.minAmount){                
        this.error = 'Минимальная сумма входного платежа ' + exchangeWay.minAmount + ' ' + exchangeWay.inPmMethodObj.currencySymbol;
        return false;
    }                                                               
    
    if( amountIn > exchangeWay.maxAmount){        
        this.error = 'Максимальная сумма входного платежа ' + exchangeWay.maxAmount + ' ' + exchangeWay.inPmMethodObj.currencySymbol;        
        return false;
    }   
    
    amountInInit = amountIn;                                                                
    
    if(exchangeWay.inPmMethodObj.whoPayPsCommission == 'payee'){
        inPmCommission = exchangeWay.inPmMethodObj.calcCommissionIn2Out(amountIn, this.pmCommissionData, countriesGroupId);
        if(inPmCommission == false){
            this.error = 'Комиссия не может быть рассчитана. Введите другую сумму.';
            return false;
        }
        amountIn = amountIn - inPmCommission;
    }
    
    systemCommission = exchangeWay.calcCommission(amountIn);
    
    // получаем currencyRate из массива
    rate = this.getCurrencyRate(exchangeWay.inPmMethodObj.currencyId, exchangeWay.outPmMethodObj.currencyId);
    //rate = 1;
    
    amountOut = (amountIn - systemCommission) * rate;    
    
    if(exchangeWay.outPmMethodObj.whoPayPsCommission == 'payer'){        
        if(isAmountOutToPay == false){
            outPmCommission = exchangeWay.outPmMethodObj.calcCommissionIn2Out(amountOut, this.pmCommissionData, countriesGroupId);                                    
            if(outPmCommission == false){
                this.error = 'Комиссия не может быть рассчитана. Введите другую сумму.';
                return false;
            }
            amountOut = amountOut - outPmCommission;            
        }
    }        

    
    result = {
        amount_in:          parseFloat(amountInInit),
        amount_out:         parseFloat(amountOut),
        system_commission:  parseFloat(systemCommission),
        rate:               parseFloat(rate),
        in_pm_commission:   parseFloat(inPmCommission),
        out_pm_commission:  parseFloat(outPmCommission)               
    };
    
    return result;    
};

/**
* Создает заявку на обмен
* @param decimal amountIn Сумма на входе
* @param ExchangeWay exchangeWay ID направление обмена
* @return array [amount_in: "", amount_out: "", system_commission: "", rate: "", in_pm_commission: "", out_pm_commission: ""]
*/                         
Exchange.prototype.calcExchangeOut = function(amountOut, exchangeWay, countriesGroupId, isAmountOutToPay){                   
    var amountOutInit = amountOut;
    var outPmCommission = 0;
    var rate = 1;
    var amountIn = 0;
    var systemCommission = 0;        
    var inPmCommission = 0;    
    
    if(isAmountOutToPay == undefined){
        isAmountOutToPay = false;
    }    
    
    if(exchangeWay.outPmMethodObj.whoPayPsCommission == 'payer'){        
        if(isAmountOutToPay == false){
            outPmCommission = exchangeWay.outPmMethodObj.calcCommissionOut2In(amountOut, this.pmCommissionData, countriesGroupId);
            if(outPmCommission == false){
                this.error = 'Комиссия не может быть рассчитана. Введите другую сумму.';
                return false;                
            }
            amountOut = amountOut + outPmCommission;            
        } else{
            outPmCommission = exchangeWay.outPmMethodObj.calcCommissionIn2Out(amountOut, this.pmCommissionData, countriesGroupId);            
        }
    }     
    
    rate = this.getCurrencyRate(exchangeWay.outPmMethodObj.currencyId, exchangeWay.inPmMethodObj.currencyId);
    //rate = 1;
    
    amountIn = amountOut * rate;
    
    systemCommission = exchangeWay.calcCommission(amountIn, true);    
    
    amountIn = amountIn + systemCommission;
        
    if(exchangeWay.inPmMethodObj.whoPayPsCommission == 'payee'){
        inPmCommission = exchangeWay.inPmMethodObj.calcCommissionOut2In(amountIn, this.pmCommissionData, countriesGroupId);
        if(inPmCommission == false){
            this.error = 'Комиссия не может быть рассчитана. Введите другую сумму.';
            return false;
        }            
        amountIn = amountIn + inPmCommission;
    }
    
    // check bound restriction
    if(amountIn < exchangeWay.minAmount || amountIn > exchangeWay.maxAmount){        
        if(amountIn < exchangeWay.minAmount){
            this.error = 'Минимальная сумма входного платежа ' + exchangeWay.minAmount + ' ' + exchangeWay.inPmMethodObj.currencySymbol;
            return false;
        }
        if(amountIn > exchangeWay.maxAmount){
            this.error = 'Максимальная сумма входного платежа ' + exchangeWay.maxAmount + ' ' + exchangeWay.inPmMethodObj.currencySymbol;        
            return false;
        }               
        return this.calcExchangeIn(amountIn, exchangeWay);
    }                                                               
        
    result = {
        amount_in:          parseFloat(amountIn),
        amount_out:         parseFloat(amountOutInit),
        system_commission:  parseFloat(systemCommission),
        rate:               parseFloat(rate),
        in_pm_commission:   parseFloat(inPmCommission),
        out_pm_commission:  parseFloat(outPmCommission)               
    };
    
    return result;           
};