from app.functions.roll_dices import roll_dices
from app.tables.cybermods import CyberMods
from app.functions.split_number import split_number


def roll_cybermods():
    """
    Cybermods are similar to mutations, except that they are artificial changes to the body,
    where mutations are 'natural' changes to the body. Fewer cybermods are possible than with
    mutations. So, you make one roll of 1d4, and then parse up the score between the two
    types of mods, as you see fit. If no emphasis is specified, the score will be split as evenly
    as possible. If the score is 1, then a physical mod will be the default.
    :return: table of cybermods (in json format)
    """
    cybermods_table = {}

    cybermods_count = roll_dices(1, 4, False)["result"]
    phys_cnt = split_number(cybermods_count)[1]  # This ends up being the higher of the two numbers
    ment_cnt = split_number(cybermods_count)[0]
    cybermods_table['count'] = {'mental': ment_cnt, 'physical': phys_cnt}

    mental_cybermods_scores = []
    physical_cybermods_scores = []

    for _ in range(ment_cnt):
        mscore = roll_dices(1, 10, False).get('result')
        if mscore in mental_cybermods_scores:
            mscore = roll_dices(1, 10, False).get('result')
        mental_cybermods_scores.append(mscore)

    for _ in range(phys_cnt):
        pscore = roll_dices(1, 10, False).get('result')
        if pscore in physical_cybermods_scores:
            pscore = roll_dices(1, 10, False).get('result')
        physical_cybermods_scores.append(pscore)

    cyb = CyberMods()
    mcyb = []
    pcyb = []

    for score in mental_cybermods_scores:
        modification = cyb.get_mental_cybermod(score)
        mcyb.append(modification)

    if len(mcyb) != 0:
        cybermods_table['mental'] = mcyb

    for score in physical_cybermods_scores:
        modification = cyb.get_physical_cybermod(score)
        pcyb.append(modification)

    if len(pcyb) != 0:
        cybermods_table['physical'] = pcyb

    return cybermods_table
