package com.shopify.sample.ui;

import android.content.Intent;
import android.os.Bundle;
import android.test.AndroidTestCase;

import com.shopify.buy.dataprovider.BuyClientFactory;
import com.shopify.buy.dataprovider.BuyClient;

/**
 * Created by krisorr on 2015-07-14.
 */
public class ProductDetailsBuilderTest extends AndroidTestCase {

    private static final String WEB_RETURN_TO_LABEL = "Go Back";
    private static final String WEB_RETURN_TO_URL = "myapp://";

    protected BuyClient buyClient;

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        buyClient = BuyClientFactory.getBuyClient(getShopDomain(), getApiKey(), getAppId(), getApplicationName());
    }

    public void testBuild() {
        Intent intent = new ProductDetailsBuilder(this.getContext())
                .setShopDomain(getShopDomain())
                .setApiKey(getApiKey())
                .setAppId(getAppId())
                .setApplicationName(getApplicationName())
                .setProductId(getProductId())
                .setWebReturnToLabel(WEB_RETURN_TO_LABEL)
                .setWebReturnToUrl(WEB_RETURN_TO_URL)
                .build();

        validateIntent(intent);
    }

    public void testBuildWithBuyClient() {
        Intent intent = new ProductDetailsBuilder(getContext(), buyClient)
                .setProductId(getProductId())
                .setWebReturnToLabel(WEB_RETURN_TO_LABEL)
                .setWebReturnToUrl(WEB_RETURN_TO_URL)
                .build();

        validateIntent(intent);
    }

    public void testBuildWithMissingShopDomain() {
        try {
            Intent intent = new ProductDetailsBuilder(getContext())
                    .setApiKey(getApiKey())
                    .setAppId(getAppId())
                    .setApplicationName(getApplicationName())
                    .setProductId(getProductId())
                    .build();

            fail("Expected IllegalArgumentException");
        } catch (Exception e) {
        }

    }

    public void testBuildWithMissingApiKey() {
        try {
            Intent intent = new ProductDetailsBuilder(getContext())
                    .setShopDomain(getShopDomain())
                    .setAppId(getAppId())
                    .setApplicationName(getApplicationName())
                    .setProductId(getProductId())
                    .build();

            fail("Expected IllegalArgumentException");
        } catch (Exception e) {
        }
    }

    public void testBuildWithMissingApplicationName() {
        try {
            Intent intent = new ProductDetailsBuilder(getContext())
                    .setShopDomain(getShopDomain())
                    .setApiKey(getApiKey())
                    .setAppId(getAppId())
                    .setProductId(getProductId())
                    .build();

            fail("Expected IllegalArgumentException");
        } catch (Exception e) {
        }
    }

    private void validateIntent(Intent intent) {
        assertNotNull(intent);

        Bundle bundle = intent.getExtras();

        assertEquals(bundle.getString(ProductDetailsConfig.EXTRA_SHOP_DOMAIN), getShopDomain());
        assertEquals(bundle.getString(ProductDetailsConfig.EXTRA_SHOP_API_KEY), getApiKey());
        assertEquals(bundle.getString(ProductDetailsConfig.EXTRA_SHOP_PRODUCT_ID), getProductId());
        assertEquals(bundle.getString(ProductDetailsConfig.EXTRA_WEB_RETURN_TO_LABEL), WEB_RETURN_TO_LABEL);
        assertEquals(bundle.getString(ProductDetailsConfig.EXTRA_WEB_RETURN_TO_URL), WEB_RETURN_TO_URL);
        assertEquals(bundle.getString(ProductDetailsConfig.EXTRA_SHOP_APPLICATION_NAME), getApplicationName());
    }

    public String getShopDomain() {
        return "placeholder.myshopify.com";
    }

    public String getApiKey() {
        return "placeholderApiKey";
    }

    public String getAppId() {
        return "placeholderAppId";
    }

    public String getProductId() {
        return "2096063363";
    }

    public String getApplicationName() {
        return "MobileBuyTest";
    }
}

@@ -1,8 +1,1154 @@
+/*%0A * The MIT License (MIT)%0A *%0A * Copyright (c) 2015 Shopify Inc.%0A *%0A * Permission is hereby granted, free of charge, to any person obtaining a copy%0A * of this software and associated documentation files (the %22Software%22), to deal%0A * in the Software without restriction, including without limitation the rights%0A * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell%0A * copies of the Software, and to permit persons to whom the Software is%0A * furnished to do so, subject to the following conditions:%0A *%0A * The above copyright notice and this permission notice shall be included in%0A * all copies or substantial portions of the Software.%0A *%0A * THE SOFTWARE IS PROVIDED %22AS IS%22, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR%0A * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,%0A * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE%0A * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER%0A * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,%0A * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN%0A * THE SOFTWARE.%0A */%0A%0A
 package 
@@ -1311,23 +1311,16 @@
 uyClient
-Factory
 ;%0Aimport
@@ -1362,55 +1362,17 @@
 ient
-;%0A%0A/**%0A * Created by krisorr on 2015-07-14.%0A */
+Builder;%0A
 %0Apub
@@ -1703,16 +1703,20 @@
 Client =
+ new
  BuyClie
@@ -1721,91 +1721,213 @@
 ient
-Factory.getBuyClient(getShopDomain(), getApiKey(), getAppId(), getApplicationName()
+Builder()%0A                .shopDomain(getShopDomain())%0A                .apiKey(getApiKey())%0A                .appId(getAppId())%0A                .applicationName(getApplicationName())%0A                .build(
 );%0A 

fix test that depended on BuyClientFactory