Attribute VB_Name = "Racecard_aspx"

Option Explicit

Public Sub ScrapeRacecard()
  Common.ResetSheet

  Dim COL_IDX_HORSE_NO As Integer
  Dim COL_IDX_LAST_6_RUNS As Integer
  Dim COL_IDX_COLOUR As Integer
  Dim COL_IDX_HORSE As Integer
  Dim COL_IDX_BRAND_NO As Integer
  Dim COL_IDX_WT As Integer
  Dim COL_IDX_JOCKEY As Integer
  Dim COL_IDX_OVER_WT As Integer
  Dim COL_IDX_DRAW As Integer
  Dim COL_IDX_TRAINER As Integer
  Dim COL_IDX_RTG As Integer
  Dim COL_IDX_RTG_PLUS_MINUS As Integer
  Dim COL_IDX_HORSE_WT_DECLARATION As Integer
  Dim COL_IDX_WT_PLUS_MINUS_VS_DECLARATION As Integer
  Dim COL_IDX_BEST_TIME As Integer
  Dim COL_IDX_AGE As Integer
  Dim COL_IDX_WFA As Integer
  Dim COL_IDX_SEX As Integer
  Dim COL_IDX_SEASON_STAKES As Integer
  Dim COL_IDX_PRIORITY As Integer
  Dim COL_IDX_DAYS_SINCE_LAST_RUN As Integer
  Dim COL_IDX_GEAR As Integer
  Dim COL_IDX_OWNER As Integer
  Dim COL_IDX_SIRE As Integer
  Dim COL_IDX_DAM As Integer
  Dim COL_IDX_IMPORT_CAT As Integer

  Dim i, j, k, l, m As Integer
  Dim not_used As String

  Dim CURRENT_DIR As String
  CURRENT_DIR = ThisWorkbook.Path

  ' Create a new instance of the WebDriver.
  Dim driver As New WebDriver

  ' Set the path to the WebDriver executable.
  driver.SetBinary CURRENT_DIR & "\Chromium\Application\chrome.exe"
  driver.AddArgument "--disable-blink-features=AutomationControlled --blink-settings=imagesEnabled=false"

  ' Start Chrome.
  driver.Start "chrome"

  ' not_used = Common.WriteCell(s"A3","helloworld")

  Dim startCell As Range
  Set startCell = Worksheets("Sheet1").Range("A3")

  Dim row_offset, row_offset_start As Integer
  row_offset = 0
  row_offset_start = 0

  Dim driver1 As New WebDriver
  driver1.SetBinary CURRENT_DIR & "\Chromium\Application\chrome.exe"
  driver1.AddArgument "--disable-blink-features=AutomationControlled --blink-settings=imagesEnabled=false"
  driver1.Start "chrome"

  driver.Get "https://racing.hkjc.com/racing/information/English/Racing/Racecard.aspx"
  Dim race_links_scraped, race_links As Variant
  race_links_scraped = driver.ExecuteScript( _
      "let links = [];"  & _
      "document"  & _
      "  .querySelectorAll('.top_races')[0]"  & _
      "  .querySelectorAll('a')"  & _
      "  .forEach((e) => links.push(e.href));"  & _
      "first_race_link = links[0].replace('RaceNo=2', 'RaceNo=1');"  & _
      "links = [first_race_link, ...links];"  & _
      "return links.join('||');"  _
  )
  race_links = Split(race_links_scraped, "||")


  Dim first_race_link, last_race_link As String
  first_race_link = race_links(0)
  last_race_link = race_links(UBound(race_links))
  Dim s_date, s_loc, s_race_num As String
  Dim i_num As Integer

  s_date = CStr(getDateFrRaceCardLink(first_race_link)(0))
  s_loc = CStr(getDateFrRaceCardLink(first_race_link)(1))
  s_race_num = CStr(getDateFrRaceCardLink(last_race_link)(2))
  i_num = CInt(s_race_num)

  Dim race_link as Variant

  ' on error goto eh
  On Error GoTo ErrorHandler


  For Each race_link In race_links
  ' For m = 1 To i_num 'i_num
    ' Navigate to a website.
    ' driver.Get "https://racing.hkjc.com/racing/information/Chinese/Racing/Racecard.aspx?RaceDate=" & s_date & "&Racecourse=" & s_loc & "&RaceNo=" & CStr(m)

    Dim chinese_race_link as String
    chinese_race_link = replace(race_link, "English","Chinese")
    driver.Get chinese_race_link

    driver.FindElementsByTag ("td")
    ' document.querySelectorAll('#racecardlist tbody tr')[2].textContent

    Dim race_summary_content, race_summary_result As String
    ' document.querySelectorAll('.raceCard div')[6].textContent.split(',').map(l => l.replace(/\n/g,'').trim()).filter(l => l.trim() != '')
    race_summary_content = "{ return  document.querySelectorAll('.raceCard div')[8]?.innerHTML.replace(/<br>/g,', ').split(', ').join('|||') }"
    race_summary_result = driver.ExecuteScript(race_summary_content)

    Dim course_name As String
    Dim course_length As String
    Dim course_class As String
    Dim course_condition As String
    Dim mark_range As String

    course_name = driver.ExecuteScript("{return document.querySelectorAll('.raceCard div')[8]?.innerHTML.split(/<br>/g)[2].split(', ').reverse().slice(2).join(' ')}")
    course_length = driver.ExecuteScript("{return document.querySelectorAll('.raceCard div')[8]?.innerHTML.split(/<br>/g)[2].split(', ').reverse()[1]}")
    course_class = driver.ExecuteScript("{return document.querySelectorAll('.raceCard div')[8]?.innerHTML.split(/<br>/g)[3].split(', ').reverse()[0] }")
    course_condition = driver.ExecuteScript("{return document.querySelectorAll('.raceCard div')[8]?.innerHTML.split(/<br>/g)[2].split(', ').reverse()[0] }")
    mark_range = driver.ExecuteScript("{return document.querySelectorAll('.raceCard div')[8]?.innerHTML.split(/<br>/g)[3].split(', ').reverse()[1].split(': ')[1] }")

    row_offset_start = row_offset

    Dim actual_horse_count As Integer
    actual_horse_count = 0

    For j = 0 To 30

      Dim s_test, s_test_result As String
      s_test = "{ return  document.querySelectorAll('#racecardlist tbody tr td')[" & (27) + j * 27 + 1 & "]?.textContent.trim()||'not found' }"
      s_test_result = driver.ExecuteScript(s_test)

      If (s_test_result = "not found") Then

      Else
        actual_horse_count = actual_horse_count + 1

        Dim horse_id As String

        Dim s_content, s_result As String
        '1  ????       --> Horse No.
        s_content = "{ return  document.querySelectorAll('#racecardlist tbody tr td')[" & (27) + j * 27 + 1 & "].textContent.trim() }"
        s_result = driver.ExecuteScript(s_content)
        startCell.Offset(row_offset, 1).value = s_result

        '2  6???        --> Last 6 Runs
        '3  ??       --> Colour
        '4  ??       --> Horse
        s_content = "{ return  document.querySelectorAll('#racecardlist tbody tr td')[" & (27) + j * 27 + 4 & "].textContent.trim() }"
        s_result = driver.ExecuteScript(s_content)
        startCell.Offset(row_offset, 2).value = s_result

        horse_id = driver.ExecuteScript("{ return  document.querySelectorAll('#racecardlist tbody tr td')[" & (27) + j * 27 + 4 & "].querySelector('a').href.split('=')[1] }")
        startCell.Offset(row_offset, 400).value = horse_id

        '5  ??       --> Brand No.
        s_content = "{ return  document.querySelectorAll('#racecardlist tbody tr td')[" & (27) + j * 27 + 5 & "].textContent.trim() }"
        s_result = driver.ExecuteScript(s_content)
        startCell.Offset(row_offset, 0).value = s_result

        '6  ??       --> Wt.
        s_content = "{ return  document.querySelectorAll('#racecardlist tbody tr td')[" & (27) + j * 27 + 6 & "].textContent.trim() }"
        s_result = driver.ExecuteScript(s_content)
        startCell.Offset(row_offset, 7).value = s_result

        '7  ??       --> Jockey
        s_content = "{ return  document.querySelectorAll('#racecardlist tbody tr td')[" & (27) + j * 27 + 7 & "].textContent.trim() }"
        s_result = driver.ExecuteScript(s_content)
        startCell.Offset(row_offset, 3).value = s_result

        '8  ????       --> Over Wt.
        '9  ??       --> Draw
        s_content = "{ return  document.querySelectorAll('#racecardlist tbody tr td')[" & (27) + j * 27 + 9 & "].textContent.trim() }"
        s_result = driver.ExecuteScript(s_content)
        startCell.Offset(row_offset, 5).value = s_result

        '10  ???     --> Trainer
        s_content = "{ return  document.querySelectorAll('#racecardlist tbody tr td')[" & (27) + j * 27 + 10 & "].textContent.trim() }"
        s_result = driver.ExecuteScript(s_content)
        startCell.Offset(row_offset, 4).value = s_result

        '11  ??       --> Rtg.
        s_content = "{ return  document.querySelectorAll('#racecardlist tbody tr td')[" & (27) + j * 27 + 12 & "].textContent.trim() }"
        s_result = driver.ExecuteScript(s_content)
        startCell.Offset(row_offset, 8).value = s_result

        '12  ??+/-        --> Rtg.+/-
        '13  ????       --> Horse Wt. (Declaration)
        s_content = "{ return  document.querySelectorAll('#racecardlist tbody tr td')[" & (27) + j * 27 + 13 & "].textContent.trim() }"
        s_result = driver.ExecuteScript(s_content)
        startCell.Offset(row_offset, 9).value = s_result

        '14  ????+/-        --> Wt.+/- (vs Declaration)
        '15  ????       --> Best Time
        '16  ??       --> Age
        s_content = "{ return  document.querySelectorAll('#racecardlist tbody tr td')[" & (27) + j * 27 + 17 & "].textContent.trim() }"
        s_result = driver.ExecuteScript(s_content)
        startCell.Offset(row_offset, 6).value = s_result

        '17  ????       --> WFA
        '18  ??       --> Sex
        s_content = "{ return  document.querySelectorAll('#racecardlist tbody tr td')[" & (27) + j * 27 + 19 & "].textContent.trim() }"
        s_result = driver.ExecuteScript(s_content)
        startCell.Offset(row_offset, 10).value = s_result

        '19  ????       --> Season Stakes
        '20  ??????       --> Priority
        '21  ??????       --> Days since Last Run
        s_content = "{ return  document.querySelectorAll('#racecardlist tbody tr td')[" & (27) + j * 27 + 22 & "].textContent.trim() }"
        s_result = driver.ExecuteScript(s_content)
        startCell.Offset(row_offset, 12).value = s_result

        '22  ??       --> Gear
        s_content = "{ return  document.querySelectorAll('#racecardlist tbody tr td')[" & (27) + j * 27 + 23 & "].textContent.trim() }"
        s_result = driver.ExecuteScript(s_content)
        startCell.Offset(row_offset, 11).value = s_result

        '23  ??       --> Owner
        '24  ??       --> Sire
        s_content = "{ return  document.querySelectorAll('#racecardlist tbody tr td')[" & (27) + j * 27 + 25 & "].textContent.trim() }"
        s_result = driver.ExecuteScript(s_content)
        startCell.Offset(row_offset, 13).value = s_result

        '25  ??       --> Dam
        s_content = "{ return  document.querySelectorAll('#racecardlist tbody tr td')[" & (27) + j * 27 + 26 & "].textContent.trim() }"
        s_result = driver.ExecuteScript(s_content)
        startCell.Offset(row_offset, 14).value = s_result

        '26  ????       --> Import Cat.


        startCell.Offset(row_offset, 15).value = Split(race_summary_result, "|||")(1)
        startCell.Offset(row_offset, 16).value = Split(race_summary_result, "|||")(3)
        startCell.Offset(row_offset, 17).value = course_length
        startCell.Offset(row_offset, 18).value = course_name
        startCell.Offset(row_offset, 19).value = course_class
        startCell.Offset(row_offset, 20).value = course_condition
        startCell.Offset(row_offset, 21).value = mark_range
        startCell.Offset(row_offset, 22).value = Split(Split(Split(race_summary_result, "|||")(0), " - ")(0), " ")(2)


        not_used = Horse_aspx.ScrapeHorse(driver1, startCell.Offset(row_offset, 400).value, CStr(row_offset))

        not_used = TrackworkResult_aspx.Scrape(driver1, startCell.Offset(row_offset, 400).value, row_offset, s_date)

        row_offset = row_offset + 1
      End If

    Next j

    'not_used = LocalResults_aspx.ScrapeLocalResults(driver1, s_date, CStr(m), row_offset_start, actual_horse_count)
    'not_used = DisplaySectionalTime_aspx.ScrapeDisplaySectionalTime(driver1, s_date, CStr(m), row_offset_start, actual_horse_count)

  ' Next m
  Next race_link

  GoTo Done

ErrorHandler:
  MsgBox "Error: " & Err.Description

Done:
  driver.Quit
  driver1.Quit

  Common.ResumeScreenUpdate

  Common.TidySheet

End Sub



' write cell
Sub writeCell(cell_addres As String, value As String)

End Sub


Private Sub Take_ScreenShot_Desktop()
  Dim utils As New utils
  Dim driver As New ChromeDriver
  driver.Get "https://en.wikipedia.org/wiki/Main_Page"

  Dim img As Object

  'take a screenshot of the desktop
  Set img = utils.TakeScreenshot()

  'save the image in the folder of the workbook
  img.SaveAs ThisWorkbook.Path & "\sc-desktop.png"

  driver.Quit
End Sub





