import copy
import os
from . import res

from pprint import pprint

def start_workflow():
    res.bootstrap_folder_structure()

    aggregated_metrics_av1_from_file = res.read_dict_from_json_file(
        os.path.join(
            res.get_path_results_aggregations(),
            res.get_filename_results_aggregations_av1()
        )
    )
    aggregated_metrics_hevc_from_file = res.read_dict_from_json_file(
        os.path.join(
            res.get_path_results_aggregations(),
            res.get_filename_results_aggregations_hevc()
        )
    )
    
    aggregated_metrics_hevc = {}
    for preset, data_of_preset in aggregated_metrics_hevc_from_file.items():
        for crf, data_of_crf in data_of_preset.items():
            aggregated_metrics_hevc = copy.deepcopy(data_of_crf)
            del aggregated_metrics_hevc['samples']

    tolerance_filesize_in_percent = 20
    hevc_filesize_percentage_mean = aggregated_metrics_hevc["filesize_percentage"]["mean"]
    hevc_filesize_percentage_median = aggregated_metrics_hevc["filesize_percentage"]["median"]
    hevc_filesize_percentage_mean_with_tolerance = aggregated_metrics_hevc["filesize_percentage"]["mean"] * (1 + tolerance_filesize_in_percent / 100)
    hevc_filesize_percentage_median_with_tolerance = aggregated_metrics_hevc["filesize_percentage"]["median"] * (1 + tolerance_filesize_in_percent / 100)
    hevc_vmaf_score_mean = aggregated_metrics_hevc["vmaf_score"]["mean"]
    hevc_vmaf_score_median = aggregated_metrics_hevc["vmaf_score"]["median"]

    possible_candidates = {}
    possible_candidates_with_tolerance = {}
    for preset, data_of_preset in aggregated_metrics_av1_from_file.items():
        for crf, data_of_crf in data_of_preset.items():
            viable_only_with_tolerance = False

            av1_filesize_percentage_mean = data_of_crf["filesize_percentage"]["mean"]
            av1_filesize_percentage_median = data_of_crf["filesize_percentage"]["median"]
            av1_vmaf_score_mean = data_of_crf["vmaf_score"]["mean"]
            av1_vmaf_score_median = data_of_crf["vmaf_score"]["median"]

            if av1_filesize_percentage_mean > hevc_filesize_percentage_mean:
                if av1_filesize_percentage_mean > hevc_filesize_percentage_mean_with_tolerance:
                    continue
                viable_only_with_tolerance = True
            if av1_filesize_percentage_median > hevc_filesize_percentage_median:
                if av1_filesize_percentage_median > hevc_filesize_percentage_median_with_tolerance:
                    continue
                viable_only_with_tolerance = True
            if av1_vmaf_score_mean < hevc_vmaf_score_mean:
                continue
            if av1_vmaf_score_median < hevc_vmaf_score_median:
                continue

            if viable_only_with_tolerance:
                if not preset in possible_candidates_with_tolerance.keys():
                    possible_candidates_with_tolerance[preset] = {}
        
                possible_candidates_with_tolerance[preset][crf] = copy.deepcopy(data_of_crf)
                del possible_candidates_with_tolerance[preset][crf]["samples"]
                continue
            
            if not preset in possible_candidates.keys():
                possible_candidates[preset] = {}
    
            possible_candidates[preset][crf] = copy.deepcopy(data_of_crf)
            del possible_candidates[preset][crf]["samples"]


    output_file = os.path.join(
        res.get_path_results_candidates(),
        res.get_filename_results_candidates_viable()
    )
    output_file_with_tolerance = os.path.join(
        res.get_path_results_candidates(),
        res.get_filename_results_candidates_viable_with_tolerance()
    )

    res.write_dict_to_json_file(output_file, possible_candidates)
    res.write_dict_to_json_file(output_file_with_tolerance, possible_candidates_with_tolerance)