﻿Imports System.Net
Imports System.IO
Imports System.Web.Script.Serialization

Namespace SSActivewear

    Public Class API

        Public Class Request
            Public Sub New()
                Method = ""
                Host = "https://api.ssactivewear.com"
                URLPath = ""
                CustomerNumber = "YourCustomerNumber"
                APIKey = "YourAPIKey"
                ContentType = "application/json"

            End Sub

            Public Property Method As String
            Public Property Host As String
            Public Property URLPath As String
            Public Property CustomerNumber As String
            Public Property APIKey As String
            Public Property ContentType As String

            Dim serializer As New JavaScriptSerializer

            '-------------------------Products-------------------------

            Public Sub GET_Categories()
                Method = "GET"
                URLPath = "/v2/categories/71"

                Dim request As HttpWebRequest = WebRequest.Create(Host & URLPath)
                request.Method = Method
                request.Credentials = New NetworkCredential(CustomerNumber, APIKey)

                Dim Result As String = ""
                Dim ErrorText As String = ""

                Dim Categories As New List(Of Category)

                Try
                    Dim response As HttpWebResponse = request.GetResponse
                    Dim StreamReader As New StreamReader(response.GetResponseStream())
                    Result = StreamReader.ReadToEnd

                    If response.StatusCode = HttpStatusCode.OK Then
                        Categories = serializer.Deserialize(Of List(Of Category))(Result)
                    Else
                        ErrorText = Result
                    End If

                Catch ex As Exception
                    ErrorText = ex.Message
                End Try

                If ErrorText = "" Then
                    For i = 0 To Categories.Count - 1
                        Dim CategoryName As String = Categories(i).name
                    Next
                End If
            End Sub

            Public Sub GET_Styles()
                Method = "GET"
                URLPath = "/v2/styles/00760"

                Dim request As HttpWebRequest = WebRequest.Create(Host & URLPath)
                request.Method = Method
                request.Credentials = New NetworkCredential(CustomerNumber, APIKey)

                Dim Result As String = ""
                Dim ErrorText As String = ""

                Dim Styles As New List(Of Style)

                Try
                    Dim response As HttpWebResponse = request.GetResponse
                    Dim StreamReader As New StreamReader(response.GetResponseStream())
                    Result = StreamReader.ReadToEnd

                    If response.StatusCode = HttpStatusCode.OK Then
                        Styles = serializer.Deserialize(Of List(Of Style))(Result)
                    Else
                        ErrorText = Result
                    End If
                Catch ex As Exception
                    ErrorText = ex.Message
                End Try

                If ErrorText = "" Then
                    For i = 0 To Styles.Count - 1
                        Dim StyleName As String = Styles(i).styleName
                    Next
                End If
            End Sub

            Public Sub GET_Products()
                Method = "GET"
                URLPath = "/v2/products/B00760003"

                Dim request As HttpWebRequest = WebRequest.Create(Host & URLPath)
                request.Method = Method
                request.Credentials = New NetworkCredential(CustomerNumber, APIKey)

                Dim Result As String = ""
                Dim ErrorText As String = ""

                Dim Products As New List(Of Sku)

                Try
                    Dim response As HttpWebResponse = request.GetResponse
                    Dim StreamReader As New StreamReader(response.GetResponseStream())
                    Result = StreamReader.ReadToEnd

                    If response.StatusCode = HttpStatusCode.OK Then
                        Products = serializer.Deserialize(Of List(Of Sku))(Result)
                    Else
                        ErrorText = Result
                    End If
                Catch ex As Exception
                    ErrorText = ex.Message
                End Try

                If ErrorText = "" Then
                    For i = 0 To Products.Count - 1
                        Dim StyleName As String = Products(i).styleName
                    Next
                End If
            End Sub

            Public Sub GET_Specs()
                Method = "GET"
                URLPath = "/v2/specs/39"

                Dim request As HttpWebRequest = WebRequest.Create(Host & URLPath)
                request.Method = Method
                request.Credentials = New NetworkCredential(CustomerNumber, APIKey)

                Dim Result As String = ""
                Dim ErrorText As String = ""

                Dim Specs As New List(Of Spec)

                Try
                    Dim response As HttpWebResponse = request.GetResponse
                    Dim StreamReader As New StreamReader(response.GetResponseStream())
                    Result = StreamReader.ReadToEnd

                    If response.StatusCode = HttpStatusCode.OK Then
                        Specs = serializer.Deserialize(Of List(Of Spec))(Result)
                    Else
                        ErrorText = Result
                    End If
                Catch ex As Exception
                    ErrorText = ex.Message
                End Try

                If ErrorText = "" Then
                    For i = 0 To Specs.Count - 1
                        Dim StyleName As String = Specs(i).styleName
                    Next
                End If
            End Sub

            '-------------------------Orders-------------------------

            Public Sub GET_Orders()
                Method = "GET"
                URLPath = "/v2/orders/"

                Dim request As HttpWebRequest = WebRequest.Create(Host & URLPath)
                request.Method = Method
                request.Credentials = New NetworkCredential(CustomerNumber, APIKey)

                Dim Result As String = ""
                Dim ErrorText As String = ""

                Dim OrderHistory As New List(Of OrderHistory.Header)

                Try
                    Dim response As HttpWebResponse = request.GetResponse
                    Dim StreamReader As New StreamReader(response.GetResponseStream())
                    Result = StreamReader.ReadToEnd

                    If response.StatusCode = HttpStatusCode.OK Then
                        OrderHistory = serializer.Deserialize(Of List(Of OrderHistory.Header))(Result)
                    Else
                        ErrorText = Result
                    End If
                Catch ex As Exception
                    ErrorText = ex.Message
                End Try

                If ErrorText = "" Then
                    For i = 0 To OrderHistory.Count - 1
                        Dim OrderNumber As String = OrderHistory(i).orderNumber
                    Next
                End If
            End Sub

            Public Sub POST_Orders()
                Method = "POST"
                URLPath = "/v2/Orders"

                Dim Order As New Order

                Order.shippingAddress = New Address
                Order.shippingAddress.customer = "Company ABC"
                Order.shippingAddress.attn = "John Doe"
                Order.shippingAddress.address = "123 Main St"
                Order.shippingAddress.city = "Bollingbrook"
                Order.shippingAddress.state = "IL"
                Order.shippingAddress.zip = "60440"
                Order.shippingAddress.residential = True

                Order.shippingMethod = "1"
                Order.shipBlind = False
                Order.poNumber = "Test"
                Order.emailConfirmation = ""
                Order.testOrder = True

                Order.lines = New List(Of Order.Line)
                Order.lines.Add(New Order.Line)
                Order.lines(0).warehouseAbbr = "IL"
                Order.lines(0).identifier = "B00760003"
                Order.lines(0).qty = 2


                Dim request As HttpWebRequest = WebRequest.Create(Host & URLPath)
                request.Method = Method
                request.Credentials = New NetworkCredential(CustomerNumber, APIKey)
                request.ContentType = ContentType

                Try
                    Using streamWriter = New StreamWriter(request.GetRequestStream())
                        Dim json As String = serializer.Serialize(Order)
                        streamWriter.Write(json)
                        streamWriter.Flush()
                        streamWriter.Close()
                    End Using
                Catch ex As Exception

                End Try


                Dim Result As String = ""
                Dim ErrorText As String = ""

                Dim OrderHistory As New List(Of OrderHistory.Header)

                Try
                    Dim response As HttpWebResponse = request.GetResponse
                    Dim StreamReader As New StreamReader(response.GetResponseStream())
                    Result = StreamReader.ReadToEnd

                    If response.StatusCode = HttpStatusCode.OK Then
                        OrderHistory = serializer.Deserialize(Of List(Of OrderHistory.Header))(Result)
                    Else
                        ErrorText = Result
                    End If
                Catch ex As Exception
                    ErrorText = ex.Message
                End Try

                If ErrorText = "" Then
                    Dim ConfirmationNumber As String = OrderHistory(0).orderNumber
                End If

            End Sub

            '-------------------------Cross Ref-------------------------

            Public Sub GET_CrossRef()
                Method = "GET"
                URLPath = "/v2/crossref/"

                Dim request As HttpWebRequest = WebRequest.Create(Host & URLPath)
                request.Method = Method
                request.Credentials = New NetworkCredential(CustomerNumber, APIKey)

                Dim Result As String = ""
                Dim ErrorText As String = ""

                Dim CrossRef As New List(Of CrossRef)

                Try
                    Dim response As HttpWebResponse = request.GetResponse
                    Dim StreamReader As New StreamReader(response.GetResponseStream())
                    Result = StreamReader.ReadToEnd

                    If response.StatusCode = HttpStatusCode.OK Then
                        CrossRef = serializer.Deserialize(Of List(Of CrossRef))(Result)
                    Else
                        ErrorText = Result
                    End If
                Catch ex As Exception
                    ErrorText = ex.Message
                End Try

                If ErrorText = "" Then
                    For i = 0 To CrossRef.Count - 1
                        Dim YourSku As String = CrossRef(i).yourSku
                    Next
                End If
            End Sub

            Public Sub PUT_CrossRef()
                Method = "PUT"

                Dim YourSku As String = "ABC"
                Dim Identifier As String = "B00760503"

                URLPath = "/v2/crossref/" & YourSku & "?Identifier=" & Identifier

                Dim request As HttpWebRequest = WebRequest.Create(Host & URLPath)
                request.Method = Method
                request.Credentials = New NetworkCredential(CustomerNumber, APIKey)
                request.ContentLength = 0

                Dim Result As String = ""
                Dim ErrorText As String = ""
                Try
                    Dim response As HttpWebResponse = request.GetResponse
                    Dim StreamReader As New StreamReader(response.GetResponseStream())
                    If response.StatusCode = HttpStatusCode.OK Then
                        'Record Updated
                    ElseIf response.StatusCode = HttpStatusCode.Created Then
                        'Record Created
                    End If

                Catch ex As Exception
                    ErrorText = ex.Message
                End Try


            End Sub

            Public Sub DELETE_CrossRef()
                Method = "DELETE"

                Dim YourSku As String = "ABC"

                URLPath = "/v2/crossref/" & YourSku

                Dim request As HttpWebRequest = WebRequest.Create(Host & URLPath)
                request.Method = Method
                request.Credentials = New NetworkCredential(CustomerNumber, APIKey)

                Dim Result As String = ""
                Dim ErrorText As String = ""
                Try
                    Dim response As HttpWebResponse = request.GetResponse
                    Dim StreamReader As New StreamReader(response.GetResponseStream())
                    If response.StatusCode = HttpStatusCode.NoContent Then
                        'Record Deleted 
                    End If
                Catch ex As Exception
                    ErrorText = ex.Message
                End Try


            End Sub

        End Class

        Public Class Order

            Public Sub New()
                shippingMethod = Nothing
                shipBlind = Nothing
                poNumber = Nothing
                emailConfirmation = Nothing
                testOrder = Nothing
            End Sub
            Public Property shippingAddress As Address
            Public Property shippingMethod As String
            Public Property shipBlind As Boolean?
            Public Property poNumber As String
            Public Property emailConfirmation As String
            Public Property testOrder As Boolean?
            Public Property CreditCard As CC
            Public Property lines As List(Of Line)

            Public Class Line
                Public Property warehouseAbbr As String
                Public Property identifier As String
                Public Property qty As Integer?

            End Class

            Public Class CC
                Public Property cardNumber As String
                Public Property expMonth As String
                Public Property expYear As String
                Public Property billingZipCode As String
                Public Property billingStreetNumber As String

            End Class

        End Class

        Public Class OrderHistory

            Public Class Header

                Public Property orderHeaderID As Integer

                Public Property guid As Guid?
                Public Property companyName As String
                Public Property warehouseAbbr As String
                Public Property orderNumber As String
                Public Property invoiceNumber As String
                Public Property poNumber As String

                Public Property customerNumber As String

                Public Property orderDate As Date?
                Public Property shipDate As Date?
                Public Property invoiceDate As Date?
                Public Property orderType As String
                Public Property terms As String
                Public Property orderStatus As String

                Public Property dropship As Boolean?
                Public Property shippingCarrier As String
                Public Property shippingMethod As String
                Public Property shipBlind As Boolean?
                Public Property shippingCollectNumber As String
                Public Property trackingNumber As String

                Public Property shippingAddress As Address


                Public Property subtotal As Decimal?
                Public Property shipping As Decimal?
                Public Property cod As Decimal?
                Public Property tax As Decimal?
                Public Property smallOrderFee As Decimal?
                Public Property cuponDiscount As Decimal?
                Public Property sampleDiscount As Decimal?
                Public Property setUpFee As Decimal?
                Public Property restockFee As Decimal?
                Public Property debitCredit As Decimal?
                Public Property total As Decimal?

                Public Property totalPieces As Integer?
                Public Property totalLines As Integer?
                Public Property totalWeight As Decimal?
                Public Property totalBoxes As Integer?

                Public Property lines As List(Of Line)

                Public Class Line

                    Public Sub New()
                    End Sub


                    Public Property lineNumber As Integer?
                    Public Property type As String
                    Public Property skuID As Integer?
                    Public Property sku As String
                    Public Property gtin As String
                    Public Property yourSku As String

                    Public Property qtyOrdered As Integer?
                    Public Property qtyShipped As Integer?
                    Public Property price As Decimal?

                    Public Property brandName As String
                    Public Property styleName As String
                    Public Property title As String
                    Public Property colorName As String
                    Public Property sizeName As String


                End Class

            End Class

        End Class

        Public Class Address

            Public Sub New()
                customer = Nothing
                attn = Nothing
                residential = Nothing
            End Sub

            Public Property customer As String
            Public Property attn As String
            Public Property address As String
            Public Property city As String
            Public Property state As String
            Public Property zip As String
            Public Property residential As Boolean?

        End Class

        Public Class Category
            Public Property categoryID As Integer?
            Public Property name As String
            Public Property image As String

        End Class

        Public Class Spec
            Public Property specID As Integer?
            Public Property styleID As Integer?
            Public Property partNumber As String
            Public Property brandName As String
            Public Property styleName As String
            Public Property sizeName As String
            Public Property sizeOrder As String
            Public Property specName As String
            Public Property value As String

        End Class

        Public Class Style
            Public Property styleID As Integer?
            Public Property partNumber As String
            Public Property brandName As String
            Public Property styleName As String
            Public Property title As String
            Public Property description As String
            Public Property baseCategory As String
            Public Property categories As String
            Public Property catalogPageNumber As String
            Public Property newStyle As Boolean?
            Public Property comparableGroup As Integer?
            Public Property companionGroup As Integer?
            Public Property brandImage As String
            Public Property styleImage As String
            Public Property specs As List(Of Spec)

        End Class

        Public Class Sku
            Public Property sku As String
            Public Property gtin As String
            Public Property yourSku As String

            Public Property styleID As Integer?
            Public Property brandName As String
            Public Property styleName As String

            Public Property colorName As String
            Public Property colorCode As String
            Public Property colorPriceCodeName As String
            Public Property colorGroup As String
            Public Property colorFamily As String
            Public Property colorSwatchImage As String
            Public Property colorSwatchTextColor As String
            Public Property colorFrontImage As String
            Public Property colorSideImage As String
            Public Property colorBackImage As String
            Public Property color1 As String
            Public Property color2 As String

            Public Property sizeName As String
            Public Property sieCode As String
            Public Property sizeOrder As String
            Public Property sizePriceCodeName As String

            Public Property caseQty As Integer?
            Public Property unitWeight As Decimal?

            Public Property piecePrice As Decimal?
            Public Property dozenPrice As Decimal?
            Public Property casePrice As Decimal?
            Public Property salePrice As Decimal?
            Public Property customerPrice As Decimal?
            Public Property saleExpiration As Date?

            Public Property warehouses As List(Of Warehouse)

        End Class

        Public Class Warehouse
            Public Property warehouseAbbr As String
            Public Property skuID As Integer?
            Public Property qty As Integer?
            Public Property closeout As Boolean?
            Public Property dropship As Boolean?
            Public Property excludeFreeFreight As Boolean?
        End Class

        Public Class CrossRef
            Public Property customerID As Integer?
            Public Property yourSku As String
            Public Property skuID As Integer?
            Public Property sku As String
            Public Property gtin As String
            Public Property brandName As String
            Public Property styleName As String
            Public Property colorName As String
            Public Property sizeName As String

        End Class

    End Class

End Namespace
