#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Tuneup assignment
Use the timeit and cProfile libraries to find bad code.
"""

__author__ = "Mavrick Watts with assistance from Chris W, and Mike D"

import cProfile
import pstats
import timeit
import io


def profile(func):
    """A cProfile decorator function that can be used to
    measure performance.
    """
    # Be sure to review the lesson material on decorators.
    # You need to understand how they are constructed and used.
    # raise NotImplementedError("Complete this decorator function")
    def inner(*args, **kwargs):
        pr = cProfile.Profile()
        pr.enable()
        retval = func(*args, **kwargs)
        pr.disable()
        s = io.StringIO()
        sortby = 'cumulative'
        ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
        ps.print_stats()
        print(s.getvalue())
        return retval
    return inner


def read_movies(src):
    """Returns a list of movie titles."""
    print(f'Reading file: {src}')
    with open(src, 'r') as f:
        return f.read().splitlines()


def is_duplicate(title, movies):
    """Returns True if title is within movies list."""
    for movie in movies:
        if movie == title:
            return True
    return False

@profile
def find_duplicate_movies(src):
    """Returns a list of duplicate movies from a src list."""
    movies = read_movies(src)
    movies = [movie.lower() for movie in movies]
    movies.sort()
    duplicates = [movie1 for movie1, movie2 in
        zip(movies[:-1], movies[1:]) if movie1 == movie2]
    return duplicates


def timeit_helper():
    """Part A: Obtain some profiling measurements using timeit."""
    t=timeit.Timer(stmt='find_duplicate_movies("movies.txt")',
     setup='from __main__ import find_duplicate_movies')
    result = min(t.repeat(repeat=7, number=5))//5
    print('Best time across 7 repeats of 5 runs per repeat: ' + str(result) + ' ' + 'sec')
timeit_helper()


def main():
    """Computes a list of duplicate movie entries."""
    result = find_duplicate_movies('movies.txt')
    print(f'Found {len(result)} duplicate movies:')
    print('\n'.join(result))


if __name__ == '__main__':
    main()

