import math
import pandas as pd


def tma(df: pd.DataFrame, parameters: dict = None, columns: dict = None) -> tuple:
    """
    Calculates the Triangular Moving Average (TMA) of a series.
    TMA is generated by applying two successive simple moving averages. This
    produces a smoother curve with a triangular weighting, reducing short-term
    noise while keeping overall trend direction visible.

    Args:
        df (pd.DataFrame): The input DataFrame.
        parameters (dict, optional): Dictionary containing calculation parameters:
            - window (int): The lookback period for the TMA. Default is 20.
        columns (dict, optional): Dictionary containing column name mappings:
            - close_col (str): The column name for closing prices. Default is 'Close'.

    Returns:
        tuple: A tuple containing the TMA series and a list of column names.

    The Triangular Moving Average is calculated as follows:

    1. Determine Window Lengths:
       If window is even: Half1 = window/2, Half2 = window/2 + 1
       If window is odd: Half1 = ceil(window/2), Half2 = ceil(window/2)

    2. Calculate First Pass SMA:
       SMA1 = SMA(Close, Half1)

    3. Calculate Second Pass SMA (TMA):
       TMA = SMA(SMA1, Half2)

    Interpretation:
    - TMA places the majority of weight on the middle data points of the series.
    - It is very smooth but has significant lag (roughly twice that of an SMA).

    Use Cases:
    - Trend Identification: Excellent for visualizing the very long-term trend.
    - Noise Reduction: Filters out almost all short-term volatility.
    """
    if parameters is None:
        parameters = {}
    if columns is None:
        columns = {}

    close_col = columns.get('close_col', 'Close')
    window = int(parameters.get('window', 20))

    first_window = max(1, math.ceil(window / 2))
    second_window = max(1, math.floor(window / 2) if window % 2 == 0 else first_window)

    series = df[close_col]
    first_pass = series.rolling(window=first_window).mean()
    tma_series = first_pass.rolling(window=second_window).mean()
    tma_series.name = f'TMA_{window}'

    return tma_series, [tma_series.name]


def strategy_tma(
    data: pd.DataFrame,
    parameters: dict = None,
    config = None,
    trading_type: str = 'long',
    day1_position: str = 'none',
    risk_free_rate: float = 0.0,
    long_entry_pct_cash: float = 1.0,
    short_entry_pct_cash: float = 1.0
) -> tuple:
    """
    TMA (Triangular Moving Average) - Dual MA Crossover Strategy
    
    LOGIC: Buy when fast TMA crosses above slow TMA, sell when crosses below.
    WHY: TMA applies double smoothing (SMA of SMA), creating triangular weighting.
         Very smooth but has significant lag. Best for long-term trend identification.
    BEST MARKETS: Long-term trending markets. Stocks, indices for position trading.
                  Excellent noise reduction but slow to react.
    TIMEFRAME: Daily or weekly charts. Best for long-term position trading.
    
    Args:
        data: DataFrame with OHLCV data
        parameters: Dict with 'short_window' (default 10), 'long_window' (default 20)
        config: BacktestConfig object for backtest settings
        trading_type: 'long', 'short', or 'both'
        day1_position: Initial position ('none', 'long', 'short')
        risk_free_rate: Risk-free rate for Sharpe ratio calculation
        long_entry_pct_cash: Percentage of cash to use for long entries
        short_entry_pct_cash: Percentage of cash to use for short entries
        
    Returns:
        tuple: (results_dict, portfolio_df, indicator_cols_to_plot, data_with_indicators)
    """
    from ..run_cross_trade_strategies import run_cross_trade
    from ..compute_indicators import compute_indicator
    
    if parameters is None:
        parameters = {}
    
    short_window = int(parameters.get('short_window', 10))
    long_window = int(parameters.get('long_window', 20))
    price_col = 'Close'
    
    if short_window == 0:
        short_window_indicator = 'Close'
    else:
        short_window_indicator = f'TMA_{short_window}'
        data, _, _ = compute_indicator(
            data=data,
            indicator='tma',
            parameters={"window": short_window},
            figure=False
        )
    
    long_window_indicator = f'TMA_{long_window}'
    data, _, _ = compute_indicator(
        data=data,
        indicator='tma',
        parameters={"window": long_window},
        figure=False
    )
    
    results, portfolio = run_cross_trade(
        data=data,
        short_window_indicator=short_window_indicator,
        long_window_indicator=long_window_indicator,
        price_col=price_col,
        config=config,
        long_entry_pct_cash=long_entry_pct_cash,
        short_entry_pct_cash=short_entry_pct_cash,
        trading_type=trading_type,
        day1_position=day1_position,
        risk_free_rate=risk_free_rate
    )
    
    indicator_cols_to_plot = [short_window_indicator, long_window_indicator]
    
    return results, portfolio, indicator_cols_to_plot, data
