Imports InovarMais.Core.Extensions.StringExt
Imports i9.Dashboard

Public Class SingleBarGraphFornecedorAnalogo
    Inherits System.Web.UI.Page

    Public Property Titutlo As String

    Public Property Cor As String

    Public Property TheData As String

    Public Property style As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim sessionID = Request.QueryString("ID_Graf")

        If Not SessionH.GrafSession.ContainsKey(sessionID) Then Exit Sub

        Dim session = SessionH.GrafSession(sessionID)

        Titutlo = session.Titutlo

        Cor = session.Cor

        style = "width:" & session.GWidth - 20 & "px; height:" & session.GHeight - 35 & "px;"

        BuildMulti(session)
    End Sub

    Private Sub BuildMulti(g As GraphCashe)
        Dim fullObjList = New List(Of String)

        If g.DataSource.Count = 0 Then Exit Sub
        If g.DataSource.First.DicValues.Count = 0 Then Exit Sub

        fullObjList.Add("['" & g.Titutlo & "'," & String.Join(",", g.DataSource.First.DicValues.Keys.Select(Function(x) "'" & x & "'")) & "]")

        For Each d In g.DataSource.OrderBy(Function(x) x.SerieName)
            Dim newListVals = New List(Of String)

            For Each l In d.DicValues
                newListVals.Add(Math.Round(l.Value, 2, MidpointRounding.AwayFromZero).ToString.Replace(",", "."))
            Next

            fullObjList.Add("['" & d.SerieName & "'," & String.Join(",", newListVals) & "]")

        Next

        TheData = "[" & String.Join(",", fullObjList) & "]"
    End Sub

End Class