#exec(open("Tanya01.py").read())

import gudhi as gd
import gudhi.representations
import numpy as np
import random

import matplotlib.pyplot as plt

from scipy.spatial.distance import pdist, squareform
import warnings

from sklearn import datasets  # Import the datasets module from scikit-learn

random.seed(23)

# Generate synthetic data using the make_circles function
# n_samples: Number of points to generate
# noise: Standard deviation of Gaussian noise added to the data
# factor: Scale factor between inner and outer circle

circles, labels = datasets.make_circles(n_samples=20, noise=0.09, factor=0.5)

# Create a Rips complex with a maximum edge length of 0.6
Rips_complex = gd.RipsComplex(points = circles, max_edge_length=0.6)

Rips_simplex_tree = Rips_complex.create_simplex_tree(max_dimension=3)

filt_Rips = list(Rips_simplex_tree.get_filtration())

diag_Rips = Rips_simplex_tree.persistence()
for ddd in diag_Rips: print(ddd)

# Plot the persistence diagram

#gudhi.plot_persistence_diagram(diag_Rips, legend=True)
plt.show()

diag_Rips = Rips_simplex_tree.persistence()
#gudhi.plot_persistence_barcode(diag_Rips, legend=True)
plt.show()

st=Rips_simplex_tree
st_gen = st.get_filtration()
for splx in st_gen: print (splx)

num_vertices = st.num_vertices()
num_simplices = st.num_simplices()

print("Number of vertices:", num_vertices)
print("Number of simplices:", num_simplices)


def findInPersistence (vPers, val):
    n1 = 0
    for x in vPers:
        if x[1][1] == val:
            return [n1]
        n1 += 1
    return []

def findInFiltration (vPers, val, len1):
    n1 = 0
    v = []
    for x in vPers:
        if x[1] == val and len(x[0]) != len1: v.append(n1)
        n1 += 1
    return v

def juntaFiltPers (st):
    import numpy as np
    
    diag = st.persistence()

    vPers = []
    for x in diag:
        if x[1][1] == float('inf'): continue
        y = [x[0], [x[1][0], x[1][1]]]
        #np.append(vDiag, y)
        vPers.append(y)

    gen = st.get_filtration()
    vFil = []
    for x in gen:
        if x[1] == 0.0: continue
        vFil.append(x)
        
    n1 = 0
    vfp = []
    for x in vFil:
        opt = 'p'
        n2 = findInPersistence(vPers, x[1])
        if n2 == []:
            opt = 'f'
            n2 = findInFiltration(vFil, x[1], len(x[0]))
        print(f"[{n1:2}] opt: {opt} ; fil: {x} ; link: {n2} ---> {n2}")
        if n2 == []:
            fp = {'f':x, 'opt':'x', 'fp':[]}
        elif opt == 'p':
            fp = {'f':x, 'opt':'p', 'fp':vPers[n2[0]]}
        else:
            v2 = []
            for n3 in n2:
                v2.append(vFil[n3])
            fp = {'f':x, 'opt':'f', 'fp':n2, 'vfp': v2}
            vfp.append(fp)
        n1 += 1
        
    return vfp

def print_VFP (vfp):
    for x in vfp:
        #if x['opt'] == 'p': continue
        if x['opt'] == 'p':
            print(f"{x['f']} <--- ({x['opt']}) ---> {x['fp']}")
        else:
            x1 = len(x['fp'])
            if x1 == 1:
                print(f"{x['f']} <--- ({x['opt']}) ---> {x['vfp'][0]}")
            else:
                print(f"{x['f']} <--- ({x['opt']}) ---> Qt: {x1}")
                for x1 in x['vfp']:
                    print(f"{' '*30} {x1}")
            

#vfp = juntaFiltPers(Rips_simplex_tree)
#print_VFP(vfp)
