import math
from thumb_cohen_d import th_cohen_d
import pandas as pd

def es_hedges_g_os(data, mu="none", appr="none", qual="sawilowsky"):
    '''
    Function to calculate Hedges g for a one-sample case.
    
    Parameters
    ----------
    data : numeric list
        list with numbers
    mu : float
        the hypothesized mean
    appr : string
        approximation to use, either 'none', 'hedges', 'durlak' or 'xue'
    qual : string
        rule-of-thumb to use for qualification, see th_cohen_d for details and options
    
    Returns
    -------
    g : float
        Hedges g for a one-sample
    qual : string
        Interpretation of g using Cohen's rule of thumb.
    comment : string
        Version used.
   
    Author
    ------
    Made by P. Stikker
    Please visit: https://PeterStatistics.com    
    YouTube channel: https://www.youtube.com/stikpet    
    '''
    #set hypothesized median to mid range if not provided
    if (mu=="none"):
        mu = (min(data) + max(data)) / 2
        
    # sample size and degrees of freedom
    n = len(data)
    df = n - 1
    
    # sample mean and standard deviation
    avg = sum(data)/n
    s = (sum((data-avg)**2)/df)**(1/2)
    
    # Cohen's d for one-sample
    d = (avg - mu) / s
    
    # helper variable m
    m = df/2
    
    if appr=="none" and m < 172:
        g = d*math.gamma(m)/(math.gamma(m-0.5)*m**(1/2))
        comment = "exact"
    elif appr=="hedges":
        g = d*(1 - 3/(4*df-1))
        comment = "Hedges approximation"
    elif appr=="durlak":
        g = d*(n-3)/(n-2.25)*((n-2)/n)**0.5
        comment = "Durlak approximation"
    else:
        g = d*(1-9/df+69/(2*df**2)-72/(df**3)+687/(8*df**4)-441/(8*df**5)+247/(16*df**6))**(1/12)
        comment = "Xue approximation"
    
    # print warning if exact was asked but couldn't be used
    if appr=="none" and m >= 172:
        print("WARNING: exact method could not be computed due to large sample size, Xue approximation used instead")
        comment = "Xue approximation"
    
    # Adjust to regular Cohen d and take absolute value for interpretation
    abs_g_to_d = abs(g*2**0.5)
    
    # Interpretation table for Cohen d (Cohen, 1988, p. 40)
    qualification = th_cohen_d(abs_g_to_d, qual)
    
    
    testResults = pd.DataFrame([[g, qualification, comment]], columns=["Hedges g", "Classification", "Version"])
        
    pd.set_option('display.max_colwidth', None)
    
    return testResults
