using Base: Number
using CSV: eltype
using Dash 

using OrderedCollections, DataFrames   

data = OrderedDict(
      "Date" => ["2015-01-01", "2015-10-24", "2016-05-10", "2017-01-10", "2018-05-10", "2018-08-15"],
      "Region" => ["Montreal", "Toronto", "New York City", "Miami", "San Francisco", "London"],
      "Temperature" => [1, -20, 3.512, 4, 10423, -441.2],
      "Humidity" => [10, 20, 30, 40, 50, 60],
      "Pressure" => [2, 10924, 3912, -10, 3591.2, 15],
)

df = DataFrame(data) 

df[!,"id"] = 1:size(df)[1]
app = dash()

function highlight_max_row(df)
  df1 = select(df, Not(:id))
  nm = names(df1)
  new_nm = String[]
  for (idx, ec) in enumerate(eachcol(df1))
    if (eltype(ec) <: Number)
      push!(new_nm,nm[idx])
    end
  end
    df_numeric_columns = df[!,new_nm]
  locs = let x = Symbol[]
    map(eachrow(df_numeric_columns)) do rw
      push!(x, argmax(rw))
    end
    x
  end
    return [
        Dict(
            "if" =>  Dict(
                "filter_query" =>  "{id} = $idx",
                "column_id" =>  col
            ),
            "backgroundColor" =>  "#3D9970",
            "color" =>  "white"
        )
        for (idx,col) in enumerate(locs)
    ]
end
app.layout = dash_datatable(
    data = map(eachrow(df)) do r
      Dict(names(r) .=> values(r))
    end,
    sort_action="native",
    columns=[Dict("name" =>c, "id" => c) for c in names(df) if c!= "id"],
    style_data_conditional=highlight_max_row(df)
    
)

run_server(app, "0.0.0.0", debug=true)
