import { englishPage } from "./language"

async function handleRequest(request) {
    // request.headers.set('cdn-src-ip', '203.116.0.0')
    const en = englishPage(request)

    const ipinfo = en ? [["request.ws.airportCode", request.ws.airportCode, "Airport Code"],
                ["request.ws.area", request.ws.area, "Area"],
                ["request.ws.asn", request.ws.asn, "ASN"],
                ["request.ws.asOrganization", request.ws.asOrganization, "ASN Organization"],
                ["request.ws.country", request.ws.country, "Country"],
                ["request.ws.countryCode", request.ws.countryCode, "Country Code"],
                ["request.ws.city", request.ws.city, "City"],
                ["request.ws.continent", request.ws.continent, "Continent"],
                ["request.ws.ipBelong", request.ws.ipBelong, "IP Belonging"],
                ["request.ws.isp", request.ws.isp, "ISP"],
                ["request.ws.latitude", request.ws.latitude, "Latitude"],
                ["request.ws.longitude", request.ws.longitude, "Longitude"],
                ["request.ws.province", request.ws.province, "Province"],
                ["request.ws.provinceISO", request.ws.provinceISO, "Province ISO Code"],
                ["request.ws.timezone", request.ws.timezone, "Timezone"],
                ["request.ws.zone", request.ws.zone, "Zone"]]
          : [["request.ws.airportCode", request.ws.airportCode, "机场码"],
                    ["request.ws.area", request.ws.area, "国家大区"],
                    ["request.ws.asn", request.ws.asn, "ASN"],
                    ["request.ws.asOrganization", request.ws.asOrganization, "拥有ASN的组织"],
                    ["request.ws.country", request.ws.country, "国家"],
                    ["request.ws.countryCode", request.ws.countryCode, "国家缩写"],
                    ["request.ws.city", request.ws.city, "城市"],
                    ["request.ws.continent", request.ws.continent, "大洲"],
                    ["request.ws.ipBelong", request.ws.ipBelong, "IP归属"],
                    ["request.ws.isp", request.ws.isp, "运营商"],
                    ["request.ws.latitude", request.ws.latitude, "纬度"],
                    ["request.ws.longitude", request.ws.longitude, "经度"],
                    ["request.ws.province", request.ws.province, "省份"],
                    ["request.ws.provinceISO", request.ws.provinceISO, "省份ISO简称"],
                    ["request.ws.timezone", request.ws.timezone, "时区"],
                    ["request.ws.zone", request.ws.zone, "洲大区"],]

    let page = en ? HOMEPAGE_PREFIX_EN : HOMEPAGE_PREFIX
    page = page.replace('%{cdn-src-ip}', request.headers.get('cdn-src-ip'))
    for (let i = 0; i < ipinfo.length; i++) {
        const m = ipinfo[i]
        page += `
        <tr>
        <td>${i+1}</td>
        <td>${m[0]}</td>
        <td>${m[1]}</td>
        <td>${m[2]}</td>
      </tr>`
    }
    page += HOMEPAGE_SUFFIX

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




const HOMEPAGE_PREFIX_EN = `
<!DOCTYPE html>
<html>
<head>
<title>Client IP Information Display</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>Your access IP is: %{cdn-src-ip}, the detailed information is as follows:</p>
<table>
  <tr>
    <th>Index</th>
    <th>Field Name</th>
    <th>Field Value</th>
    <th>Description</th>
  </tr>
`

const HOMEPAGE_SUFFIX = `
</table>

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