import { englishPage } from "./language"

async function handleRequest(request) {
    movies = []
    const response = await fetch('https://www.rottentomatoes.com/', {cdnProxy: false})
    if (response.status !== 200) {
      return response
    }
    const res = new HTMLRewriter().on('div[data-curation="rt-hp-poster-list-coming-soon"] tiles-carousel-responsive-deprecated > tiles-carousel-responsive-item-deprecated > tile-dynamic > a > score-pairs-deprecated > rt-text', new ElementHandler('rating'))
                                  .on('div[data-curation="rt-hp-poster-list-coming-soon"] tiles-carousel-responsive-deprecated > tiles-carousel-responsive-item-deprecated > tile-dynamic > a > span.p--small', new ElementHandler('title'))
                                  .transform(response)
    const text = await res.text()
    let hostname = request.headers.get('x-ws-request-id')
    if (hostname) hostname = hostname.match(/_(.*?)_/)[1]
    const homepage = englishPage(request) ? HOMEPAGE_PREFIX_EN : HOMEPAGE_PREFIX
    let page = homepage.replace('127.0.0.1', hostname)
    for (let i = 0; i < movies.length; i++) {
        const m = movies[i]
        page += `
        <tr>
        <td>${i+1}</td>
        <td>${m[1]}</td>
        <td>${m[0]}</td>
      </tr>`
    }
    page += HOMEPAGE_SUFFIX

    return new Response(page, {headers: {"Content-Type": "text/html; charset=utf-8"}})
}

let movies = []

class ElementHandler {
    constructor(field) {
        this.field = field
    }
  
    text(text) {
      if (text.text) {
        if (this.field == 'rating') {
          movies.push([text.text])
        } else {
            const m = movies[movies.length - 1]
            m.push(text.text)
        }
      }
    }
  }



const HOMEPAGE_PREFIX_EN = `
<!DOCTYPE html>
<html>
<head>
<title>HTMLRewriter Web Content Scraping</title>
<style>
  table {
    width: 100%;
    border-collapse: collapse;
  }
  th, td {
    padding: 8px;
    text-align: left;
    border-bottom: 1px solid #ddd;
  }
  tr:nth-child(even) {
    background-color: #f2f2f2;
  }
</style>
</head>
<body>
<p>HTMLRewriter is a streaming web content rewriter. This example demonstrates how to use it to scrape content from a target webpage.</p>
<p>Scraping the 'New & Upcoming In Theaters' movies from <a href="https://www.rottentomatoes.com/" target="_blank">Rotten Tomatoes</a> and displaying them below.</p>
<table>
  <tr>
    <th>Index</th>
    <th>Title</th>
    <th>Rating</th>
  </tr>
`

const HOMEPAGE_SUFFIX = `
</table>

</body>
</html>
`
                    
addEventListener("fetch", event => {
    return event.respondWith(handleRequest(event.request))
})