Attribute VB_Name = "LocalResults_aspx"

Option Explicit


Public Sub ScrapeLocalResults()
  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 not_used As Variant

  On Error GoTo ErrorHandler
  ResetCell
  
  Dim race_date As String
  Dim s_year, s_month, s_date, temp_new_date As String
  race_date = Common.readCell("AE1")
  
  s_year = Split(race_date, "/")(0)
  s_month = Split(race_date, "/")(1)
  s_date = Split(race_date, "/")(2)
  temp_new_date = s_date & "/" & s_month & "/" & s_year
  
  Dim s_race_date As String
  s_race_date = Format(temp_new_date, "DD/MM/YYYY")
  

  'ByVal race_date As String, race_num As String, row_offset As Integer, horse_count As Integer
  Dim row_offset As Integer
  row_offset = 0
    
  ' on error goto eh
  On Error GoTo ErrorHandler

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

  ' Set the path to the WebDriver executable.

  Dim CURRENT_DIR As String
  CURRENT_DIR = ThisWorkbook.Path
  driver.SetBinary CURRENT_DIR & "\Chromium\Application\chrome.exe"
  driver.AddArgument "--disable-blink-features=AutomationControlled --blink-settings=imagesEnabled=false"

  ' Start Chrome.
  driver.Start "chrome"

  
  Dim By As New By
  ' Navigate to a website.
  ' https://racing.hkjc.com/racing/information/Chinese/Racing/LocalResults.aspx?RaceDate=2024/04/20&Racecourse=ST&RaceNo=1
  driver.Get "https://racing.hkjc.com/racing/information/Chinese/Racing/LocalResults.aspx?RaceDate=" & s_race_date & "&RaceNo=1"

  ' document.querySelectorAll('#racecardlist tbody tr')[2].textContent

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

  Dim hourse_number As String
  Dim script_content, script_result As String
  Dim hourse_table As Variant


  ' test if match result table found
  Dim table_count, table_count_result As String
  table_count_result = driver.ExecuteScript("{ return  document.querySelectorAll('.performance').length }")
  table_count = CInt(table_count_result)

  ' find max race_num
  Dim max_race_num As Integer
  
  script_content = "{" & _
    "    let all_a = {};" & _
    "    all_a = document.querySelectorAll('.top_races a');" & _
    "    return all_a?.item(all_a?.length-2)?.href?.split('=')[3] || 0;" & _
    "}"
    

  max_race_num = CInt(driver.ExecuteScript(script_content))
  
  Dim i, j, k, l As Integer
  
  Dim current_row As Integer
  current_row = row_offset

  
  For l = 1 To max_race_num
    driver.Get "https://racing.hkjc.com/racing/information/Chinese/Racing/LocalResults.aspx?RaceDate=" & race_date & "&RaceNo=" & CStr(l)
    script_content = "{" & _
    "list_e = [];" & _
    "document" & _
    "  .querySelector('.performance table')" & _
    "  .querySelectorAll('tr')" & _
    "  .forEach((e_tr, idx) => {" & _
    "    if (idx > 0) {" & _
    "      let temp = e_tr.querySelectorAll('td');" & _
    "      let temp_l = [];" & _
    "      temp.forEach(e => temp_l.push(e.textContent.trim()));" & _
    "      list_e.push(" & _
    "        [" & _
    "          temp_l.join(',')," & _
    "          temp[9].textContent.replace(/[\n| ]+/g, '_').slice(1,-1)," & _
    "        ].join(',')" & _
    "      );" & _
    "    }" & _
    "  });" & _
    "return list_e.join('|||')" & _
    "}"
    script_result = driver.ExecuteScript(script_content)
    hourse_table = Split(script_result, "|||")
    
    Dim table_race_date, race_course, race_number, total_race_number, race_cource_desc As String
    table_race_date = driver.ExecuteScript("{ return document.querySelector('div.raceMeeting_select')?.querySelectorAll('p')[0]?.querySelectorAll('span')[0]?.textContent.split(/ +/g)[1] || '-'}")
    race_course = driver.ExecuteScript("{ return document.querySelector('div.raceMeeting_select')?.querySelectorAll('p')[0]?.querySelectorAll('span')[0]?.textContent.split(/ +/g)[3] || '-'}")
    race_number = driver.ExecuteScript("{ return document.querySelectorAll('div.race_tab td')[0]?.textContent?.split(/\s\(/g)[0] || '-'; }")
    total_race_number = driver.ExecuteScript("{ return document.querySelectorAll('div.race_tab td')[0]?.textContent?.split(/ /g)[3]?.replace(/[\(|\)]/g,'') || '-'; }")
    race_cource_desc = driver.ExecuteScript("{ return document.querySelector('div.race_tab')?.querySelectorAll('td')[11]?.textContent || '-'; } ")

    
    For i = LBound(hourse_table) To UBound(hourse_table)
        Dim temp As Variant
        temp = Split(hourse_table(i), ",")

        startCell.Offset(current_row, 30).value = table_race_date
        startCell.Offset(current_row, 31).value = race_course
        startCell.Offset(current_row, 32).value = race_number
        startCell.Offset(current_row, 33).value = total_race_number
        startCell.Offset(current_row, 34).value = race_cource_desc
        
        startCell.Offset(current_row, 37).value = temp(2)
        startCell.Offset(current_row, 38).value = temp(3)
        startCell.Offset(current_row, 39).value = temp(4)
        startCell.Offset(current_row, 40).value = "'" & temp(7)
        startCell.Offset(current_row, 41).value = temp(5)
        startCell.Offset(current_row, 42).value = temp(6)
        startCell.Offset(current_row, 35).value = temp(0)
        startCell.Offset(current_row, 36).value = temp(1)
        startCell.Offset(current_row, 49).value = "'" & temp(8)
        startCell.Offset(current_row, 50).value = temp(11)

        Dim temp_l As Variant
        temp_l = Split(temp(12), "_")
        For k = 0 To 5
          If (k >= UBound(temp_l)) Then
              startCell.Offset(current_row, 43 + k).value = "-"
          Else
                startCell.Offset(current_row, 43 + k).value = temp_l(k)
          End If
        Next k

        current_row = current_row + 1
    Next i
    
    
    Dim num_of_horse As Integer
    Dim race_row_end As Integer
    race_row_end = current_row - 1
    
    num_of_horse = UBound(hourse_table)
  
    not_used = DisplaySectionalTime_aspx.ScrapeDisplaySectionalTime(driver, race_date, CStr(l), race_row_end - num_of_horse, num_of_horse)

  Next l

  
  GoTo Done

ErrorHandler:
    MsgBox "hello error2"
    MsgBox "Error: " & Err.Description

Done:
    TidySheet
    
    driver.Quit
  
  
End Sub

Function ResetCell()
  Worksheets("Sheet1").Range("AE3:BF300").ClearContents
End Function

Function TidySheet()
  Worksheets("Sheet1").Range("OK1:OK200").ClearContents
  
  'widen column width
  Worksheets("Sheet1").Range("AE3:BF300").Columns.AutoFit
  
  ' align text to center
  Worksheets("Sheet1").Range("AE3:BF300").HorizontalAlignment = xlCenter
  
End Function


' 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



