import matplotlib.pyplot as plt
import random

from numpy import tanh


# Значения part = 1, 2 соответствуют режимам считывания с первой и второй пары приборов соответственно
# (currently not available), режимы 3 и 4 являются тестовыми и генерируют значения самостоятельно
def get_point(part, num):
    if part == 1:
        v1 = open('voltmeter1.txt')
        i1 = open('ampere-meter1.txt')
        vol[0].append(float(v1.readline()))
        cur[0].append(float(i1.readline()))
        newv1 = v1.read().replace(str(vol[0][-1]) + '\n', '')
        newi1 = i1.read().replace(str(cur[0][-1]) + '\n', '')
        v1.close()
        i1.close()
        v1 = open('voltmeter1.txt', 'w')
        v1.write(newv1)
        i1 = open('ampere-meter1.txt', 'w')
        i1.write(newi1)
        v1.close()
        i1.close()

    elif part == 2:
        v2 = open('voltmeter2.txt')
        i2 = open('ampere-meter2.txt')
        vol[1].append(float(v2.readline()))
        cur[1].append(float(i2.readline()))
        newv2 = v2.read().replace(str(vol[1][-1]) + '\n', '')
        newi2 = i2.read().replace(str(cur[1][-1]) + '\n', '')
        v2.close()
        i2.close()
        v2 = open('voltmeter2.txt', 'w')
        v2.write(newv2)
        i2 = open('ampere-meter2.txt', 'w')
        i2.write(newi2)
        v2.close()
        i2.close()
    elif part == 3:
        vol[2][num].append(random.random() * 100)
        cur[2][num].append(random.random() * 100)
    elif part == 4:
        vol[3][num].append(random.random() * 50 - 25)
        if experiment % 3 == 0:
            cur[3][num].append(
                (83 * tanh(15.5 / 83 * vol[3][num][-1]) + 1.7 * vol[3][num][-1]) * (0.98 + random.random() / 25))
        elif experiment % 3 == 1:
            cur[3][num].append(
                (44 * tanh(8.7 / 44 * vol[3][num][-1]) + 0.9 * vol[3][num][-1]) * (0.98 + random.random() / 25))
        else:
            cur[3][num].append(
                (22 * tanh(4.4 / 22 * vol[3][num][-1]) + 0.5 * vol[3][num][-1]) * (0.98 + random.random() / 25))


def update_plot(part, num):
    for i in range(10):
        get_point(part, num)


def saveplot(part):
    plt.figure(figsize=(8, 6))
    for i in range(len(vol[part - 1])):
        plt.scatter(vol[part - 1][i], cur[part - 1][i], marker='o')
    plt.grid()
    plt.xlabel('Напряжение, В')
    plt.ylabel('Ток, мкА')
    plt.savefig('tmpplots/tmpplot' + str(part) + '.jpg', dpi=200)
    plt.close()


def empty_plot(part):
    vol[part - 1] = [[]]
    cur[part - 1] = [[]]
    plt.figure(figsize=(8, 6))
    plt.scatter(cur[part - 1], vol[part - 1], marker='o')
    plt.grid()
    plt.savefig('tmpplots/tmpplot' + str(part) + '.jpg', dpi=200)
    plt.close()


def plus_plot(part):
    global experiment
    vol[part - 1].append([])
    cur[part - 1].append([])
    experiment += 1


def minus_plot(part):
    vol[part - 1].pop()
    cur[part - 1].pop()
    if len(vol[part - 1]) == 0:
        vol[part - 1].append([])
        cur[part - 1].append([])


vol = [[], [], [[]], [[]]]
cur = [[], [], [[]], [[]]]
experiment = 0
