from load_pgn_to_graph import *
from random import shuffle
from data_set import *
from my_graph import *
import numpy as np
import math
import os


def create_graph_from_players_png(file_name, number_of_games=None):
    lpg = LoadPgnToGraph(file_name=file_name, number_of_games=number_of_games)
    lpg.load_players_games()
    lpg.export()

def eigenvalues_80(player):
    file_name = 'exported_data/' + player + '_eig_80.txt'
    if os.path.isfile(file_name):
        os.remove(file_name)

    for i in range(50, 101, 50):
        g = MyGraph('graph')
        g.load_gml('exported_graphs/' + player + '.gml')
        daco = g.eigenvalue(True)
        daco = [abs(i) for i in daco]
        daco = sorted(daco)

        with open(file_name, mode='a') as file:
            x = int((80 * len(daco)) / 100)
            file.write(str(i) + ' ' + str(daco[x]) + '\n')
        file.close()

def eigenvalues(player, number):
    file_name = 'exported_data/' + player + '_eig_' + str(number) + '.txt'
    if os.path.isfile(file_name):
        os.remove(file_name)

    for i in range(100, 4001, 100):
        g = MyGraph('test_matrix')
        g.load_gml('exported_graphs/' + player + '/' + player + '_' + str(i) + '.gml')
        daco = g.eigenvalue(True)
        daco = [abs(i) for i in daco]
        daco = sorted(daco)

        with open(file_name, mode='a') as file:
            x = int((number * len(daco)) / 100)
            file.write(str(i) + ' ' + str(daco[x]) + '\n')
        file.close()

def complex_plane(player):
    g = MyGraph('g')
    g.load_gml('exported_graphs/' + player + '.gml')
    daco = g.eigenvalue(True)

    with open('exported_data/' + player + '_eigen.txt', mode='w') as file:
        for i in reversed(daco):
            comp = complex(i)
            a = str(comp.real)
            b = str(comp.imag)
            file.write(a + ' ' + b + '\n')
    file.close()

def fit_power_law(file_path):

    with open(file_path + '.txt', 'r') as file:
        lines = file.readlines()

    X = [float(line.strip().split(' ')[0]) for line in lines]
    Y = [float(line.strip().split(' ')[1]) for line in lines]
    X = X[:300]
    Y = Y[:300]
    k, q = np.polyfit(X, Y, 1)
    print(k, q)

    new_Y = [float((x*k + q)) for x in X if x != 0]

    string = ""
    for i in range(len(new_Y)):
        string += str(X[i]) + ' ' + str(new_Y[i]) + '\n'

    with open(file_path + '_fitted.txt', 'w') as file:
        file.write(string)

def export_inverted_graph(player_name):
    g = MyGraph('inverted_' + player_name)
    g.load_gml('C:/Users/Palo/Desktop/skola/3.rok/bakalarska_praca/source/exported_graphs/' + player_name + '.gml')
    g.invert_edges()
    g.export_to_xlsx()

# FREQUENCY
def frequency(file_name, l):
    with open(file_name + '.txt', 'w') as file:
        index = 1
        s = sum(l)
        lar = l[-1]
        for i in reversed(l):
            if index == 500:
                break
            file.write(str(math.log(index, 10)) + ' ' + str(math.log(i / lar, 10)) + '\n')

            index += 1
    file.close()

# SEQUENCES

def sequences(file_name, l):
    with open(file_name + '.txt', 'w') as file:
        index = 1
        s = sum(l)
        lar = l[-1]
        for i in reversed(l):
            file.write(str(math.log(index, 10)) + ' ' + str(math.log(i, 10)) + '\n')
            index += 1
    file.close()

# DEGREES

def degrees(file_name, l):
    s = sum(carlsen_in_degrees)
    k_max = l[-1]
    with open(file_name + '.txt', 'w') as file:
        k = 1
        for i in reversed(l):
            file.write(str(math.log(i / k_max, 10)) + ' ' + str(math.log(k, 10)) + '\n')
            k += 1
    file.close()

# RANKS

def ranks(file_name, l):
    with open(file_name + '.txt', 'w') as file:
        index = 1
        for i in reversed(l):
            file.write(str(math.log(index, 10)) + ' ' + str(math.log(i, 10)) + '\n')
            index += 1
    file.close()

def pagerank_vs_ceirank(page, chei, file_name):
    rank_dictionary = dict()
    for i in range(len(page)):
        rank_dictionary[page[i]] = chei[i]

    rank_dictionary = sorted(rank_dictionary.items())

    with open(file_name + '.txt', 'w') as file:
        index = 1
        for k, v in rank_dictionary:
            file.write(str(k) + ' ' + str(v) + '\n')
            index += 1
    file.close()

def run_demo():
    create_graph_from_players_png("Ding.pgn", number_of_games=500)
    eigenvalues_80('Ding')
    complex_plane('Ding')
    frequency('exported_data/ding_500', ding)
    degrees('exported_data/ding_in_degrees', ding_in_degrees)
    degrees('exported_data/ding_out_degrees', ding_out_degrees)
    ding_cheirank_unordered.sort()
    ranks('exported_data/ding_pagerank', ding_pagerank)
    ranks('exported_data/ding_cheirank', ding_cheirank_unordered)
    ranks('exported_data/ding_hubs', ding_hubs)
    ranks('exported_data/ding_authorities', ding_authorities)
    pagerank_vs_ceirank(ding_pagerank_unordered, ding_cheirank_unordered, 'exported_data/ding_pr_cr')

run_demo()



