import pprint
from FactorioCalcBase.data.binary import sorted_recipe_list, production_machine_category_list_dict
from FactorioCalcBase.recipe import Recipe
from FactorioCalcBase.calculator_base import CalculatorBase
from FactorioCalcBase.dependency_dict_common_function import dict_add_number
import time


def test_change_machine(test_obj: CalculatorBase, target_recipe, failed_dict):
    recipe_obj = Recipe(recipe_name=target_recipe)
    cat = recipe_obj.get_category()
    available_machine_list = production_machine_category_list_dict.get(cat)
    failed_dict['method_failed']['change_machine_failed'] = {}
    if len(available_machine_list) > 1:
        for machine in available_machine_list:
            test_obj.change_machine_to_specific_block(recipe_name=target_recipe,
                                                      machine_name=machine)
            if test_obj.block_obj_dict['recipe']['machine_name'] != machine:
                raise 'MachineNotChanged'


def test_calculator_base_methods(test_obj: CalculatorBase, failed_dict: dict):

    recipe_list = list(test_obj.block_obj_dict['recipe'].keys())
    for recipe in recipe_list:
        try:
            test_change_machine(test_obj, recipe, failed_dict)
        except:
            dict_add_number(failed_dict['method_failed']['change_machine_failed'], recipe, 1)


def test_calculator_base(failed_dict):
    mrms = [0, 0.3]
    pm = [None, ["assembling-machine-2", "stone-furnace", "burner-mining-drill"]]
    uk = [True, False]
    am = [1, 101.5]

    failed_dict['init_failed'] = {}
    failed_dict['method_failed'] = {
        'change_machine_failed': {

        }
    }
    for recipe in sorted_recipe_list:
        for mining_research_modifier in mrms:
            for preferred_machines in pm:
                for use_kovarex in uk:
                    for amount in am:
                        try:
                            test_obj = CalculatorBase(recipe_name=recipe, amount=amount,
                                                      preferred_machine_list=preferred_machines,
                                                      use_kovarex=use_kovarex,
                                                      mining_research_modifier=mining_research_modifier)
                        except:
                            dict_add_number(failed_dict['init_failed'], key=recipe, val=1)
                        test_calculator_base_methods(test_obj, failed_dict)
    pprint.pp(failed_dict)
    return failed_dict


def run_test():
    start_time = time.time()
    test_calculator_base({})
    print(f'finished in {time.time()-start_time}')

[((10, 17, 10, 50), 'FactorioCalcBase.recipe.Recipe', 'Recipe', (), '', False, 'from FactorioCalcBase.recipe import Recipe\n'), ((12, 29, 12, 75), 'FactorioCalcBase.data.binary.production_machine_category_list_dict.get', 'production_machine_category_list_dict.get', ({(12, 71, 12, 74): 'cat'}, {}), '(cat)', False, 'from FactorioCalcBase.data.binary import sorted_recipe_list, production_machine_category_list_dict\n'), ((57, 4, 57, 26), 'pprint.pp', 'pprint.pp', ({(57, 14, 57, 25): 'failed_dict'}, {}), '(failed_dict)', False, 'import pprint\n'), ((62, 17, 62, 28), 'time.time', 'time.time', ({}, {}), '()', False, 'import time\n'), ((29, 12, 29, 93), 'FactorioCalcBase.dependency_dict_common_function.dict_add_number', 'dict_add_number', ({(29, 28, 29, 81): "failed_dict['method_failed']['change_machine_failed']", (29, 83, 29, 89): 'recipe', (29, 91, 29, 92): '(1)'}, {}), "(failed_dict['method_failed']['change_machine_failed'],\n    recipe, 1)", False, 'from FactorioCalcBase.dependency_dict_common_function import dict_add_number\n'), ((64, 25, 64, 36), 'time.time', 'time.time', ({}, {}), '()', False, 'import time\n'), ((50, 39, 53, 104), 'FactorioCalcBase.calculator_base.CalculatorBase', 'CalculatorBase', (), '', False, 'from FactorioCalcBase.calculator_base import CalculatorBase\n'), ((55, 28, 55, 90), 'FactorioCalcBase.dependency_dict_common_function.dict_add_number', 'dict_add_number', (), '', False, 'from FactorioCalcBase.dependency_dict_common_function import dict_add_number\n')]