package io.voucherify.android.demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.math.BigDecimal;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import io.voucherify.android.client.VoucherifyAndroidClient;
import io.voucherify.android.client.VoucherifyUtils;
import io.voucherify.android.client.exception.VoucherifyError;
import io.voucherify.android.client.model.Customer;
import io.voucherify.android.client.model.Order;
import io.voucherify.android.client.model.ValidationContext;
import io.voucherify.android.client.model.VoucherRedemptionResult;
import io.voucherify.android.client.model.VoucherResponse;
import io.voucherify.android.view.OnValidatedListener;
import io.voucherify.android.view.VoucherCheckoutView;
import okhttp3.logging.HttpLoggingInterceptor;


public class MainActivity extends AppCompatActivity {

    private EditText etProductPrice;
    private TextView tvDiscount;
    private TextView tvNewPrice;
    private VoucherCheckoutView voucherCheckout;

    private VoucherifyAndroidClient voucherifyClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        voucherifyClient = new VoucherifyAndroidClient.Builder(
                "c7306167-294a-4c06-aa5b-d81ee8155a00",
                "07bca261-3bd3-49cc-a58a-8ffce2d20d1b")
                .withCustomTrackingId("demo-android")
                .setLogLevel(HttpLoggingInterceptor.Level.BODY)
                .build();

        tvDiscount = (TextView) findViewById(R.id.tv_discount);
        tvNewPrice = (TextView) findViewById(R.id.tv_new_price);
        etProductPrice = (EditText) findViewById(R.id.et_product_price);
        voucherCheckout = (VoucherCheckoutView) findViewById(R.id.voucher_checkout);
        voucherCheckout.setVoucherifyClient(voucherifyClient);
        voucherCheckout.setOnValidatedListener(new OnValidatedListener() {
            @Override
            public void onValid(final VoucherResponse result) {
                updateDiscountDetails(result);
                etProductPrice.setOnKeyListener(new View.OnKeyListener() {
                    @Override
                    public boolean onKey(View view, int i, KeyEvent keyEvent) {
                        updateDiscountDetails(result);
                        return false;
                    }
                });
            }

            @Override
            public void onInvalid(final VoucherResponse result) {
                tvDiscount.setText(null);
                tvNewPrice.setText(null);
                etProductPrice.setOnKeyListener(null);
                voucherCheckout.setVoucherErrorMessage(String.format(getString(R.string.errorVoucherMessage), result.getReason()));
            }

            @Override
            public void onError(VoucherifyError error) {
                voucherCheckout.setVoucherErrorMessage(error.getMessage());
            }
        });

        tryRx();
    }

    private void updateDiscountDetails(VoucherResponse result) {
        try {
            Double originalPrice = Double.valueOf(etProductPrice.getText().toString());
            BigDecimal newPrice = VoucherifyUtils.calculatePrice(BigDecimal.valueOf(originalPrice), result, null);
            BigDecimal discount = VoucherifyUtils.calculateDiscount(BigDecimal.valueOf(originalPrice), result, null);

            tvDiscount.setText(String.format(getString(R.string.discountTitle), discount.toString()));
            tvNewPrice.setText(String.format(getString(R.string.priceAfterDiscountTitle), newPrice.toString()));
        } catch (Exception e) {/* ignoring wrong etProductPrice value */}
    }

    private void tryRx(){
        Customer customer = new Customer();
        customer.setEmail("123@aasda.coco");
        customer.setName("Olo Bolo");
        voucherifyClient.validations().rx().validate("test_code_test")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<VoucherResponse>() {
                    @Override
                    public void accept(VoucherResponse voucherResponse) throws Exception {
                        Log.d(MainActivity.class.getName(), voucherResponse.toString());
                    }

                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) {
                    }
                });

        voucherifyClient.validations().rx().validate("test_code_test", new ValidationContext(customer, new Order(100, null)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<VoucherResponse>() {
                    @Override
                    public void accept(VoucherResponse voucherResponse) throws Exception {
                        Log.d(MainActivity.class.getName(), voucherResponse.toString());
                    }

                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) {
                    }
                });

        voucherifyClient.validations().rx().validate("test_code_test", ValidationContext.create(100))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<VoucherResponse>() {
                    @Override
                    public void accept(VoucherResponse voucherResponse) throws Exception {
                        Log.d(MainActivity.class.getName(), voucherResponse.toString());
                    }

                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) {
                    }
                });

        voucherifyClient.redemptions().rx().redeem("test_code_test")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<VoucherRedemptionResult>() {
                    @Override
                    public void accept(VoucherRedemptionResult voucherResponse) throws Exception {
                        Log.d(MainActivity.class.getName(), voucherResponse.toString());
                    }

                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) {
                    }
                });
    }

}

@@ -109,33 +109,8 @@
 ty;%0A
-import android.util.Log;%0A
 impo
@@ -131,24 +131,24 @@
 w.KeyEvent;%0A
+
 import andro
@@ -260,149 +260,8 @@
 l;%0A%0A
-import io.reactivex.android.schedulers.AndroidSchedulers;%0Aimport io.reactivex.functions.Consumer;%0Aimport io.reactivex.schedulers.Schedulers;%0A
 impo
@@ -437,237 +437,8 @@
 or;%0A
-import io.voucherify.android.client.model.Customer;%0Aimport io.voucherify.android.client.model.Order;%0Aimport io.voucherify.android.client.model.ValidationContext;%0Aimport io.voucherify.android.client.model.VoucherRedemptionResult;%0A
 impo
@@ -2850,26 +2850,8 @@
  %7D);
-%0A%0A        tryRx();
 %0A   
@@ -3549,2880 +3549,12 @@
 */%7D%0A
+
     %7D%0A
-%0A    private void tryRx()%7B%0A        Customer customer = new Customer();%0A        customer.setEmail(%22123@aasda.coco%22);%0A        customer.setName(%22Olo Bolo%22);%0A        voucherifyClient.validations().rx().validate(%22test_code_test%22)%0A                .subscribeOn(Schedulers.io())%0A                .observeOn(AndroidSchedulers.mainThread())%0A                .subscribe(new Consumer%3CVoucherResponse%3E() %7B%0A                    @Override%0A                    public void accept(VoucherResponse voucherResponse) throws Exception %7B%0A                        Log.d(MainActivity.class.getName(), voucherResponse.toString());%0A                    %7D%0A%0A                %7D, new Consumer%3CThrowable%3E() %7B%0A                    @Override%0A                    public void accept(Throwable throwable) %7B%0A                    %7D%0A                %7D);%0A%0A        voucherifyClient.validations().rx().validate(%22test_code_test%22, new ValidationContext(customer, new Order(100, null)))%0A                .subscribeOn(Schedulers.io())%0A                .observeOn(AndroidSchedulers.mainThread())%0A                .subscribe(new Consumer%3CVoucherResponse%3E() %7B%0A                    @Override%0A                    public void accept(VoucherResponse voucherResponse) throws Exception %7B%0A                        Log.d(MainActivity.class.getName(), voucherResponse.toString());%0A                    %7D%0A%0A                %7D, new Consumer%3CThrowable%3E() %7B%0A                    @Override%0A                    public void accept(Throwable throwable) %7B%0A                    %7D%0A                %7D);%0A%0A        voucherifyClient.validations().rx().validate(%22test_code_test%22, ValidationContext.create(100))%0A                .subscribeOn(Schedulers.io())%0A                .observeOn(AndroidSchedulers.mainThread())%0A                .subscribe(new Consumer%3CVoucherResponse%3E() %7B%0A                    @Override%0A                    public void accept(VoucherResponse voucherResponse) throws Exception %7B%0A                        Log.d(MainActivity.class.getName(), voucherResponse.toString());%0A                    %7D%0A%0A                %7D, new Consumer%3CThrowable%3E() %7B%0A                    @Override%0A                    public void accept(Throwable throwable) %7B%0A                    %7D%0A                %7D);%0A%0A        voucherifyClient.redemptions().rx().redeem(%22test_code_test%22)%0A                .subscribeOn(Schedulers.io())%0A                .observeOn(AndroidSchedulers.mainThread())%0A                .subscribe(new Consumer%3CVoucherRedemptionResult%3E() %7B%0A                    @Override%0A                    public void accept(VoucherRedemptionResult voucherResponse) throws Exception %7B%0A                        Log.d(MainActivity.class.getName(), voucherResponse.toString());%0A                    %7D%0A%0A                %7D, new Consumer%3CThrowable%3E() %7B%0A                    @Override%0A                    public void accept(Throwable throwable) %7B%0A                    %7D%0A                %7D);%0A    %7D%0A%0A
 %7D%0A

Remove examples