import logging


def create_dictionary(new_time, new_voltage, working_data, measures,
                      filename):
    """Create a fully analyzed dictionary of the ECG data

    It will load the processed data from measures and raw data from
    working_data and generate a dictionary listed as
    metrics = {'duration': duration,
               'voltage_extremes': voltage_extremes,
               'num_beats': num_beats,
               'mean_hr_bpm': mean_hr_bpm,
               'beats': beats}.

    :param new_time: list of time of data
    :param new_voltage: list of voltage of data
    :param working_data: basic information of raw data before processing
    :param measures: a dictionary of information of processed data
    :param filename: name of the file being read

    :returns:
        - metrics - a dictionary containing duration,
                    voltage extremes, number of beats, beats per minute,
                    and the time where beats occur
    """
    logging.info("Start calculation/assigning duration of"
                 " ECG trace of file {}".format(filename))
    duration = new_time[len(new_time) - 1]

    logging.info("Start calculation/assigning voltage_extremes of"
                 " ECG trace of file {}".format(filename))
    voltage_extremes = (min(new_voltage), max(new_voltage))

    logging.info("Start calculation/assigning num_beats of"
                 " ECG trace of file {}".format(filename))

    peak_len = len(working_data['peaklist'])
    rm_peak_len = len(working_data['removed_beats'])
    num_beats = peak_len - rm_peak_len

    logging.info("Start calculation/assigning mean_hr_bpm of"
                 " ECG trace of file {}".format(filename))
    mean_hr_bpm = measures['bpm']

    logging.info("Start calculation/assigning where the beats"
                 " of ECG trace of file {}".format(filename))
    beats = [new_time[i] for i in [x for x in working_data['peaklist']
                                   if x not in working_data['removed_beats']]]

    metrics = {'duration': duration,
               'voltage_extremes': voltage_extremes,
               'num_beats': num_beats,
               'mean_hr_bpm': mean_hr_bpm,
               'beats': beats}
    return metrics