Attribute VB_Name = "DisplaySectionalTime_aspx"

Option Explicit


Public Function ScrapeDisplaySectionalTime(driver As WebDriver, ByVal race_date As String, race_num As String, row_offset As Integer, ByVal horse_count As Integer)
  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 script_content, script_result As String

  Dim i, j As Integer


  On Error GoTo ErrorHandler
  
  ' Navigate to a website.
  Dim s_year, s_month, s_date, temp_new_date As String
  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")

  driver.Get "https://racing.hkjc.com/racing/information/chinese/Racing/DisplaySectionalTime.aspx?RaceDate=" & s_race_date & "&RaceNo=" & race_num
  

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



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


    If (table_count > 0) Then



  Dim hourse_table As Variant
  script_content = "{" & _
  "  list_e = [];" & _
  "  document" & _
  "    .querySelector('table.race_table')" & _
  "    .querySelectorAll('tr')" & _
  "    .forEach((e_tr, idx) => {" & _
  "      if (idx > 2) {" & _
  "        list_td = [];" & _
  "        temp = e_tr.querySelectorAll('td');" & _
  "        list_td.push(temp[2].textContent.replace(/[\(\)]/g,'').split(/\s/g)[1]);" & _
  "        list_td.push(temp[3]?.querySelectorAll('p')[1]?.textContent.replace(/\n/g, '')?.split(/\s+/g)[0] || '-');" & _
  "        list_td.push(temp[4]?.querySelectorAll('p')[1]?.textContent.replace(/\n/g, '')?.split(/\s+/g)[0] || '-');" & _
  "        list_td.push(temp[5]?.querySelectorAll('p')[1]?.textContent.replace(/\n/g, '')?.split(/\s+/g)[0] || '-');" & _
  "        list_td.push(temp[6]?.querySelectorAll('p')[1]?.textContent.replace(/\n/g, '')?.split(/\s+/g)[0] || '-');" & _
  "        list_td.push(temp[7]?.querySelectorAll('p')[1]?.textContent.replace(/\n/g, '')?.split(/\s+/g)[0] || '-');" & _
  "        list_td.push(temp[8]?.querySelectorAll('p')[1]?.textContent.replace(/\n/g, '')?.split(/\s+/g)[0] || '-');" & _
  "        list_td.push(temp[9]?.textContent);" & _
  "        list_e.push(list_td.join(','));" & _
  "      }" & _
  "    });" & _
  "  return list_e.join('|||'); " & _
  "}"
  script_result = driver.ExecuteScript(script_content)
  hourse_table = Split(script_result, "|||")

  'Country of origin
  ' document.querySelector('table.race_table').querySelectorAll('tr')[2+1].querySelectorAll('td')[0-9]
  For i = LBound(hourse_table) To UBound(hourse_table)
    For j = LBound(hourse_table) To UBound(hourse_table)
      Dim temp As Variant
      temp = Split(hourse_table(j), ",")
      Dim horse_id As String


      If (InStr(startCell.Offset(row_offset + i, 37).value, temp(0)) > 0) Then
        startCell.Offset(row_offset + i, 51 + 0).value = temp(1)
        startCell.Offset(row_offset + i, 51 + 1).value = temp(2)
        startCell.Offset(row_offset + i, 51 + 2).value = temp(3)
        startCell.Offset(row_offset + i, 51 + 3).value = temp(4)
        startCell.Offset(row_offset + i, 51 + 4).value = temp(5)
        startCell.Offset(row_offset + i, 51 + 5).value = temp(6)
        startCell.Offset(row_offset + i, 51 + 6).value = "'" & temp(7)
      End If
    Next j
  Next i


    Else
        For j = 0 To horse_count - 1
            ' table not ready yet
            startCell.Offset(row_offset + j, 30).value = "HKJC table not ready"
        Next j
    
    End If

  GoTo Done

ErrorHandler:
    MsgBox "hello error ?"
  MsgBox "Error: " & Err.Description

Done:
    Debug.Print "hello done"
  
  
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





