import json
from typing import List
from tqdm import tqdm
from random import randint, random, sample
from astpretty import pprint
from copy import deepcopy
from string import ascii_letters
import ast


def write_rows(rows):
    # TODO write this as a gzip for much better compression
    with open('data.10_simple_variations.jsonl', 'a') as f:
        f.writelines(rows)


class AlternativeList(ast.NodeTransformer):
    def __init__(self):
        self.n_changes = 0
        super().__init__()

    def visit_List(self, node: List):
        code = ast.unparse(node)
        try:
            list_val = eval(code)
        except Exception:
            return node

        for _ in range(randint(0, 3)):
            list_val.append(randint(-999, 999))

        if not list_val:
            list_val.append(randint(-999, 999))

        list_val = sample(list_val, randint(1, len(list_val)))

        self.n_changes += 1
        return ast.parse(str(list_val)).body[0].value


class AlternativeConstant(ast.NodeTransformer):
    def __init__(self):
        self.n_changes = 0
        super().__init__()

    def visit_Constant(self, node):
        '''
            Switch constant values to simple variations
        '''
        if type(node.value) is int:
            if randint(0, 1) == 1:
                node.value = randint(-9, 9)
            else:
                node.value = randint(-999, 999)
        elif type(node.value) is str:
            if randint(0, 1) == 1 and node.value:
                if node.value:
                    node.value = ''.join(sample(node.value, randint(1, len(node.value))))
                else:
                    self.n_changes -= 1
            else:
                node.value = ''.join(sample(ascii_letters, randint(1, 4)))
        elif type(node.value) is float:
            if randint(0, 1) == 1:
                node.value = random()
            else:
                node.value = random() * randint(-999, 999)
        elif type(node.value) is bool:
            node.value = bool(randint(0, 1))
        else:
            self.n_changes -= 1
        self.n_changes += 1
        return super().visit_Constant(node)


class AlternativeNames(ast.NodeTransformer):

    def visit_Name(self, node):
        return ast.copy_location(ast.Subscript(
            value=ast.Name(id='data', ctx=ast.Load()),
            slice=ast.Index(value=ast.Str(s=node.id)),
            ctx=node.ctx
        ), node)


def state_dict_to_str(state):
    vals = []
    for k, v in state.items():
        vals.append(
            f'{k} = {v}'
        )
    vals = sorted(vals)
    return ';'.join(vals)


def trace_code(start_state: str, code: str):
    state = {}
    try:
        exec(start_state, {}, state)
    except Exception:
        return
    start_state = dict(state)
    try:
        exec(code, {}, state)
    except Exception:
        return
    return state_dict_to_str(start_state), code, state_dict_to_str(state)


def make_alternative_rows(start, code):
    variations = {}
    n_tries = 0
    state_root = ast.parse(start)

    while len(variations) < 10 and n_tries < 20:

        node_transformer = AlternativeList()
        alt_state_root = node_transformer.visit(deepcopy(state_root))

        if node_transformer.n_changes < 1:
            node_transformer = AlternativeConstant()
            alt_state_root = node_transformer.visit(deepcopy(alt_state_root))
            if node_transformer.n_changes < 1:
                n_tries += 10

        alt_start = ast.unparse(alt_state_root)

        alt_start_code_end = trace_code(alt_start, code)
        if alt_start_code_end:
            variations[alt_start] = alt_start_code_end
        n_tries += 1

    # TODO change the names (keep alphabetical order)
    '''
    get number of vals in start states (N)
    get N random lowercase letters in alphabetical order
    parse start state and shuffle the expr.body
    make name map old->new using new characters
    use visitor with name map to swap variable names using map
        do this for start, code, end seperately
    '''
    return [
        json.dumps({'start': st, 'code': cd, 'end': en}) + '\n' for st, cd, en in variations.values()
    ]


STATS = {
    'alt_count': 0,
    'num_rows': 0
}


with open('new_all_states.txt', 'r') as f:
    rows = []
    prev_lines = []
    for i, line in tqdm(enumerate(f), total=9_000_000):
        line = line.strip()

        if line[-1] != ';':
            prev_lines.append(line)
            continue
        elif prev_lines:
            line = '\n'.join(prev_lines) + '\n' + line
            prev_lines = []

        start, code_end = line.split('; code: ')
        start = start.removeprefix('state: ')
        code, end = code_end.split('; output: ')
        end = end.removesuffix(';')

        rows.append(json.dumps({
            'start': start, 'code': code, 'end': end
        }) + '\n')
        alt_rows = make_alternative_rows(start, code)
        rows += alt_rows

        STATS['alt_count'] += len(alt_rows)
        STATS['num_rows'] += 1

        if len(rows) > 100_000:
            breakpoint()
            write_rows(rows)
            rows = []