Imports System.IO
Imports System.Threading.Tasks
Imports Tami.Pago.Core

Public Class HomeController
    Inherits Controller

    Private ReadOnly _pagoManager As TamiPagoManager

    Public Sub New()
        _pagoManager = New TamiPagoManager(StaticContents.ServiceEndpoint, StaticContents.MerchantId, StaticContents.TerminalId, StaticContents.SecretKey, StaticContents.FixedKidValue, StaticContents.FixedKValue)
    End Sub

    Public Function Index() As ActionResult
        Return View()
    End Function

    Public Async Function Auth() As Task(Of ActionResult)
        Dim request As New AuthRequest() With {
            .Amount = CInt(StaticContents.GetExampleBasket().GetTotalAmount()),
            .BillingAddress = StaticContents.BillingAddress,
            .ShippingAddress = StaticContents.ShippingAddress,
            .Basket = StaticContents.GetExampleBasket(),
            .Buyer = StaticContents.GetExampleBuyer(),
            .Card = StaticContents.GetExampleCard(),
            .OrderId = Guid.NewGuid().ToString("N"),
            .PaymentChannel = PaymentChannels.WEB
        }
        Dim response = Await _pagoManager.AuthAsync(request)
        Return Json(response, JsonRequestBehavior.AllowGet)
    End Function

    Public Async Function AuthWithBonus() As Task(Of ActionResult)
        Dim request As New AuthWithBonusRequest With {
        .Amount = CType(StaticContents.GetExampleBasket().GetTotalAmount(), Integer),
        .BillingAddress = StaticContents.BillingAddress,
        .ShippingAddress = StaticContents.ShippingAddress,
        .Basket = StaticContents.GetExampleBasket(),
        .Buyer = StaticContents.GetExampleBuyer(),
        .Card = StaticContents.GetExampleCard(),
        .OrderId = Guid.NewGuid().ToString("N"),
        .PaymentChannel = PaymentChannels.WEB,
        .IsRewardToBeUsed = True,
        .RewardToBeUsed = New RewardToBeUsed() With {
            .List = New List(Of RewardItem) From {
                New RewardItem() With {
                    .Type = "BNS",
                    .Amount = 10
                    }
                }
            }
        }

        Dim response = Await _pagoManager.AuthWithBonusAsync(request)
        Return Json(response, JsonRequestBehavior.AllowGet)
    End Function

    Public Async Function PreAuth() As Task(Of ActionResult)
        Dim request As New PreAuthRequest() With {
            .Amount = CInt(StaticContents.GetExampleBasket().GetTotalAmount()),
            .BillingAddress = StaticContents.BillingAddress,
            .ShippingAddress = StaticContents.ShippingAddress,
            .Basket = StaticContents.GetExampleBasket(),
            .Buyer = StaticContents.GetExampleBuyer(),
            .Card = StaticContents.GetExampleCard(),
            .OrderId = Guid.NewGuid().ToString("N"),
            .PaymentChannel = PaymentChannels.WEB
        }
        Dim response = Await _pagoManager.PreAuthAsync(request)
        Return Json(response, JsonRequestBehavior.AllowGet)
    End Function

    Public Async Function PostAuth() As Task(Of ActionResult)
        Dim orderId = Guid.NewGuid().ToString("N")
        Dim preAuthRequest As New PreAuthRequest() With {
            .Amount = CInt(StaticContents.GetExampleBasket().GetTotalAmount()),
            .BillingAddress = StaticContents.BillingAddress,
            .ShippingAddress = StaticContents.ShippingAddress,
            .Basket = StaticContents.GetExampleBasket(),
            .Buyer = StaticContents.GetExampleBuyer(),
            .Card = StaticContents.GetExampleCard(),
            .OrderId = orderId,
            .PaymentChannel = PaymentChannels.WEB
        }
        Dim preAuthResponse = Await _pagoManager.PreAuthAsync(preAuthRequest)

        Dim postAuthRequest As New PostAuthRequest() With {
            .Amount = 40,
            .OrderId = orderId
        }
        Dim postAuthResponse = Await _pagoManager.PostAuthAsync(postAuthRequest)
        Return Json(postAuthResponse, JsonRequestBehavior.AllowGet)
    End Function

    Public Async Function Reverse() As Task(Of ActionResult)
        Dim orderId = Guid.NewGuid().ToString("N")

        Dim authRequest As New AuthRequest() With {
            .Amount = CInt(StaticContents.GetExampleBasket().GetTotalAmount()),
            .BillingAddress = StaticContents.BillingAddress,
            .ShippingAddress = StaticContents.ShippingAddress,
            .Basket = StaticContents.GetExampleBasket(),
            .Buyer = StaticContents.GetExampleBuyer(),
            .Card = StaticContents.GetExampleCard(),
            .OrderId = orderId,
            .PaymentChannel = PaymentChannels.WEB
        }
        Dim authResponse = Await _pagoManager.AuthAsync(authRequest)

        Dim reverseRequest As New ReverseRequest() With {
            .Amount = 10,
            .OrderId = orderId
        }
        Dim reverseResponse = Await _pagoManager.ReverseAsync(reverseRequest)
        Return Json(reverseResponse, JsonRequestBehavior.AllowGet)
    End Function

    Public Async Function Auth3D() As Task(Of ActionResult)
        Dim request As New ThreeDAuthRequest() With {
            .Amount = CInt(StaticContents.GetExampleBasket().GetTotalAmount()),
            .BillingAddress = StaticContents.BillingAddress,
            .ShippingAddress = StaticContents.ShippingAddress,
            .Basket = StaticContents.GetExampleBasket(),
            .Buyer = StaticContents.GetExampleBuyer(),
            .Card = StaticContents.GetExampleCard(),
            .OrderId = Guid.NewGuid().ToString("N"),
            .PaymentChannel = PaymentChannels.WEB,
            .CallbackUrl = "https://localhost:7229/ThreeDResponse"
        }
        Dim response = Await _pagoManager.Auth3DAsync(request)
        Return Json(response, JsonRequestBehavior.AllowGet)
    End Function

    Public Async Function PreAuth3D() As Task(Of ActionResult)
        Dim request As New ThreeDPreAuthRequest() With {
            .Amount = CInt(StaticContents.GetExampleBasket().GetTotalAmount()),
            .BillingAddress = StaticContents.BillingAddress,
            .ShippingAddress = StaticContents.ShippingAddress,
            .Basket = StaticContents.GetExampleBasket(),
            .Buyer = StaticContents.GetExampleBuyer(),
            .Card = StaticContents.GetExampleCard(),
            .OrderId = Guid.NewGuid().ToString("N"),
            .PaymentChannel = PaymentChannels.WEB,
            .CallbackUrl = "https://localhost:7229/ThreeDResponse"
        }
        Dim response = Await _pagoManager.PreAuth3DAsync(request)
        Return Json(response, JsonRequestBehavior.AllowGet)
    End Function

    Public Async Function AuthComplete3D() As Task(Of ActionResult)
        Dim orderId = Guid.NewGuid().ToString("N")

        Dim request As New ThreeDAuthRequest() With {
            .Amount = CInt(StaticContents.GetExampleBasket().GetTotalAmount()),
            .BillingAddress = StaticContents.BillingAddress,
            .ShippingAddress = StaticContents.ShippingAddress,
            .Basket = StaticContents.GetExampleBasket(),
            .Buyer = StaticContents.GetExampleBuyer(),
            .Card = StaticContents.GetExampleCard(),
            .OrderId = orderId,
            .PaymentChannel = PaymentChannels.WEB,
            .CallbackUrl = "https://localhost:7229/ThreeDResponse"
        }
        Dim authResponse = Await _pagoManager.Auth3DAsync(request)

        Dim complete3DRequest As New Complete3dAuthRequest() With {
            .OrderId = orderId
        }
        Dim response = Await _pagoManager.Complete3DAuthAsync(complete3DRequest)
        Return Json(response, JsonRequestBehavior.AllowGet)
    End Function

    Public Async Function Query(orderId As String, isTransactionDetail As Boolean) As Task(Of ActionResult)
        Dim request As New QueryRequest()
        request.OrderId = orderId
        request.IsTransactionDetail = isTransactionDetail

        Dim response = Await _pagoManager.QueryAsync(request)
        Return Json(response, JsonRequestBehavior.AllowGet)
    End Function

    Public Async Function InstallmentInfo(binNumber As Integer) As Task(Of ActionResult)
        Dim request As New InstallmentInfoRequest()
        request.BinNumber = binNumber

        Dim response = Await _pagoManager.InstallmentInfoAsync(request)
        Return Json(response, JsonRequestBehavior.AllowGet)
    End Function

    Public Async Function BinInfo(binNumber As Integer) As Task(Of ActionResult)
        Dim request As New BinInfoRequest()
        request.BinNumber = binNumber

        Dim response = Await _pagoManager.BINInfoAsync(request)
        Return Json(response, JsonRequestBehavior.AllowGet)
    End Function

    Public Async Function BonusInquiry() As Task(Of ActionResult)
        Dim request As New BonusInquiryRequest() With {
        .IpAddress = "1.1.111.111",
        .EmailAddress = "cem@cem.com",
        .CardNumber = "5549600754609018",
        .ExpireMonth = 10,
        .ExpireYear = 27,
        .Cvv = "",
        .CurrencyCode = 949,
        .Amount = 10
    }

        Dim response = Await _pagoManager.BonusInquiryAsync(request)
        Return Json(response, JsonRequestBehavior.AllowGet)
    End Function

    Public Async Function ThreeDResponse() As Task(Of ActionResult)
        Dim requestBody As String
        Using reader As New StreamReader(Request.InputStream, Encoding.UTF8)
            requestBody = Await reader.ReadToEndAsync()
        End Using

        Response.StatusCode = 200
        Response.ContentType = "text/plain"
        Response.Write(requestBody)

        Return Content(requestBody, "text/plain")
    End Function
End Class