package com.braintreepayments.api;

import android.content.Context;
import android.os.Bundle;

import com.samsung.android.sdk.samsungpay.v2.PartnerInfo;
import com.samsung.android.sdk.samsungpay.v2.SamsungPay;
import com.samsung.android.sdk.samsungpay.v2.SpaySdk;
import com.samsung.android.sdk.samsungpay.v2.StatusListener;
import com.samsung.android.sdk.samsungpay.v2.payment.CardInfo;
import com.samsung.android.sdk.samsungpay.v2.payment.CustomSheetPaymentInfo;
import com.samsung.android.sdk.samsungpay.v2.payment.PaymentManager;
import com.samsung.android.sdk.samsungpay.v2.payment.sheet.CustomSheet;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.samsung.android.sdk.samsungpay.v2.SpaySdk.PARTNER_SERVICE_TYPE;
import static com.samsung.android.sdk.samsungpay.v2.SpaySdk.ServiceType.INAPP_PAYMENT;
import static com.samsung.android.sdk.samsungpay.v2.payment.PaymentManager.EXTRA_KEY_TEST_MODE;

public class SamsungPayInternalClient {

    private static final String BRAINTREE_TOKENIZATION_API_VERSION = "2018-10-01";

    private final SamsungPay samsungPay;
    private final PaymentManager paymentManager;

    SamsungPayInternalClient(Context context, Configuration configuration, String sessionId, String integrationType) throws JSONException {
//        PartnerInfo partnerInfo = new SamsungPartnerInfoBuilder()
//                .setConfiguration(configuration)
//                .setSessionId(sessionId)
//                .setIntegrationType(integrationType)
//                .build();


        Bundle bundle = new Bundle();
        bundle.putString(PARTNER_SERVICE_TYPE, INAPP_PAYMENT.toString());

        boolean isTestEnvironment = false;
        String samsungPayEnvironment = configuration.getSamsungPayEnvironment();
        if (samsungPayEnvironment != null) {
            isTestEnvironment = samsungPayEnvironment.equalsIgnoreCase("SANDBOX");
        }
        bundle.putBoolean(EXTRA_KEY_TEST_MODE, isTestEnvironment);

        JSONObject clientSdkMetadata = new MetadataBuilder()
                .integration(integrationType)
                .sessionId(sessionId)
                .version()
                .build();

        JSONObject additionalData = new JSONObject();
        additionalData.put("braintreeTokenizationApiVersion", BRAINTREE_TOKENIZATION_API_VERSION);
        additionalData.put("clientSdkMetadata", clientSdkMetadata);
        bundle.putString("additionalData", additionalData.toString());

        String serviceId = configuration.getSamsungPayServiceId();
        PartnerInfo partnerInfo = new PartnerInfo(serviceId, bundle);

        this.samsungPay = new SamsungPay(context, partnerInfo);
        this.paymentManager = new PaymentManager(context, partnerInfo);
    }

    void getSamsungPayStatus(final GetSamsungPayStatusCallback callback) {
        samsungPay.getSamsungPayStatus(new StatusListener() {
            @Override
            public void onSuccess(int statusCode, Bundle bundle) {
                callback.onResult(statusCode, null);
            }

            @Override
            public void onFail(int errorCode, Bundle bundle) {
                SamsungPayException exception = new SamsungPayException(errorCode);
                callback.onResult(null, exception);
            }
        });
    }

    void getAcceptedCardBrands(final GetAcceptedCardBrandsCallback callback) {
        paymentManager.requestCardInfo(new Bundle(), new PaymentManager.CardInfoListener() {
            @Override
            public void onResult(final List<CardInfo> cardInfos) {
                List<SpaySdk.Brand> result = new ArrayList<>();
                if (cardInfos != null) {
                    for (CardInfo cardInfo : cardInfos) {
                        result.add(cardInfo.getBrand());
                    }
                }
                callback.onResult(result, null);
            }

            @Override
            public void onFailure(int errorCode, Bundle bundle) {
                Exception error = new SamsungPayException(errorCode);
                callback.onResult(null, error);
            }
        });
    }

    public void startSamsungPay(CustomSheetPaymentInfo customSheetPaymentInfo, final StartSamsungPayCallback callback) {
        paymentManager.startInAppPayWithCustomSheet(customSheetPaymentInfo, new PaymentManager.CustomSheetTransactionInfoListener() {
            @Override
            public void onCardInfoUpdated(CardInfo cardInfo, CustomSheet customSheet) {
                // TODO: notify merchant card info updated
            }

            @Override
            public void onSuccess(CustomSheetPaymentInfo customSheetPaymentInfo, String s, Bundle bundle) {
                try {
                    JSONObject json = new JSONObject(s);
                    SamsungPayNonce samsungPayNonce = SamsungPayNonce.fromJSON(json);
                    callback.onResult(samsungPayNonce, null);
                } catch (JSONException e) {
                    callback.onResult(null, e);
                }
            }

            @Override
            public void onFailure(int errorCode, Bundle bundle) {
                if (errorCode == SpaySdk.ERROR_USER_CANCELED) {
                    UserCanceledException userCanceledError = new UserCanceledException("User Canceled");
                    callback.onResult(null, userCanceledError);
                } else {
                    SamsungPayException samsungPayError = new SamsungPayException(errorCode);
                    callback.onResult(null, samsungPayError);
                }
            }
        });
    }

    public void goToSamsungPayUpdatePage() {
        samsungPay.goToUpdatePage();
    }

    public void activateSamsungPay() {
        samsungPay.activateSamsungPay();
    }
}

@@ -1334,18 +1334,16 @@
 ption %7B%0A
-//
         
@@ -1400,18 +1400,16 @@
 ilder()%0A
-//
         
@@ -1449,18 +1449,16 @@
 ration)%0A
-//
         
@@ -1490,18 +1490,16 @@
 sionId)%0A
-//
         
@@ -1543,18 +1543,16 @@
 onType)%0A
-//
         
@@ -1574,1082 +1574,8 @@
 );%0A%0A
-%0A        Bundle bundle = new Bundle();%0A        bundle.putString(PARTNER_SERVICE_TYPE, INAPP_PAYMENT.toString());%0A%0A        boolean isTestEnvironment = false;%0A        String samsungPayEnvironment = configuration.getSamsungPayEnvironment();%0A        if (samsungPayEnvironment != null) %7B%0A            isTestEnvironment = samsungPayEnvironment.equalsIgnoreCase(%22SANDBOX%22);%0A        %7D%0A        bundle.putBoolean(EXTRA_KEY_TEST_MODE, isTestEnvironment);%0A%0A        JSONObject clientSdkMetadata = new MetadataBuilder()%0A                .integration(integrationType)%0A                .sessionId(sessionId)%0A                .version()%0A                .build();%0A%0A        JSONObject additionalData = new JSONObject();%0A        additionalData.put(%22braintreeTokenizationApiVersion%22, BRAINTREE_TOKENIZATION_API_VERSION);%0A        additionalData.put(%22clientSdkMetadata%22, clientSdkMetadata);%0A        bundle.putString(%22additionalData%22, additionalData.toString());%0A%0A        String serviceId = configuration.getSamsungPayServiceId();%0A        PartnerInfo partnerInfo = new PartnerInfo(serviceId, bundle);%0A%0A
     

Integrate SamsunPartnerInfoBuilder with SamsungPayInternalClient.