# fresh_tomatoes.py

import webbrowser
import os
import re
import entertainment_center

# Opening DOCTYPE and html tags
html_open = '''
<!DOCTYPE html>
<html lang="en">
'''

# Closing html tag
html_close = '''
</html>
'''

# Styles and scripting for the page
main_page_head = '''
<head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="IE=Edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="keywords" content="Vishal Lama, Udacity, Movie Trailers">
    <title>Fresh Tomatoes!</title>

    <!-- Custom social media icons -->
    <link rel="stylesheet"
          href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">

    <!-- Bootstrap 3 -->
    <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap-theme.min.css">
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <script src="https://netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
    <style type="text/css" media="screen">
        body {
            padding: 80px 0 0 0;
        }
        #trailer .modal-dialog {
            margin: 100px 0 0 0;
            width: 640px;
            height: 480px;
        }
        .hanging-close {
            position: absolute;
            top: -12px;
            right: -12px;
            z-index: 9001;
        }
        #trailer-video {
            width: 100%;
            height: 100%;
        }
        .movie-tile {
            margin: 0 0 0 20px;
            padding: 20px 0 0 0;
        }
        .movie-tile:hover {
            background-color: #EEE;
            cursor: pointer;
            transform: scale(1.05);
            transition: all 0.5s ease 0s;
        }
        .scale-media {
            padding-bottom: 56.25%;
            position: relative;
        }
        .scale-media iframe {
            border: none;
            height: 100%;
            position: absolute;
            width: 100%;
            left: 0;
            top: 0;
            background-color: white;
        }
        footer {
            padding: 20px;
            background: #288;
        }
    </style>
    <script type="text/javascript">
        // Pause the video when the modal is closed
        $(document).on('click', '.hanging-close, .modal-backdrop, .modal', function (event) {
            // Remove the src so the player itself gets removed, as this is the only
            // reliable way to ensure the video stops playing in IE
            $("#trailer-video-container").empty();
        });
        // Start playing the video whenever the trailer modal is opened
        $(document).on('click', '.movie-tile', function (event) {
            var trailerYouTubeId = $(this).attr('data-trailer-youtube-id')
            var sourceUrl = 'http://www.youtube.com/embed/' + trailerYouTubeId + '?autoplay=1&html5=1';
            $("#trailer-video-container").empty().append($("<iframe></iframe>", {
              'id': 'trailer-video',
              'type': 'text-html',
              'src': sourceUrl,
              'frameborder': 0
            }));
        });
        // Animate in the movies when the page loads
        $(document).ready(function () {
          $('.movie-tile').hide().first().show("fast", function showNext() {
            $(this).next("div").show("fast", showNext);
          });
        });
    </script>
</head>
'''

# The main page layout and title bar
main_page_content = '''
<body>
<!-- Trailer Video Modal -->
<div class="modal" id="trailer">
  <div class="modal-dialog">
    <div class="modal-content">
      <a href="#" class="hanging-close" data-dismiss="modal" aria-hidden="true"></a>
      <div class="scale-media" id="trailer-video-container">
      </div>
    </div>
  </div>
</div>

<!-- Main Page Content -->
<div class="container">
  <div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
    <div class="container">
      <div class="navbar-header">
        <a class="navbar-brand" href="#">Fresh Tomatoes Movie Trailers</a>
      </div>
    </div>
  </div>
</div>
<div class="container">
  <div class="row">
    {movie_tiles}
  </div>
</div>
<div class="clearfix visible-sm-block"></div>
<footer>
    <div class="col-sm-12 text-center">
      <h4>&copy; 2015 Fresh Tomatoes!</h4>
    </div>
</footer>
</body>
'''

# A single movie entry html template
movie_tile_content = '''
<div class="col-md-6 col-lg-4 movie-tile text-center"
    data-trailer-youtube-id="{trailer_youtube_id}" data-toggle="modal" data-target="#trailer">
    <img src="{poster_image_url}" width="220" height="342" alt="{movie_title}">
    <h2>{movie_title}</h2>
    <h4>{release_year}</h4>
    <h5>{movie_rating}</h5>
</div>
'''


def create_movie_tiles_content(movies):
    # The HTML content for this section of the page
    content = ''
    for movie in movies:
        # Extract the youtube ID from the url
        youtube_id_match = re.search(r'(?<=v=)[^&#]+', movie.trailer_youtube_url)
        youtube_id_match = youtube_id_match or re.search(r'(?<=be/)[^&#]+', movie.trailer_youtube_url)
        trailer_youtube_id = youtube_id_match.group(0) if youtube_id_match else None

        # Append the tile for the movie with its content filled in
        content += movie_tile_content.format(
            movie_title=movie.title,
            poster_image_url=movie.poster_image_url,
            trailer_youtube_id=trailer_youtube_id,
            movie_rating = movie.mpaa_rating,
            release_year = movie.release_date.split()[-1]
        )
    return content


def open_movies_page(movies):
    # Create or overwrite the output file
    output_file = open('fresh_tomatoes.html', 'w')

    # Replace the placeholder for the movie tiles with the actual dynamically generated content
    rendered_content = main_page_content.format(movie_tiles=create_movie_tiles_content(movies))

    # Output the file
    output_file.write(html_open + main_page_head + rendered_content + html_close)
    output_file.close()

    # open the output file in the browser
    url = os.path.abspath(output_file.name)
    webbrowser.open('file://' + url, new=2)     # open in a new tab, if possible


def main():
    open_movies_page(entertainment_center.movies)


if __name__ == "__main__":
    main()


@@ -1201,20 +1201,18 @@
 ding
+-top
 : 80px
- 0 0 0
 ;%0A  
@@ -1274,21 +1274,19 @@
 rgin
+-top
 : 
-1
+2
 00px
- 0 0 0
 ;%0A  
@@ -1620,15 +1620,16 @@
 rgin
-: 0 0 0
+-bottom:
  20p
@@ -1654,20 +1654,18 @@
 ding
+-top
 : 20px
- 0 0 0
 ;%0A  

Update padding for responsive images