import numpy as np
from matplotlib import pyplot as plt
import scipy as sp
from scipy.stats import pearsonr
from matplotlib import markers
from sys import argv
import json
import random
blue = '#0984e3'
red = '#d63031'
green = '#227D51'
kikyo = '#6A4C9C'
hanaba = '#F7C242'

sim = [0.79844266, 0.8377947, 0.87015217, 0.91429971, 0.94337877, 0.89212607, 0.99805755, 0.98048562, 0.98530593, 0.99598291, 0.87856398, 0.99217795, 0.62392839, 0.99253731, 0.99703892]
ana = [0.8, 0.8290884722222223, 0.8581769444444445, 0.8872654166666667, 0.9163538888888889, 0.9454423611111111, 0.9745308333333333, 1.0, 1.0, 1.0, 1.0, 1.0, 0.5, 1.0, 1.0]
nowak_classic = [0.50427784, 0.26236224, 0.25333527, 0.25242894, 0.25232019, 0.25228393, 0.25228393, 0.25228393, 0.25228393, 0.25228393, 0.25228393, 0.25228393, 0.25228393, 0.25228393, 0.25228393]
nowak_survive = [0.50017833, 0.26068193, 0.88939394, 0.9595738, 0.99796403, 0.99828179, 0.99827646, 1, 1, 1, 1, 1, 1, None, None]
santos_survive = [0.50071587, 0.27929701, 0.9098483, 0.938, 0.99437628, 0.99861352, 0.99877322, 1, 1, 1, 1, 1, 1, None, None]
santos_classic = [0.49821874, 0.2757036, 0.26718917, 0.26270039, 0.25895974, 0.2562166, 0.25439971, 0.25290346, 0.25187032, 0.25097969, 0.25037406, 0.24983969, 0.24919843, 0.24884218, 0.24862843]

MAX_ROUND = 10

def p1(x, coopr, crawx, crawy, postfix, show=True):
    fig = plt.figure(figsize=(5, 2))
    ax = fig.gca()
    if postfix == 'SURVIVE':
        ax.plot(x, sim, marker='s', linestyle='dotted', markerfacecolor='none', color=red, linewidth=2, label="Simulation")
        ax.plot(x, ana, marker='^', linestyle='dotted', markerfacecolor='none', color=green, linewidth=2, label="Analytical")
        ax.plot(x, santos_survive, marker='x', linestyle='dotted', markerfacecolor='none', color=kikyo, linewidth=2, label="Baseline#1")
        ax.plot(x, nowak_survive, marker='*', linestyle='dotted', markerfacecolor='none', color=hanaba, linewidth=2, label="Baseline#2")
    elif postfix == 'CLASSIC':
        ax.plot(x, santos_classic, marker='x', linestyle='dotted', markerfacecolor='none', color=kikyo, linewidth=2, label="Baseline#1")
        ax.plot(x, nowak_classic, marker='*', linestyle='dotted', markerfacecolor='none', color=hanaba, linewidth=2, label="Baseline#2")
    ax.plot(x, coopr, marker='o', linestyle='dotted', markerfacecolor='none', color=blue, linewidth=2, label="Human")
    ax.scatter(crawx, crawy, s=MAX_ROUND, c='dimgray', marker='x', linewidth=0.5, alpha=0.4)
    ax.set_ylim(-0.05, 1.05)
    ax.set_yticks(np.linspace(0, 1, 5))
    ax.tick_params(labelsize=10)
    ax.tick_params(direction='in')
    ax.set_xlim(0, MAX_ROUND+1)
    # ax.set_xticks([1, 5, 10, 15, 20, 25, 30])
    ax.set_xticks([1, 5, 10])
    # ax.set_xticklabels([''])
    ax.set_xlabel("Rounds", size=11)
    ax.set_ylabel(r"f$_{\rm c}$", size=11, fontstyle='normal')
    ax.yaxis.grid(True, linestyle='--')
    # if postfix == 'SURVIVE':
    #     ax.legend(loc='upper center', fontsize=10, ncol=3)

    plt.tight_layout()
    if show:
        plt.show()
    else:
        plt.savefig("graph/fc_plot_%s.pdf" % postfix)


def plot(mode, show, network_type=None):
    coopr = np.loadtxt("outputs/CR_%s_%s.csv" % (mode, network_type), delimiter=',')
    crawx, crawy = [],[]
    with open("outputs/CRAW_%s_%s.json" % (mode, network_type)) as f:
        data = json.load(f)
        for l in data:
            for i in range(len(l)):
                crawx.append((i + 1) + random.uniform(-0.25, 0.25))
                crawy.append(l[i])
    x = np.arange(1, MAX_ROUND + 1)
    p1(x, coopr[:MAX_ROUND], crawx, crawy, f'{mode}_{network_type}', show)

if __name__ == '__main__':
    # fc_plot c/s/nb/nw t/f
    network_type = None
    if argv[1] == 'c':
        mode = 'CLASSIC'
    elif argv[1] == 's':
        mode = 'SURVIVE'
    elif argv[1] == 'nb':
        mode = 'NEW'
        network_type = 'BA'
    else:
        mode = 'NEW'
        network_type = 'WS'

    show = argv[2] == 't'
    plot(mode, show, network_type)
