/* TA-LIB Copyright (c) 1999-2025, Mario Fortier
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * - Neither name of author nor the names of its contributors
 *   may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* Important:
 *  This file is automatically generated by the utility ta_codegen.
 *  Any modifications will be lost on next execution of ta_codegen.
 *
 *  Modifications should instead be done with the "C" source file
 *  in ta-lib\src\ta_func
 */

/* List of contributors:
 *
 *  Initial  Name/description
 *  -------------------------------------------------------------------
 *  MF       Mario Fortier
 *
 *
 * Change history:
 *
 *  MMDDYY BY   Description
 *  -------------------------------------------------------------------
 *  112400 MF   Template creation.
 *  052603 MF   Adapt code to compile with .NET Managed C++
 *  062804 MF   Resolve div by zero bug on limit case.
 */

// Import types from parent module
use super::*;

// Allow non-snake-case names to maintain TA-Lib API compatibility
#[allow(non_snake_case)]
#[allow(unused_variables)]
#[allow(dead_code)]
#[allow(unused_mut)]
#[allow(unused_assignments)]
impl Core {
    /// Lookback period for [`Core::rsi`]: the number of leading input values consumed before the
    /// first output value can be produced.
    ///
    /// # Arguments
    ///
    /// * `optInTimePeriod` — Lookback for the gain/loss averaging (default 14, range 2..=100000)
    ///
    /// Returns `usize::MAX` when a parameter is out of range. Integer parameters accept `i32::MIN`
    /// to select their default value.
    #[inline]
    pub fn rsi_lookback(&self, mut optInTimePeriod: i32) -> usize {
        if ((optInTimePeriod) as i32) == (i32::MIN) {
            optInTimePeriod = 14;
        } else if (((optInTimePeriod) as i32) < 2) || (((optInTimePeriod) as i32) > 100000) {
            return usize::MAX;
        }
        let mut retValue: usize = 0_usize;
        retValue = (optInTimePeriod + self.unstable_period[FuncUnstId::Rsi as usize]) as usize;
        if self.compatibility == Compatibility::Metastock {
            retValue = retValue - 1;
        }
        return retValue;
    }
    /// Wilder's Relative Strength Index, a momentum oscillator bounded 0-100 from the ratio of
    /// average gains to average losses over the period. Used to gauge overbought/oversold
    /// conditions. >70 overbought, \<30 oversold.
    ///
    /// # Formula
    ///
    /// ```text
    /// Initial avgGain/avgLoss = simple mean of up/down moves over the period, then Wilder-smoothed each bar: $avg = (avg_{prev}\cdot(period-1) + move)/period$. $RSI = 100\cdot avgGain/(avgGain+avgLoss)$ (equivalent to $100 - 100/(1+RS)$).
    /// ```
    ///
    /// # Notes
    ///
    /// * In Metastock-compatibility mode an extra initial value is emitted, treating the first bar
    ///   as having no gain or loss.
    ///
    /// # Arguments
    ///
    /// * `startIdx` — Start index of the requested calculation range.
    /// * `endIdx` — End index of the requested calculation range (inclusive).
    /// * `inReal` — Price series (typically close)
    /// * `optInTimePeriod` — Lookback for the gain/loss averaging (default 14, range 2..=100000)
    /// * `outBegIdx` — Set to the input index of the first output value.
    /// * `outNBElement` — Set to the number of output values written.
    /// * `outReal` — RSI value.
    ///
    /// Integer parameters accept `i32::MIN` to select their default value.
    ///
    /// # Errors
    ///
    /// Returns [`RetCode::OutOfRangeStartIndex`] when `endIdx < startIdx`, and
    /// [`RetCode::BadParam`] when an optional parameter is outside its documented range.
    ///
    /// # Panics
    ///
    /// Input slices must cover `startIdx..=endIdx` and output slices must hold the number of values
    /// produced for that range: undersized slices panic or, for functions that forward to unchecked
    /// internals, cause undefined behavior. Sizing every output slice to the input length is always
    /// sufficient.
    ///
    /// # Examples
    ///
    /// ```
    /// use ta_lib::{Core, RetCode};
    ///
    /// let data: Vec<f64> = (0..252).map(|i| 100.0 + 10.0 * (0.1 * i as f64).sin()).collect();
    ///
    /// let core = Core::new();
    /// let mut out_beg = 0;
    /// let mut out_nb = 0;
    /// let mut out = vec![0.0; 252];
    ///
    /// let ret = core.rsi(0, data.len() - 1, &data, 14, &mut out_beg, &mut out_nb, &mut out);
    /// assert_eq!(ret, RetCode::Success);
    /// assert!(out_nb > 0);
    /// ```
    ///
    /// # See also
    ///
    /// [`Core::cmo`] · [`Core::stochrsi`]
    ///
    /// # References
    ///
    /// * J. Welles Wilder, *New Concepts in Technical Trading Systems*, Trend Research (ISBN
    ///   0894590278)
    ///
    /// Further reading: [ta-lib.org/functions/rsi](https://ta-lib.org/functions/rsi/)
    #[doc(alias = "relativestrengthindex")]
    pub fn rsi(
        &self,
        startIdx: usize,
        endIdx: usize,
        inReal: &[f64],
        mut optInTimePeriod: i32,
        outBegIdx: &mut usize,
        outNBElement: &mut usize,
        outReal: &mut [f64],
    ) -> RetCode {
        if endIdx < startIdx {
            return RetCode::OutOfRangeStartIndex;
        }
        if ((optInTimePeriod) as i32) == (i32::MIN) {
            optInTimePeriod = 14;
        } else if (((optInTimePeriod) as i32) < 2) || (((optInTimePeriod) as i32) > 100000) {
            return RetCode::BadParam;
        }
        let mut startIdx = startIdx;
        let mut outIdx: usize = 0_usize;
        let mut today: usize = 0_usize;
        let mut lookbackTotal: usize = 0_usize;
        let mut unstablePeriod: usize = 0_usize;
        let mut i: usize = 0_usize;
        let mut prevGain: f64 = 0.0_f64;
        let mut prevLoss: f64 = 0.0_f64;
        let mut prevValue: f64 = 0.0_f64;
        let mut savePrevValue: f64 = 0.0_f64;
        let mut tempValue1: f64 = 0.0_f64;
        let mut tempValue2: f64 = 0.0_f64;
        // The following algorithm is base on the original
        // work from Wilder's and shall represent the
        // original idea behind the classic RSI.
        //
        // Metastock is starting the calculation one price
        // bar earlier. To make this possible, they assume
        // that the very first bar will be identical to the
        // previous one (no gain or loss).
        // If changing this function, please check also CMO
        // which is mostly identical (just different in one step
        // of calculation).
        (*outBegIdx) = 0;
        (*outNBElement) = 0;
        // Adjust startIdx to account for the lookback period.
        lookbackTotal = (self.rsi_lookback(optInTimePeriod) as usize) as usize;
        if startIdx < lookbackTotal {
            startIdx = lookbackTotal;
        }
        // Make sure there is still something to evaluate.
        if startIdx > endIdx {
            return RetCode::Success;
        }
        outIdx = 0;
        // Index into the output.
        // Trap special case where the period is '1'.
        // In that case, just copy the input into the
        // output for the requested range (as-is !)
        if optInTimePeriod == 1 {
            (*outBegIdx) = startIdx;
            i = ((endIdx - startIdx + 1) as usize) as usize;
            (*outNBElement) = i as usize;
            {
            let _n = (i * 1) as usize;
            let _di = (0) as usize;
            let _si = (startIdx) as usize;
            outReal[_di.._di + _n].copy_from_slice(&inReal[_si.._si + _n]);
        };
            return RetCode::Success;
        }
        // Accumulate Wilder's "Average Gain" and "Average Loss"
        // among the initial period.
        today = startIdx - lookbackTotal;
        prevValue = inReal[today] as f64;
        unstablePeriod = (self.unstable_period[FuncUnstId::Rsi as usize]) as usize;
        // If there is no unstable period,
        // calculate the 'additional' initial
        // price bar who is particuliar to
        // metastock.
        // If there is an unstable period,
        // no need to calculate since this
        // first value will be surely skip.
        if unstablePeriod == 0 && self.compatibility == Compatibility::Metastock {
            // Preserve prevValue because it may get
            // overwritten by the output.
            // (because output ptr could be the same as input ptr).
            savePrevValue = prevValue;
            // No unstable period, so must calculate first output
            // particular to Metastock.
            // (Metastock re-use the first price bar, so there
            //  is no loss/gain at first. Beats me why they
            //  are doing all this).
            prevGain = 0.0;
            prevLoss = 0.0;
            // for( i = (optInTimePeriod) as usize; i > 0; i -= 1 )
            i = (optInTimePeriod) as usize;
            while i > 0 {
                tempValue1 = inReal[today] as f64;
                today = today + 1;
                tempValue2 = tempValue1 - prevValue;
                prevValue = tempValue1;
                if tempValue2 < 0.0 {
                    prevLoss -= tempValue2;
                } else {
                    prevGain += tempValue2;
                }
                i -= 1;
            }
            tempValue1 = prevLoss / (optInTimePeriod as f64);
            tempValue2 = prevGain / (optInTimePeriod as f64);
            // Write the output.
            tempValue1 = tempValue2 + tempValue1;
            if !((tempValue1).abs() < 1e-14) {
                outReal[outIdx] = 100.0 * (tempValue2 / tempValue1);
                outIdx = outIdx + 1;
            } else {
                outReal[outIdx] = 0.0;
                outIdx = outIdx + 1;
            }
            // Are we done?
            if today > endIdx {
                (*outBegIdx) = startIdx;
                (*outNBElement) = outIdx;
                return RetCode::Success;
            }
            // Start over for the next price bar.
            today = today - (optInTimePeriod as usize);
            prevValue = savePrevValue;
        }
        // Remaining of the processing is identical
        // for both Classic calculation and Metastock.
        prevGain = 0.0;
        prevLoss = 0.0;
        today = today + 1;
        // for( i = (optInTimePeriod) as usize; i > 0; i -= 1 )
        i = (optInTimePeriod) as usize;
        while i > 0 {
            tempValue1 = inReal[today] as f64;
            today = today + 1;
            tempValue2 = tempValue1 - prevValue;
            prevValue = tempValue1;
            if tempValue2 < 0.0 {
                prevLoss -= tempValue2;
            } else {
                prevGain += tempValue2;
            }
            i -= 1;
        }
        // Subsequent prevLoss and prevGain are smoothed
        // using the previous values (Wilder's approach).
        //  1) Multiply the previous by 'period-1'.
        //  2) Add today value.
        //  3) Divide by 'period'.
        prevLoss /= optInTimePeriod as f64;
        prevGain /= optInTimePeriod as f64;
        // Often documentation present the RSI calculation as follow:
        //    RSI = 100 - (100 / 1 + (prevGain/prevLoss))
        //
        // The following is equivalent:
        //    RSI = 100 * (prevGain/(prevGain+prevLoss))
        //
        // The second equation is used here for speed optimization.
        if today > startIdx {
            tempValue1 = prevGain + prevLoss;
            if !((tempValue1).abs() < 1e-14) {
                outReal[outIdx] = 100.0 * (prevGain / tempValue1);
                outIdx = outIdx + 1;
            } else {
                outReal[outIdx] = 0.0;
                outIdx = outIdx + 1;
            }
        } else {
            // Skip the unstable period. Do the processing
            // but do not write it in the output.
            while today < startIdx {
                tempValue1 = inReal[today] as f64;
                tempValue2 = tempValue1 - prevValue;
                prevValue = tempValue1;
                prevLoss *= (optInTimePeriod - 1) as f64;
                prevGain *= (optInTimePeriod - 1) as f64;
                if tempValue2 < 0.0 {
                    prevLoss -= tempValue2;
                } else {
                    prevGain += tempValue2;
                }
                prevLoss /= optInTimePeriod as f64;
                prevGain /= optInTimePeriod as f64;
                today = today + 1;
            }
        }
        // Unstable period skipped... now continue
        // processing if needed.
        while today <= endIdx {
            tempValue1 = inReal[today] as f64;
            today = today + 1;
            tempValue2 = tempValue1 - prevValue;
            prevValue = tempValue1;
            prevLoss *= (optInTimePeriod - 1) as f64;
            prevGain *= (optInTimePeriod - 1) as f64;
            if tempValue2 < 0.0 {
                prevLoss -= tempValue2;
            } else {
                prevGain += tempValue2;
            }
            prevLoss /= optInTimePeriod as f64;
            prevGain /= optInTimePeriod as f64;
            tempValue1 = prevGain + prevLoss;
            if !((tempValue1).abs() < 1e-14) {
                outReal[outIdx] = 100.0 * (prevGain / tempValue1);
                outIdx = outIdx + 1;
            } else {
                outReal[outIdx] = 0.0;
                outIdx = outIdx + 1;
            }
        }
        (*outBegIdx) = startIdx;
        (*outNBElement) = outIdx;
        return RetCode::Success;
    }
    /// Unchecked variant of [`Core::rsi`], used for internal cross-indicator calls.
    ///
    /// Skips parameter validation and uses unchecked indexing internally. Every argument must
    /// satisfy the constraints documented on [`Core::rsi`]; an out-of-range parameter, an input
    /// slice not covering `startIdx..=endIdx`, or an undersized output slice may panic or cause
    /// undefined behavior. Prefer [`Core::rsi`].
    #[inline]
    pub fn rsi_unguarded(
        &self,
        mut startIdx: usize,
        endIdx: usize,
        inReal: &[f64],
        mut optInTimePeriod: i32,
        outBegIdx: &mut usize,
        outNBElement: &mut usize,
        outReal: &mut [f64],
    ) -> RetCode {
        let mut outIdx: usize = 0_usize;
        let mut today: usize = 0_usize;
        let mut lookbackTotal: usize = 0_usize;
        let mut unstablePeriod: usize = 0_usize;
        let mut i: usize = 0_usize;
        let mut prevGain: f64 = 0.0_f64;
        let mut prevLoss: f64 = 0.0_f64;
        let mut prevValue: f64 = 0.0_f64;
        let mut savePrevValue: f64 = 0.0_f64;
        let mut tempValue1: f64 = 0.0_f64;
        let mut tempValue2: f64 = 0.0_f64;
        unsafe {
        assert!(endIdx < inReal.len());
        let _assertLb = self.rsi_lookback(optInTimePeriod);
        let _assertStart = if startIdx > _assertLb { startIdx } else { _assertLb };
        assert!(_assertStart > endIdx || endIdx - _assertStart < outReal.len());
        (*outBegIdx) = 0;
        (*outNBElement) = 0;
        lookbackTotal = (self.rsi_lookback(optInTimePeriod) as usize) as usize;
        if startIdx < lookbackTotal {
            startIdx = lookbackTotal;
        }
        if startIdx > endIdx {
            return RetCode::Success;
        }
        outIdx = 0;
        if optInTimePeriod == 1 {
            (*outBegIdx) = startIdx;
            i = ((endIdx - startIdx + 1) as usize) as usize;
            (*outNBElement) = i as usize;
            {
            let _n = (i * 1) as usize;
            let _di = (0) as usize;
            let _si = (startIdx) as usize;
            outReal[_di.._di + _n].copy_from_slice(&inReal[_si.._si + _n]);
        };
            return RetCode::Success;
        }
        today = startIdx - lookbackTotal;
        prevValue = *inReal.as_ptr().add(today) as f64;
        unstablePeriod = (self.unstable_period[FuncUnstId::Rsi as usize]) as usize;
        if unstablePeriod == 0 && self.compatibility == Compatibility::Metastock {
            savePrevValue = prevValue;
            prevGain = 0.0;
            prevLoss = 0.0;
            // for( i = (optInTimePeriod) as usize; i > 0; i -= 1 )
            i = (optInTimePeriod) as usize;
            while i > 0 {
                tempValue1 = *inReal.as_ptr().add(today) as f64;
                today = today + 1;
                tempValue2 = tempValue1 - prevValue;
                prevValue = tempValue1;
                if tempValue2 < 0.0 {
                    prevLoss -= tempValue2;
                } else {
                    prevGain += tempValue2;
                }
                i -= 1;
            }
            tempValue1 = prevLoss / (optInTimePeriod as f64);
            tempValue2 = prevGain / (optInTimePeriod as f64);
            tempValue1 = tempValue2 + tempValue1;
            if !((tempValue1).abs() < 1e-14) {
                *outReal.as_mut_ptr().add(outIdx) = 100.0 * (tempValue2 / tempValue1);
                outIdx = outIdx + 1;
            } else {
                *outReal.as_mut_ptr().add(outIdx) = 0.0;
                outIdx = outIdx + 1;
            }
            if today > endIdx {
                (*outBegIdx) = startIdx;
                (*outNBElement) = outIdx;
                return RetCode::Success;
            }
            today = today - (optInTimePeriod as usize);
            prevValue = savePrevValue;
        }
        prevGain = 0.0;
        prevLoss = 0.0;
        today = today + 1;
        // for( i = (optInTimePeriod) as usize; i > 0; i -= 1 )
        i = (optInTimePeriod) as usize;
        while i > 0 {
            tempValue1 = *inReal.as_ptr().add(today) as f64;
            today = today + 1;
            tempValue2 = tempValue1 - prevValue;
            prevValue = tempValue1;
            if tempValue2 < 0.0 {
                prevLoss -= tempValue2;
            } else {
                prevGain += tempValue2;
            }
            i -= 1;
        }
        prevLoss /= optInTimePeriod as f64;
        prevGain /= optInTimePeriod as f64;
        if today > startIdx {
            tempValue1 = prevGain + prevLoss;
            if !((tempValue1).abs() < 1e-14) {
                *outReal.as_mut_ptr().add(outIdx) = 100.0 * (prevGain / tempValue1);
                outIdx = outIdx + 1;
            } else {
                *outReal.as_mut_ptr().add(outIdx) = 0.0;
                outIdx = outIdx + 1;
            }
        } else {
            while today < startIdx {
                tempValue1 = *inReal.as_ptr().add(today) as f64;
                tempValue2 = tempValue1 - prevValue;
                prevValue = tempValue1;
                prevLoss *= (optInTimePeriod - 1) as f64;
                prevGain *= (optInTimePeriod - 1) as f64;
                if tempValue2 < 0.0 {
                    prevLoss -= tempValue2;
                } else {
                    prevGain += tempValue2;
                }
                prevLoss /= optInTimePeriod as f64;
                prevGain /= optInTimePeriod as f64;
                today = today + 1;
            }
        }
        while today <= endIdx {
            tempValue1 = *inReal.as_ptr().add(today) as f64;
            today = today + 1;
            tempValue2 = tempValue1 - prevValue;
            prevValue = tempValue1;
            prevLoss *= (optInTimePeriod - 1) as f64;
            prevGain *= (optInTimePeriod - 1) as f64;
            if tempValue2 < 0.0 {
                prevLoss -= tempValue2;
            } else {
                prevGain += tempValue2;
            }
            prevLoss /= optInTimePeriod as f64;
            prevGain /= optInTimePeriod as f64;
            tempValue1 = prevGain + prevLoss;
            if !((tempValue1).abs() < 1e-14) {
                *outReal.as_mut_ptr().add(outIdx) = 100.0 * (prevGain / tempValue1);
                outIdx = outIdx + 1;
            } else {
                *outReal.as_mut_ptr().add(outIdx) = 0.0;
                outIdx = outIdx + 1;
            }
        }
        (*outBegIdx) = startIdx;
        (*outNBElement) = outIdx;
        return RetCode::Success;
        } // unsafe
    }
}
/***************/
/* End of File */
/***************/
