package ch.pizzaleu.android.fragment.createOrder;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.braintreepayments.api.dropin.DropInActivity;
import com.braintreepayments.api.dropin.DropInRequest;
import com.braintreepayments.api.dropin.DropInResult;
import com.braintreepayments.api.models.PayPalRequest;
import com.google.android.gms.wallet.Cart;
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog;
import com.wdullaer.materialdatetimepicker.time.TimePickerDialog;
import com.wdullaer.materialdatetimepicker.time.Timepoint;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ch.pizzaleu.android.R;
import ch.pizzaleu.android.activity.BaseActivity;
import ch.pizzaleu.android.activity.CreateOrderActivity;
import ch.pizzaleu.android.adapter.recycler.OrderPriceRecyclerAdapter;
import ch.pizzaleu.android.adapter.recycler.ProductOnOrderSummaryRecyclerAdapter;
import ch.pizzaleu.android.api.ApiConstants;
import ch.pizzaleu.android.api.ApiEndPoints;
import ch.pizzaleu.android.api.ApiErrorUtils;
import ch.pizzaleu.android.api.ApiService;
import ch.pizzaleu.android.api.ResponseObject;
import ch.pizzaleu.android.helper.DialogHelper;
import ch.pizzaleu.android.helper.PriceHelper;
import ch.pizzaleu.android.helper.SessionHelper;
import ch.pizzaleu.android.helper.SharedPrefsHelper;
import ch.pizzaleu.android.model.PaymentMethodModel;
import ch.pizzaleu.android.model.PaymentTokenModel;
import ch.pizzaleu.android.model.StoreShiftModel;
import ch.pizzaleu.android.model.cart.CartInfoModel;
import ch.pizzaleu.android.model.cart.CartProductModel;
import ch.pizzaleu.android.model.cart.CartTotalModel;
import ch.pizzaleu.android.view.AppInfoView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by cimenmus on 17/10/2017.
 */

public class CreateOrderSummaryFragment extends CreateOrderBaseFragment {

    @BindView(R.id.orderPersonFullnamePizzalinkInfoLayout) AppInfoView orderPersonFullnamePizzalinkInfoLayout;
    @BindView(R.id.orderShippingMethodPizzalinkInfoLayout) AppInfoView orderShippingMethodPizzalinkInfoLayout;
    @BindView(R.id.orderShippingAddressPizzalinkInfoLayout) AppInfoView orderShippingAddressPizzalinkInfoLayout;
    @BindView(R.id.orderPaymentMethodPizzalinkInfoLayout) AppInfoView orderPaymentMethodPizzalinkInfoLayout;
    //@BindView(R.id.orderTotalPizzalinkInfoLayout) AppInfoView orderTotalPizzalinkInfoLayout;
    @BindView(R.id.orderPriceRecyclerView) RecyclerView orderPriceRecyclerView;
    @BindView(R.id.orderProductsRecyclerView) RecyclerView orderProductsRecyclerView;
    @BindView(R.id.orderNotePizzalinkInfoLayout) AppInfoView orderNotePizzalinkInfoLayout;

    @BindString(R.string.confirm_order) String confirmOrderText;
    @BindString(R.string.slice_pizza_for_store) String slicePizzaForStoreText;
    @BindString(R.string.slice_pizza) String yesText;
    @BindString(R.string.do_not_slice_pizza) String noText;
    @BindString(R.string.restaurant_out_of_order) String restaurantOutOfOrderText;
    @BindString(R.string.choose_pick_up_date_time) String choosePickUpDateTimeText;
    @BindString(R.string.restaurant_out_of_order_on_selected_date) String restaurantOutOfOrderOnSelectedDateText;
    @BindString(R.string.something_went_wrong) String genericErrorText;
    @BindString(R.string.unknown_payment_method) String unknownPaymentMethodText;
    @BindString(R.string.payment_canceled) String paymentCanceledText;
    @BindString(R.string.andorid_pay_not_found) String androidPayNotFoundText;
    @BindString(R.string.choose) String choosetext;
    @BindString(R.string.alert) String alertText;
    @BindString(R.string.delivery_date) String deliveryDateText;
    @BindString(R.string.delivery_time) String deliveryTimeText;

    public static final java.lang.String FRAGMENT_NAME = "orderSummaryFragment";
    private final int REQUEST_CODE_BRAINTREE_PAYMENT = 3784;
    private CreateOrderActivity createOrderActivity;
    private String dateOfOrderString, timeOfOrder;
    private Calendar dateOfOrder;
    private ArrayList<CartTotalModel> cartTotalList = new ArrayList<>();
    private OrderPriceRecyclerAdapter orderPriceRecyclerAdapter;
    private ProductOnOrderSummaryRecyclerAdapter productOnOrderSummaryRecyclerAdapter;
    private StoreShiftModel storeShiftModel;
    private PaymentTokenModel braintreePaymentTokenModel;

    public CreateOrderSummaryFragment() {}

    public static CreateOrderSummaryFragment newInstance() {
        return new CreateOrderSummaryFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_create_order_summary, container, false);
        ButterKnife.bind(this, view);
        initViews();
        getCartProductsWithCommission();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        createBraintreePaymentToken();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_BRAINTREE_PAYMENT) {
            if (resultCode == Activity.RESULT_OK) {
                DropInResult result = data.getParcelableExtra(DropInResult.EXTRA_DROP_IN_RESULT);
                createOrderActivity.setSelectedPaymentMethodForBraintree(result.getPaymentMethodType());
                createBraintreePayment(result);
                // use the result to update your UI and send the payment method nonce to your server
            } else if (resultCode == Activity.RESULT_CANCELED) {
                // the user canceled
                Log.i("fjehfhreuf", "vjfjvjfv");
                DialogHelper.showAlertDialog(BaseActivity.currentActivity, paymentCanceledText);
            } else {
                // handle errors here, an exception may be available in
                Exception error = (Exception) data.getSerializableExtra(DropInActivity.EXTRA_ERROR);
                DialogHelper.showAlertDialog(BaseActivity.currentActivity, error.getMessage());
            }
        }
    }

    @OnClick({R.id.previousTextView, R.id.nextTextView})
    protected void onClick(View view){
        CreateOrderActivity createOrderActivity = (CreateOrderActivity) BaseActivity.currentActivity;
        switch (view.getId()){
            case R.id.previousTextView:
                createOrderActivity.onPreviousClicked(FRAGMENT_NAME);
                break;
            case R.id.nextTextView:
                checkOrderPrice();
                break;
        }
    }

    private void initViews(){

        nextTextView.setText(confirmOrderText);

        createOrderActivity = (CreateOrderActivity) BaseActivity.currentActivity;

        orderPersonFullnamePizzalinkInfoLayout.setText(SessionHelper.getUser().getFullname());
        orderShippingMethodPizzalinkInfoLayout.setText(createOrderActivity.getSelectedShippingMethod().getTitle());
        orderShippingAddressPizzalinkInfoLayout.setText(createOrderActivity.getSelectedShippingAddress().getAddress());
        orderPaymentMethodPizzalinkInfoLayout.setText(createOrderActivity.getSelectedPaymentMethod().getTitle());
        //orderTotalPizzalinkInfoLayout.setText(PriceHelper.roundFractions(createOrderActivity.getCartInfo().getCartTotalModel().getText()));
        if(createOrderActivity.getOrderNote().trim().isEmpty()){
            orderNotePizzalinkInfoLayout.setVisibility(View.GONE);
        }
        else {
            orderNotePizzalinkInfoLayout.setVisibility(View.VISIBLE);
            orderNotePizzalinkInfoLayout.setText(createOrderActivity.getOrderNote());
        }

        initOrderPriceRecyclerView();
        initOrderProductsRecyclerView();

    }

    private void initOrderPriceRecyclerView(){
        orderPriceRecyclerAdapter = new OrderPriceRecyclerAdapter(cartTotalList);
        orderPriceRecyclerView.setLayoutManager(new LinearLayoutManager(BaseActivity.currentActivity));
        orderPriceRecyclerView.setNestedScrollingEnabled(false);
        orderPriceRecyclerView.setAdapter(orderPriceRecyclerAdapter);
    }

    private void initOrderProductsRecyclerView(){
        productOnOrderSummaryRecyclerAdapter = new ProductOnOrderSummaryRecyclerAdapter(createOrderActivity.getCartInfo().getProducts());
        orderProductsRecyclerView.setLayoutManager(new LinearLayoutManager(BaseActivity.currentActivity));
        orderProductsRecyclerView.setNestedScrollingEnabled(false);
        orderProductsRecyclerView.setAdapter(productOnOrderSummaryRecyclerAdapter);
    }

    private void checkOrderPrice(){

        DialogHelper.showLoadingDialog();

        HashMap<String, Object> params = new HashMap<>();
        params.put("customer_id", SessionHelper.getUser().getId());
        params.put("shipping_method_title", createOrderActivity.getSelectedShippingMethod().getTitle());
        params.put("shipping_method_code", createOrderActivity.getSelectedShippingMethod().getCode());
        params.put("address_id", createOrderActivity.getSelectedShippingAddress().getId());
        params.put("payment_method_title", createOrderActivity.getSelectedPaymentMethod().getTitle());
        params.put("payment_method_code", createOrderActivity.getSelectedPaymentMethod().getCode());
        params.put("comment", createOrderNote());
        params.put("is_pizza_pass_campaign", createOrderActivity.getCartInfo().isPizzapassCampaignUsed());
        params.put("is_kebap_pass_campaign", createOrderActivity.getCartInfo().isKebappassCampaignUsed());
        params.put("application_id", ApiConstants.APP_TYPE_ID_ANDROID);
        if(SessionHelper.getSelectedCoupon() != null){
            params.put("coupon", SessionHelper.getSelectedCoupon().getCode());
        }
        if(dateOfOrderString != null && !dateOfOrderString.isEmpty() &&
                timeOfOrder != null && !timeOfOrder.isEmpty()){
            params.put("delivery_date", dateOfOrderString + " " + timeOfOrder);
        }

        Call<ResponseObject> call = ApiService.apiInterface.checkOrderPrice(
                "/" + SessionHelper.getSelectedStore().getStoreName() + ApiEndPoints.API_CHECK_ORDER_PRICE + SessionHelper.getCustomerToken().getToken(), params);
        call.enqueue(new Callback<ResponseObject>() {
            @Override
            public void onResponse(Call<ResponseObject> call, Response<ResponseObject> response) {
                if(response.isSuccessful() && response.body().isSuccess()){
                    checkDeliveryTimeForPreferenceDialog();
                }
                else{
                    DialogHelper.hideLoadingDialog();
                    ApiErrorUtils.parseError(response);
                }
            }

            @Override
            public void onFailure(Call<ResponseObject> call, Throwable t) {
                DialogHelper.hideLoadingDialog();
                DialogHelper.showFailedDialog();
            }
        });

    }

    private void checkDeliveryTime(){
        ApiService
                .apiInterface
                .checkDeliveryTime(
                        SessionHelper.getSelectedStore().getStoreName(),
                        createOrderActivity.getSelectedShippingMethod().getCode())
                .enqueue(new Callback<ResponseObject<Boolean>>() {
                    @Override
                    public void onResponse(Call<ResponseObject<Boolean>> call, Response<ResponseObject<Boolean>> response) {
                        if(response.isSuccessful() &&
                                response.body() != null){

                            if(response.body().isSuccess()){
                                redirectToPayment();
                            }
                            else {
                                getDeliveryTimeOfStore(restaurantOutOfOrderText);
                                /*
                                DialogHelper.hideLoadingDialog();
                                showRestaurantOutOfOrderDialog(restaurantOutOfOrderText);
                                */
                            }

                        }
                        else{
                            DialogHelper.hideLoadingDialog();
                            ApiErrorUtils.parseError(response);
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseObject<Boolean>> call, Throwable t) {
                        DialogHelper.hideLoadingDialog();
                        DialogHelper.showFailedDialog();
                    }
                });
    }

    private void checkDeliveryTimeForPreferenceDialog(){
        ApiService
                .apiInterface
                .checkDeliveryTime(
                        SessionHelper.getSelectedStore().getStoreName(),
                        createOrderActivity.getSelectedShippingMethod().getCode())
                .enqueue(new Callback<ResponseObject<Boolean>>() {
                    @Override
                    public void onResponse(Call<ResponseObject<Boolean>> call, Response<ResponseObject<Boolean>> response) {
                        DialogHelper.hideLoadingDialog();
                        if(response.isSuccessful() && response.body() != null){

                            if(response.body().isSuccess()){
                                showDeliveryTimePreferenceDialogWithTwoButtons();
                            }
                            else {
                                showDeliveryTimePreferenceDialogWithOneButton();
                            }

                        }
                        else{
                            ApiErrorUtils.parseError(response);
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseObject<Boolean>> call, Throwable t) {
                        DialogHelper.hideLoadingDialog();
                        DialogHelper.showFailedDialog();
                    }
                });
    }

    private void showDeliveryTimePreferenceDialogWithTwoButtons(){
        DialogHelper.showTwoButtonsDialog(
                getString(R.string.info),
                getString(R.string.delivery_time_preference_message),
                R.string.delivery_time_preference_as_soon_as_possible,
                new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        DialogHelper.showLoadingDialog();
                        checkDeliveryTime();
                    }
                },
                R.string.delivery_time_preference_choose_delivery_time,
                new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        DialogHelper.showLoadingDialog();
                        getDeliveryTimeOfStore(null);
                    }
                },
                false,
                true
        );

    }

    private void showDeliveryTimePreferenceDialogWithOneButton(){
        DialogHelper.showOneButtonDialogWithCallback(
                getString(R.string.info),
                getString(R.string.delivery_time_preference_message),
                getString(R.string.delivery_time_preference_choose_delivery_time),
                new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        DialogHelper.showLoadingDialog();
                        getDeliveryTimeOfStore(null);
                    }
                },
                false,
                true
        );
    }

    private void getDeliveryTimeOfStore(final String dialogMessage){
        ApiService
                .apiInterface
                .getDeliveryTimeOfStore(
                        SessionHelper.getSelectedStore().getStoreName(),
                        createOrderActivity.getSelectedShippingMethod().getCode())
                .enqueue(new Callback<ResponseObject<StoreShiftModel>>() {
                    @Override
                    public void onResponse(Call<ResponseObject<StoreShiftModel>> call, Response<ResponseObject<StoreShiftModel>> response) {
                        DialogHelper.hideLoadingDialog();
                        if(response.isSuccessful() &&
                                response.body() != null){

                            if(response.body().isSuccess()){
                                storeShiftModel = response.body().getData();
                                if(dialogMessage == null || dialogMessage.isEmpty()){
                                    showDatePickerDialog();
                                }
                                else {
                                    showRestaurantOutOfOrderDialog(dialogMessage);
                                }
                            }
                            else {
                                DialogHelper.showAlertDialog(BaseActivity.currentActivity, genericErrorText);
                            }

                        }
                        else{
                            DialogHelper.hideLoadingDialog();
                            ApiErrorUtils.parseError(response);
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseObject<StoreShiftModel>> call, Throwable t) {
                        DialogHelper.hideLoadingDialog();
                        DialogHelper.showFailedDialog();
                    }
                });
    }

    private void checkDeliveryTimeWithDateAndTime(){
        DialogHelper.showLoadingDialog();
        String deliveryDateAndTime = dateOfOrderString + " " + timeOfOrder;
        ApiService
                .apiInterface
                .checkDeliveryTimeWithDateAndTime(
                        SessionHelper.getSelectedStore().getStoreName(),
                        deliveryDateAndTime,
                        createOrderActivity.getSelectedShippingMethod().getCode())
                .enqueue(new Callback<ResponseObject<Boolean>>() {
                    @Override
                    public void onResponse(Call<ResponseObject<Boolean>> call, Response<ResponseObject<Boolean>> response) {
                        DialogHelper.hideLoadingDialog();
                        if(response.isSuccessful() &&
                                response.body() != null){

                            if(response.body().isSuccess()){
                                redirectToPayment();
                            }
                            else {
                                showRestaurantOutOfOrderDialog(restaurantOutOfOrderOnSelectedDateText);
                            }

                        }
                        else{
                            ApiErrorUtils.parseError(response);
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseObject<Boolean>> call, Throwable t) {
                        DialogHelper.hideLoadingDialog();
                        DialogHelper.showFailedDialog();
                    }
                });
    }

    private void showRestaurantOutOfOrderDialog(String message){

        DialogHelper.showTwoButtonsDialog(
                alertText,
                message,
                R.string.ok,
                new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        showDatePickerDialog();
                    }
                },
                R.string.cancel_all_caps,
                new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        dialog.dismiss();
                    }
                }
        );
    }

    private void showDatePickerDialog() {

        Calendar nowDate = Calendar.getInstance();
        final int nowYear = nowDate.get(Calendar.YEAR);
        final int nowMonth = nowDate.get(Calendar.MONTH);
        final int nowDay = nowDate.get(Calendar.DAY_OF_MONTH);

        if(nowDate.get(Calendar.HOUR_OF_DAY) == 23){
            nowDate.add(Calendar.DAY_OF_YEAR, 1);
        }

        DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {

                        String yearOfOrder = String.valueOf(year);
                        String monthOfOrder = String.valueOf(monthOfYear + 1);
                        String dayOfOrder = String.valueOf(dayOfMonth);

                        if(monthOfOrder.length() == 1){
                            monthOfOrder = "0" + monthOfOrder;
                        }

                        if(dayOfOrder.length() == 1){
                            dayOfOrder = "0" + dayOfOrder;
                        }

                        dateOfOrder = Calendar.getInstance();
                        dateOfOrder.set(year, monthOfYear, dayOfMonth);

                        dateOfOrderString = dayOfOrder + "-" + monthOfOrder + "-" + yearOfOrder;
                        showTimePickerDialog();
                    }
                },
                nowYear,
                nowMonth,
                nowDay
        );

        datePickerDialog.setSelectableDays(generateEnabledDays());

        datePickerDialog.vibrate(false);
        datePickerDialog.setMinDate(nowDate);
        //datePickerDialog.autoDismiss(true);
        datePickerDialog.show(BaseActivity.currentActivity.getFragmentManager(), choosetext);
    }

    private void showTimePickerDialog(){

        Calendar nowDate = Calendar.getInstance();

        boolean isTheSameDate =
                dateOfOrder.get(Calendar.YEAR) == nowDate.get(Calendar.YEAR) &&
                dateOfOrder.get(Calendar.MONTH) == nowDate.get(Calendar.MONTH) &&
                dateOfOrder.get(Calendar.DAY_OF_MONTH) == nowDate.get(Calendar.DAY_OF_MONTH);

        if(isTheSameDate &&
                createOrderActivity.getSelectedShippingMethod().getCode().toLowerCase().equals(ApiConstants.SHIPPING_METHOD_CODE_FREE_SHIPPING)){
            nowDate.add( Calendar.MINUTE, 65);
        }
        else if(isTheSameDate &&
                createOrderActivity.getSelectedShippingMethod().getCode().toLowerCase().equals(ApiConstants.SHIPPING_METHOD_CODE_PICK_UP_FROM_STORE)){
            nowDate.add( Calendar.MINUTE, 25);
        }
        else if(isTheSameDate){
            nowDate.add( Calendar.MINUTE, 10);
        }

        int orderYear = dateOfOrder.get(Calendar.YEAR);
        int orderMonth = dateOfOrder.get(Calendar.MONTH);
        int orderDay = dateOfOrder.get(Calendar.DAY_OF_MONTH);

        int nowYear = nowDate.get(Calendar.YEAR);
        int nowMonth = nowDate.get(Calendar.MONTH);
        int nowDay = nowDate.get(Calendar.DAY_OF_MONTH);

        int nowHour = nowDate.get(Calendar.HOUR_OF_DAY);
        int nowMinute = nowDate.get(Calendar.MINUTE);
        int nowSecond = nowDate.get(Calendar.SECOND);

        int selectedDayIndex = dateOfOrder.get(Calendar.DAY_OF_WEEK);
        ArrayList<String> workingParts = new ArrayList<>();
        switch (selectedDayIndex){
            case 1:
                workingParts = storeShiftModel.getSundayShift();
                break;
            case 2:
                workingParts = storeShiftModel.getMondayShift();
                break;
            case 3:
                workingParts = storeShiftModel.getTuesdayShift();
                break;
            case 4:
                workingParts = storeShiftModel.getWednesdayShift();
                break;
            case 5:
                workingParts = storeShiftModel.getThursdayShift();
                break;
            case 6:
                workingParts = storeShiftModel.getFridayShift();
                break;
            case 7:
                workingParts = storeShiftModel.getSaturdayShift();
                break;
        }

        if(orderYear == nowYear && orderMonth == nowMonth && orderDay == nowDay){
            ArrayList<String> realPartsOfToday = new ArrayList<>();
            for(String part : workingParts){
                if(isPartAvailable(part)){
                    realPartsOfToday.add(part);
                }
            }
            if(!realPartsOfToday.isEmpty()){
                workingParts.clear();
                workingParts.addAll(realPartsOfToday);
            }
        }

        TimePickerDialog timePickerDialog =
                TimePickerDialog.newInstance(
                        new TimePickerDialog.OnTimeSetListener() {
                            @Override
                            public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second) {

                                String hourOfOrder = String.valueOf(hourOfDay);
                                String minuteOfOrder = String.valueOf(minute);

                                if(hourOfOrder.length() == 1){
                                    hourOfOrder = "0" + hourOfOrder;
                                }

                                if(minuteOfOrder.length() == 1){
                                    minuteOfOrder = "0" + minuteOfOrder;
                                }

                                timeOfOrder = hourOfOrder + ":" + minuteOfOrder;
                                checkDeliveryTimeWithDateAndTime();
                                //redirectToPayment();
                            }
                        },
                        nowHour,
                        nowMinute,
                        true
                );


        Timepoint[] enabledTimePoints = generateEnabledTimePoints(workingParts);
        timePickerDialog.setSelectableTimes(enabledTimePoints);
        timePickerDialog.vibrate(false);

        if(isTheSameDate){
            boolean isMinimumTimeInTimePoints = false;
            for(Timepoint timepoint : enabledTimePoints){
                if(timepoint.getHour() == nowHour && timepoint.getMinute() == nowMinute){
                    isMinimumTimeInTimePoints = true;
                    break;
                }
            }

            if(isMinimumTimeInTimePoints){
                timePickerDialog.setMinTime(nowHour, nowMinute, nowSecond);
            }
            else if(enabledTimePoints.length > 0) {
                timePickerDialog.setMinTime(enabledTimePoints[0].getHour(), enabledTimePoints[0].getMinute(), nowSecond);
            }

        }

        timePickerDialog.show(BaseActivity.currentActivity.getFragmentManager(), choosetext);
    }

    private void redirectToPayment(){

        switch (createOrderActivity.getSelectedPaymentMethod().getPaymentMethodType()){

            case UNKNOWN:
                DialogHelper.hideLoadingDialog();
                DialogHelper.showAlertDialog(BaseActivity.currentActivity, unknownPaymentMethodText);
                break;

            case CASH:
            case TRANSFER:
            case POST_FINANCE_CARD:
            case TWINT:
                createOrder(getCreateOrderParams(null));
                break;

            default:
                DialogHelper.hideLoadingDialog();
                startBraintreePayment();
        }
    }

    private void createOrder(HashMap<String, Object> params){
        DialogHelper.showLoadingDialog();
        Call<ResponseObject<Integer>> call = ApiService.apiInterface.createOrder(
                "/" + SessionHelper.getSelectedStore().getStoreName() + ApiEndPoints.API_CREATE_ORDER + SessionHelper.getCustomerToken().getToken(), params);
        call.enqueue(new Callback<ResponseObject<Integer>>() {
            @Override
            public void onResponse(Call<ResponseObject<Integer>> call, Response<ResponseObject<Integer>> response) {
                DialogHelper.hideLoadingDialog();
                if(response.isSuccessful() && response.body().isSuccess()){

                    if(createOrderActivity.getSelectedPaymentMethod().getPaymentMethodType() == PaymentMethodModel.PaymentMethodType.POST_FINANCE_CARD ||
                            createOrderActivity.getSelectedPaymentMethod().getPaymentMethodType() == PaymentMethodModel.PaymentMethodType.TWINT){
                        showPaymentWebView(response.body().getData());
                    }
                    else {
                        createOrderActivity.onNextClicked(FRAGMENT_NAME);
                    }
                }
                else{
                    ApiErrorUtils.parseError(response);
                }
            }

            @Override
            public void onFailure(Call<ResponseObject<Integer>> call, Throwable t) {
                DialogHelper.hideLoadingDialog();
                DialogHelper.showFailedDialog();
            }
        });
    }

    // for braintree payment
    private HashMap<String, Object> getCreateOrderParams(String paymentMethodNonce){
        HashMap<String, Object> params = new HashMap<>();
        params.put("customer_id", SessionHelper.getUser().getId());
        params.put("shipping_method_title", createOrderActivity.getSelectedShippingMethod().getTitle());
        params.put("shipping_method_code", createOrderActivity.getSelectedShippingMethod().getCode());
        params.put("address_id", createOrderActivity.getSelectedShippingAddress().getId());
        params.put("payment_method_title", createOrderActivity.getSelectedPaymentMethod().getTitle());
        params.put("payment_method_code", createOrderActivity.getSelectedPaymentMethod().getCode());
        params.put("comment", createOrderNote());
        params.put("is_pizza_pass_campaign", createOrderActivity.getCartInfo().isPizzapassCampaignUsed());
        params.put("is_kebap_pass_campaign", createOrderActivity.getCartInfo().isKebappassCampaignUsed());
        params.put("application_id", ApiConstants.APP_TYPE_ID_ANDROID);
        if(paymentMethodNonce != null){
            params.put("payment_method_nonce", paymentMethodNonce);
        }
        if(SessionHelper.getSelectedCoupon() != null){
            params.put("coupon", SessionHelper.getSelectedCoupon().getCode());
        }
        if(dateOfOrderString != null && !dateOfOrderString.isEmpty() &&
                timeOfOrder != null && !timeOfOrder.isEmpty()){
            params.put("delivery_date", dateOfOrderString + " " + timeOfOrder);
        }
        return params;
    }

    private String createOrderNote(){

        CreateOrderActivity createOrderActivity = (CreateOrderActivity) BaseActivity.currentActivity;
        StringBuilder stringBuilder = new StringBuilder();
        /*
        if(isCartContainsAnyPizza()){
            stringBuilder.append(slicePizzaForStoreText).append(" : ");
            if(createOrderActivity.getSlicePizza() == Boolean.TRUE){
                stringBuilder.append(yesText);
            }
            else {
                stringBuilder.append(noText);
            }
            stringBuilder.append("\n");
        }
        */

        stringBuilder
                .append(createOrderActivity.getOrderNote());
                //.append("\n");

        /*
        if(dateOfOrderString != null && !dateOfOrderString.isEmpty() &&
                timeOfOrder != null && !timeOfOrder.isEmpty()){

            stringBuilder
                    .append(deliveryDateText)
                    .append(" ")
                    .append(dateOfOrderString)
                    .append("\n")
                    .append(deliveryTimeText)
                    .append(" ")
                    .append(timeOfOrder);

        }
        */

        if(stringBuilder.toString().equals("\n") || stringBuilder.toString().equals("\n<br/>") || stringBuilder.toString().equals("<br/>")){
            return "";
        }
        String comment = stringBuilder.toString();
        if(comment.startsWith("\n")){
            comment = comment.replace("\n", "");
        }
        return comment;
    }

    private boolean isCartContainsAnyPizza(){
        boolean containsAnyPizza = false;
        outerloop:
        for(CartProductModel cartProductModel : createOrderActivity.getCartInfo().getProducts()){
            if(SharedPrefsHelper.readPizzaCategoryIdList().contains(Integer.valueOf(cartProductModel.getProductId()))){
                containsAnyPizza = true;
                break;
            }
            for(String categoryId : cartProductModel.getCategoryList()){
                if(SharedPrefsHelper.readPizzaCategoryIdList().contains(Integer.valueOf(categoryId))){
                    containsAnyPizza = true;
                    break outerloop;
                }
            }
        }
        return containsAnyPizza;
    }

    private void getCartProducts(){
        DialogHelper.showLoadingDialog();
        /*
        Call<ResponseObject<CartInfoModel>> call = ApiService.apiInterface.getCartProducts(
                "/" + SessionHelper.getSelectedStore().getStoreName() + ApiEndPoints.API_GET_CART_PRODUCTS + SessionHelper.getCustomerToken().getToken());
                */
        HashMap<String, Object> params = new HashMap<>();
        if(SessionHelper.getSelectedCoupon() != null){
            params.put("coupon", SessionHelper.getSelectedCoupon().getCode());
        }

        Call<ResponseObject<CartInfoModel>> call = ApiService.apiInterface.getCartProducts(
                "/" + SessionHelper.getSelectedStore().getStoreName() + ApiEndPoints.API_GET_CART_PRODUCTS + SessionHelper.getCustomerToken().getToken(),
                params);

        call.enqueue(new Callback<ResponseObject<CartInfoModel>>() {
            @Override
            public void onResponse(Call<ResponseObject<CartInfoModel>> call, Response<ResponseObject<CartInfoModel>> response) {
                DialogHelper.hideLoadingDialog();
                if(response.isSuccessful() &&
                        response.body().getData() != null &&
                        response.body().isSuccess()){
                    response.body().getData().checkNull();
                    createOrderActivity.setCartInfoSafeForCampaigns(response.body().getData());
                }
                else
                    ApiErrorUtils.parseError(response);
            }

            @Override
            public void onFailure(Call<ResponseObject<CartInfoModel>> call, Throwable t) {
                DialogHelper.hideLoadingDialog();
                DialogHelper.showFailedDialog();
            }
        });
    }

    private void getCartProductsWithCommission(){
        DialogHelper.showLoadingDialog();
        /*
        Call<ResponseObject<CartInfoModel>> call = ApiService.apiInterface.getCartProductsForCommission(
                "/" + SessionHelper.getSelectedStore().getStoreName() + ApiEndPoints.API_GET_CART_PRODUCTS + SessionHelper.getCustomerToken().getToken(),
                createOrderActivity.getSelectedPaymentMethod().getCode(),
                createOrderActivity.getSelectedShippingMethod().getCode());
                */

        HashMap<String, Object> params = new HashMap<>();
        if(SessionHelper.getSelectedCoupon() != null){
            params.put("coupon", SessionHelper.getSelectedCoupon().getCode());
        }
        params.put("payment_method", createOrderActivity.getSelectedPaymentMethod().getCode());
        params.put("payment_method_code", createOrderActivity.getSelectedPaymentMethod().getCode());
        params.put("shipping_method",createOrderActivity.getSelectedShippingMethod().getCode());
        params.put("shipping_method_code",createOrderActivity.getSelectedShippingMethod().getCode());

        Call<ResponseObject<CartInfoModel>> call = ApiService.apiInterface.getCartProducts(
                "/" + SessionHelper.getSelectedStore().getStoreName() + ApiEndPoints.API_GET_CART_PRODUCTS + SessionHelper.getCustomerToken().getToken(),
                params);

        call.enqueue(new Callback<ResponseObject<CartInfoModel>>() {
            @Override
            public void onResponse(Call<ResponseObject<CartInfoModel>> call, Response<ResponseObject<CartInfoModel>> response) {
                DialogHelper.hideLoadingDialog();
                if(response.isSuccessful() &&
                        response.body().getData() != null &&
                        response.body().isSuccess()){
                    CartInfoModel cartInfoModel = response.body().getData();
                    cartInfoModel.checkNull();
                    createOrderActivity.setCartInfoSafeForCampaigns(cartInfoModel);
                    //setOrderTotalText(response.body().getData());
                    cartTotalList.clear();
                    cartTotalList.addAll(cartInfoModel.getTotalsList());
                    fixCartTotalList();
                    orderPriceRecyclerAdapter.notifyDataSetChanged();
                }
                else {
                    ApiErrorUtils.parseError(response);
                }
            }

            @Override
            public void onFailure(Call<ResponseObject<CartInfoModel>> call, Throwable t) {
                DialogHelper.hideLoadingDialog();
                DialogHelper.showFailedDialog();
            }
        });
    }

    private void createBraintreePaymentToken(){

        Call<ResponseObject<PaymentTokenModel>> call = ApiService.apiInterface.createBraintreePaymentToken(
                "/" + SessionHelper.getSelectedStore().getStoreName() + ApiEndPoints.API_CREATE_BRAINTREE_PAYMENT_TOKEN + SessionHelper.getCustomerToken().getToken());

        call.enqueue(new Callback<ResponseObject<PaymentTokenModel>>() {
            @Override
            public void onResponse(Call<ResponseObject<PaymentTokenModel>> call, Response<ResponseObject<PaymentTokenModel>> response) {
                if(response.isSuccessful() &&
                        response.body().getData() != null &&
                        response.body().isSuccess()){
                    braintreePaymentTokenModel = response.body().getData();
                    Log.i("fuhrfuhruhfr", "fhurhfurfhurfh");
                }
                else {
                    ApiErrorUtils.parseError(response);
                }
            }

            @Override
            public void onFailure(Call<ResponseObject<PaymentTokenModel>> call, Throwable t) {
                DialogHelper.showFailedDialog();
            }
        });
    }

    private void startBraintreePayment(){

        // CVV kodu default olarak gozukmuyor.
        // Gozukmesi için braintree'ye login ol
        // Go to Settings --> Processing --> CVV --> Click on Edit --> Check option 'CVV not provided(l)', Enable option 'for any transaction'

        if(braintreePaymentTokenModel == null ||braintreePaymentTokenModel.getToken().isEmpty()){
            DialogHelper.showAlertDialog(BaseActivity.currentActivity, getString(R.string.something_went_wrong));
            return;
        }
        String amountStringForBraintree = PriceHelper.removeCurrencyFromPrice(cartTotalList.get(cartTotalList.size() - 1).getText());
        PayPalRequest paypalRequest = new PayPalRequest(amountStringForBraintree)
                .currencyCode("CHF");
                //.intent(PayPalRequest.INTENT_AUTHORIZE);

        /**
         * Paypal has two flow type: Vault and Checkout.
         * Vault: Collects user card data.
         * Checkout: One time payment.
         *
         *  This method (paypalRequest() method) is optional.
         *  @param request The PayPal Request {@link PayPalRequest} for the transaction.
         *  If no amount is set, PayPal will default to the billing agreement (Vault) flow.
         *  If amount is set, PayPal will follow the one time payment (Checkout) flow.
         *
         * Drop-in verince Vault çalışıyormuş galiba, ondan dolay pappal request verdik
         * https://bitbucket.org/pusulawiss/pizza_leu/issues/73/feat-paypal-i-leyi-de-i-imi
         *
         * https://developers.braintreepayments.com/guides/paypal/checkout-with-paypal/android/v3
         * https://developers.braintreepayments.com/guides/paypal/vault/javascript/v2
         */
        DropInRequest paymentRequest = new DropInRequest()
                //.tokenizationKey(ApiConstants.BRAINTREE_TOKENIZATION_KEY_SANDBOX)
                //.tokenizationKey(ApiConstants.BRAINTREE_TOKENIZATION_KEY_PRODUCTION)
                //.clientToken("eyJ2ZXJzaW9uIjoyLCJhdXRob3JpemF0aW9uRmluZ2VycHJpbnQiOiI3NzY2ZmNmNTQ3MDc1YWMzMGZiNmIxMzU3MTI1ZTJmODc5YjM4MmNkOTcwYmYwZmYyNmQzMWJmNGRiYjI1MjQzfGNyZWF0ZWRfYXQ9MjAxOC0wNi0xM1QxODo1Mzo0Mi4wODA4MDc5MDQrMDAwMFx1MDAyNm1lcmNoYW50X2lkPTM0OHBrOWNnZjNiZ3l3MmJcdTAwMjZwdWJsaWNfa2V5PTJuMjQ3ZHY4OWJxOXZtcHIiLCJjb25maWdVcmwiOiJodHRwczovL2FwaS5zYW5kYm94LmJyYWludHJlZWdhdGV3YXkuY29tOjQ0My9tZXJjaGFudHMvMzQ4cGs5Y2dmM2JneXcyYi9jbGllbnRfYXBpL3YxL2NvbmZpZ3VyYXRpb24iLCJjaGFsbGVuZ2VzIjpbXSwiZW52aXJvbm1lbnQiOiJzYW5kYm94IiwiY2xpZW50QXBpVXJsIjoiaHR0cHM6Ly9hcGkuc2FuZGJveC5icmFpbnRyZWVnYXRld2F5LmNvbTo0NDMvbWVyY2hhbnRzLzM0OHBrOWNnZjNiZ3l3MmIvY2xpZW50X2FwaSIsImFzc2V0c1VybCI6Imh0dHBzOi8vYXNzZXRzLmJyYWludHJlZWdhdGV3YXkuY29tIiwiYXV0aFVybCI6Imh0dHBzOi8vYXV0aC52ZW5tby5zYW5kYm94LmJyYWludHJlZWdhdGV3YXkuY29tIiwiYW5hbHl0aWNzIjp7InVybCI6Imh0dHBzOi8vb3JpZ2luLWFuYWx5dGljcy1zYW5kLnNhbmRib3guYnJhaW50cmVlLWFwaS5jb20vMzQ4cGs5Y2dmM2JneXcyYiJ9LCJ0aHJlZURTZWN1cmVFbmFibGVkIjp0cnVlLCJwYXlwYWxFbmFibGVkIjp0cnVlLCJwYXlwYWwiOnsiZGlzcGxheU5hbWUiOiJBY21lIFdpZGdldHMsIEx0ZC4gKFNhbmRib3gpIiwiY2xpZW50SWQiOm51bGwsInByaXZhY3lVcmwiOiJodHRwOi8vZXhhbXBsZS5jb20vcHAiLCJ1c2VyQWdyZWVtZW50VXJsIjoiaHR0cDovL2V4YW1wbGUuY29tL3RvcyIsImJhc2VVcmwiOiJodHRwczovL2Fzc2V0cy5icmFpbnRyZWVnYXRld2F5LmNvbSIsImFzc2V0c1VybCI6Imh0dHBzOi8vY2hlY2tvdXQucGF5cGFsLmNvbSIsImRpcmVjdEJhc2VVcmwiOm51bGwsImFsbG93SHR0cCI6dHJ1ZSwiZW52aXJvbm1lbnROb05ldHdvcmsiOnRydWUsImVudmlyb25tZW50Ijoib2ZmbGluZSIsInVudmV0dGVkTWVyY2hhbnQiOmZhbHNlLCJicmFpbnRyZWVDbGllbnRJZCI6Im1hc3RlcmNsaWVudDMiLCJiaWxsaW5nQWdyZWVtZW50c0VuYWJsZWQiOnRydWUsIm1lcmNoYW50QWNjb3VudElkIjoiYWNtZXdpZGdldHNsdGRzYW5kYm94IiwiY3VycmVuY3lJc29Db2RlIjoiVVNEIn0sIm1lcmNoYW50SWQiOiIzNDhwazljZ2YzYmd5dzJiIiwidmVubW8iOiJvZmYifQ==")
                .clientToken(braintreePaymentTokenModel.getToken())
                .requestThreeDSecureVerification(true)
                .amount(amountStringForBraintree)
                .disableAndroidPay()
                .paypalRequest(paypalRequest)
                .disableVenmo();


        /*
        switch (createOrderActivity.getSelectedPaymentMethod().getPaymentMethodType()){
            case CREDIT_DEBIT_CARD:
                paymentRequest.disableAndroidPay();
                paymentRequest.disablePayPal();
                break;
            case PAYPAL:
                paymentRequest.disableAndroidPay();
                break;
            case ANDROID_PAY:
                paymentRequest.disablePayPal();
                if(getAndroidPayCart() == null){
                    DialogHelper.showAlertDialog(BaseActivity.currentActivity, androidPayNotFoundText);
                    return;
                }
                else {
                    paymentRequest.androidPayCart(getAndroidPayCart());
                }
                break;
        }
        */
        startActivityForResult(paymentRequest.getIntent(BaseActivity.currentActivity), REQUEST_CODE_BRAINTREE_PAYMENT);
    }

    private void createBraintreePayment(final DropInResult dropInResult){
        DialogHelper.showLoadingDialog();
        Call<ResponseObject> call = ApiService.apiInterface.createBraintreePayment(
                "/" + SessionHelper.getSelectedStore().getStoreName() + ApiEndPoints.API_CREATE_BRAINTREE_PAYMENT + SessionHelper.getCustomerToken().getToken(),
                getBraintreePaymentParams(dropInResult));
        call.enqueue(new Callback<ResponseObject>() {
            @Override
            public void onResponse(Call<ResponseObject> call, final Response<ResponseObject> response) {
                DialogHelper.hideLoadingDialog();
                if(response.isSuccessful() &&
                        response.body().getData() != null &&
                        response.body().isSuccess()){
                    createOrder(getCreateOrderParams(dropInResult.getPaymentMethodNonce().getNonce()));
                }
                else {
                    ApiErrorUtils.parseError(response);
                }
            }

            @Override
            public void onFailure(Call<ResponseObject> call, Throwable t) {
                DialogHelper.hideLoadingDialog();
                DialogHelper.showFailedDialog();
            }
        });
    }

    private HashMap<String, Object> getBraintreePaymentParams(DropInResult dropInResult){
        HashMap<String, Object> params = new HashMap<>();
        params.put("payment_method_nonce", dropInResult.getPaymentMethodNonce().getNonce());
        params.put("amount", PriceHelper.removeCurrencyFromPrice(createOrderActivity.getCartInfo().getCartTotalModel().getText()));
        return params;
    }

    private Cart getAndroidPayCart() {
        return Cart.newBuilder()
                .setCurrencyCode("CHF")
                .setTotalPrice(PriceHelper.removeCurrencyFromPrice(createOrderActivity.getCartInfo().getCartTotalModel().getText()))
                /*
                .addLineItem(LineItem.newBuilder()
                        .setCurrencyCode("CHF")
                        .setDescription("Description")
                        .setQuantity("1")
                        .setUnitPrice("1.00")
                        .setTotalPrice("1.00")
                        .build())
                        */
                .build();
    }

    /*
    private void setOrderTotalText(CartInfoModel cartInfoModel){

        CartTotalModel subtotalModel = new CartTotalModel("","");
        CartTotalModel commissionModel = new CartTotalModel("","");
        CartTotalModel totalModel = new CartTotalModel("","");

        String totalText = "";

        for (CartTotalModel cartTotalModel : cartInfoModel.getTotalsList()){
            switch (cartTotalModel.getTitle().toLowerCase()){
                case ApiConstants.CART_SUBTOTAL :
                    subtotalModel = cartTotalModel;
                    break;
                case ApiConstants.CART_COMMISSION :
                    commissionModel = cartTotalModel;
                    break;
                case ApiConstants.CART_TOTAL :
                    totalModel = cartTotalModel;
                    break;
            }
        }

        if(commissionModel.getText().isEmpty()){
            totalText = PriceHelper.roundFractions(cartInfoModel.getCartTotalModel().getText());
        }
        else {
            totalText =
                    new StringBuilder()
                            .append(subtotalModel.getTitle())
                            .append("\n")
                            .append(subtotalModel.getText())
                            .append("\n\n")
                            .append(commissionModel.getTitle())
                            .append("\n")
                            .append(commissionModel.getText())
                            .append("\n\n")
                            .append(totalModel.getTitle())
                            .append("\n")
                            .append(totalModel.getText())
                            //.append(PriceHelper.roundFractions(totalModel.getText()))
                            .toString();
        }
        //orderTotalPizzalinkInfoLayout.setText(totalText);

    }
    */

    private Calendar[] generateEnabledDays(){

        ArrayList<Calendar> enabledDayList = new ArrayList<>();

        for(int i = 0; i < 8; i++){

            Calendar calendar = Calendar.getInstance();
            ArrayList<String> dayParts = new ArrayList<>();

            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

            switch ((dayOfWeek + i) % 7){
                case 0:
                    dayParts = storeShiftModel.getSundayShift();
                    break;
                case 1:
                    dayParts = storeShiftModel.getMondayShift();
                    break;
                case 2:
                    dayParts = storeShiftModel.getTuesdayShift();
                    break;
                case 3:
                    dayParts = storeShiftModel.getWednesdayShift();
                    break;
                case 4:
                    dayParts = storeShiftModel.getThursdayShift();
                    break;
                case 5:
                    dayParts = storeShiftModel.getFridayShift();
                    break;
                case 6:
                    dayParts = storeShiftModel.getSaturdayShift();
                    break;
            }

            if(!dayParts.isEmpty()){
                calendar.add(Calendar.DAY_OF_YEAR, i);
                enabledDayList.add(calendar);
            }
        }

        Calendar calendar = Calendar.getInstance();
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        ArrayList<String> todayParts = new ArrayList<>();
        switch (dayOfWeek){
            case 1:
                todayParts = storeShiftModel.getSundayShift();
                break;
            case 2:
                todayParts = storeShiftModel.getMondayShift();
                break;
            case 3:
                todayParts = storeShiftModel.getTuesdayShift();
                break;
            case 4:
                todayParts = storeShiftModel.getWednesdayShift();
                break;
            case 5:
                todayParts = storeShiftModel.getThursdayShift();
                break;
            case 6:
                todayParts = storeShiftModel.getFridayShift();
                break;
            case 7:
                todayParts = storeShiftModel.getSaturdayShift();
                break;
        }
        if(isDayAvailable(todayParts)){
            enabledDayList.add(Calendar.getInstance());
        }
        return enabledDayList.toArray(new Calendar[enabledDayList.size()]);

    }

    private Timepoint[] generateEnabledTimePoints(ArrayList<String> workingParts){

        ArrayList<Timepoint> enabledTimePointList = new ArrayList<>();

        try {
            for(String part : workingParts){
                part = part.replace(" ", "");
                String[] array = part.split("-");
                String partStart = array[0];
                String partEnd = array[1];
                //partEnd = fixEndTime(partEnd); // to prevent creating orders which 15 minutes before restaurant close time. now it will controlled on backend

                int partStartHour = Integer.valueOf(partStart.substring(0,2));
                int partStartMinute = Integer.valueOf(partStart.substring(3, 5));
                int partEndHour = Integer.valueOf(partEnd.substring(0,2));
                int partEndMinute = Integer.valueOf(partEnd.substring(3, 5));

                int hour = partStartHour;
                int minute = partStartMinute;

                while (hour <= partEndHour){
                    enabledTimePointList.add(new Timepoint(hour, minute));
                    minute++;
                    if(minute == 60){
                        minute = 0;
                        hour += 1;
                    }

                    if(hour == partEndHour && minute > partEndMinute){
                        break;
                    }
                }
            }
        }catch (Exception e){
            return enabledTimePointList.toArray(new Timepoint[enabledTimePointList.size()]);

        }
        return enabledTimePointList.toArray(new Timepoint[enabledTimePointList.size()]);
    }

    private boolean isDayAvailable(ArrayList<String> parts){
        if(parts.size() < 2){
            return false;
        }
        for(String part : parts){
            if(isPartAvailable(part)){
                return true;
            }
        }
        return false;
    }

    private boolean isPartAvailable(String part){
        try {
            Date nowDate = Calendar.getInstance().getTime();
            DateFormat dateFormat = new SimpleDateFormat("HH:mm");
            String nowTimeString = dateFormat.format(nowDate);
            part = part.replace(" ", "");
            String[] array = part.split("-");
            String partStart = array[0];
            String partEnd = array[1];

            int partStartHour = Integer.valueOf(partStart.substring(0,2));
            int partStartMinute = Integer.valueOf(partStart.substring(3, 5));
            int partEndHour = Integer.valueOf(partEnd.substring(0,2));
            int partEndMinute = Integer.valueOf(partEnd.substring(3, 5));

            Calendar partStartCalendar = Calendar.getInstance();
            partStartCalendar.set(Calendar.HOUR_OF_DAY, partStartHour);
            partStartCalendar.set(Calendar.MINUTE, partStartMinute);
            partStartCalendar.set(Calendar.SECOND, 0);
            Date partStartDate = partStartCalendar.getTime();

            Calendar partEndCalendar = Calendar.getInstance();
            partEndCalendar.set(Calendar.HOUR_OF_DAY, partEndHour);
            partEndCalendar.set(Calendar.MINUTE, partEndMinute);
            partEndCalendar.set(Calendar.SECOND, 0);
            Date partEndDate = partEndCalendar.getTime();

            if(partStartDate.after(nowDate) && partEndDate.after(nowDate)){
                return true;
            }
            else {
                return false;
            }

            /*
            String[] array2 = nowTimeString.split(":");
            String nowHourString = array2[0];
            String nowMinuteString = array2[1];

            int nowHour = Integer.valueOf(nowHourString);
            int nowMinute = Integer.valueOf(nowMinuteString);

            return nowHour >= partStartHour && nowHour <= partEndHour &&
                    nowMinute >= partStartMinute && nowMinute <= partEndMinute;
            */
        }catch (Exception e){
            return false;
        }
    }

    private void fixCartTotalList(){
        ArrayList<CartTotalModel> tempTotalList = new ArrayList<>();
        for(CartTotalModel cartTotalModel : cartTotalList){
            String price = PriceHelper.removeCurrencyFromPrice(cartTotalModel.getText());
            if(!price.equals("0") && !price.equals("0.0") && !price.equals("0.00") &&
                    !price.equals("-0") && !price.equals("-0.0") && !price.equals("-0.00")){
                tempTotalList.add(cartTotalModel);
            }
        }
        cartTotalList.clear();
        cartTotalList.addAll(tempTotalList);
        tempTotalList.clear();
    }

    private void showPaymentWebView(int orderId){
        createOrderActivity.setPaymentWebViewVisibility(true);
        final WebView paymentWebView = createOrderActivity.getPaymentWebView();
        paymentWebView.loadUrl("about:blank");
        paymentWebView.clearHistory();
        paymentWebView.setVisibility(View.VISIBLE);
        paymentWebView.getSettings().setJavaScriptEnabled(true);
        paymentWebView.setWebViewClient(new WebViewClient(){

            @RequiresApi(Build.VERSION_CODES.N)
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                /*
                // Canceled because response json from PostFinance fr apple Webview is not valid
                // payment success or fail. response json will handle at "handlePaymentResponse" method of "PaymentJavaScriptInterface" class below
                if(request.getUrl().toString().contains(ApiConstants.PAYMENT_WEBVIEW_REDIRECT_URL_POSTFINANCE_TWINT_SUCCESS) ||
                        request.getUrl().toString().contains(ApiConstants.PAYMENT_WEBVIEW_REDIRECT_URL_POSTFINANCE_TWINT_FAIL)){
                    createOrderActivity.setPaymentWebViewVisibility(false);
                }
                */

                // payment success
                if(request.getUrl().toString().contains(ApiConstants.PAYMENT_WEBVIEW_REDIRECT_URL_POSTFINANCE_TWINT_SUCCESS) ||
                        request.getUrl().toString().equals(ApiConstants.PAYMENT_WEBVIEW_MAIN_PAGE_HTTP) ||
                        request.getUrl().toString().equals(ApiConstants.PAYMENT_WEBVIEW_MAIN_PAGE_HTTPS)){
                    createOrderActivity.setPaymentWebViewVisibility(false);
                    createOrderActivity.onNextClicked(FRAGMENT_NAME);
                    return false;
                }
                // payment fail
                else if(request.getUrl().toString().contains(ApiConstants.PAYMENT_WEBVIEW_REDIRECT_URL_POSTFINANCE_TWINT_FAIL) ||
                        request.getUrl().toString().contains(ApiConstants.PAYMENT_WEBVIEW_MAIN_PAGE_HTTP) ||
                        request.getUrl().toString().contains(ApiConstants.PAYMENT_WEBVIEW_MAIN_PAGE_HTTPS)){
                    createOrderActivity.setPaymentWebViewVisibility(false);
                    DialogHelper.showAlertDialog(BaseActivity.currentActivity, getString(R.string.error_message));
                    return false;
                }
                else {
                    return false;
                }
            }

            @SuppressWarnings("deprecation")
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                /*
                // Canceled because response json from PostFinance fr apple Webview is not valid
                // payment success or fail. response json will handle at "handlePaymentResponse" method of "PaymentJavaScriptInterface" class below
                if(url.contains(ApiConstants.PAYMENT_WEBVIEW_REDIRECT_URL_POSTFINANCE_TWINT_SUCCESS) ||
                        url.contains(ApiConstants.PAYMENT_WEBVIEW_REDIRECT_URL_POSTFINANCE_TWINT_FAIL)){
                    createOrderActivity.setPaymentWebViewVisibility(false);
                }
                */

                // payment success
                if(url.contains(ApiConstants.PAYMENT_WEBVIEW_REDIRECT_URL_POSTFINANCE_TWINT_SUCCESS) ||
                        url.equals(ApiConstants.PAYMENT_WEBVIEW_MAIN_PAGE_HTTP) ||
                        url.equals(ApiConstants.PAYMENT_WEBVIEW_MAIN_PAGE_HTTPS)){
                    createOrderActivity.setPaymentWebViewVisibility(false);
                    createOrderActivity.onNextClicked(FRAGMENT_NAME);
                    return true;
                }
                // payment fail
                else if(url.contains(ApiConstants.PAYMENT_WEBVIEW_REDIRECT_URL_POSTFINANCE_TWINT_FAIL) ||
                        url.contains(ApiConstants.PAYMENT_WEBVIEW_MAIN_PAGE_HTTP) ||
                        url.contains(ApiConstants.PAYMENT_WEBVIEW_MAIN_PAGE_HTTPS)){
                    createOrderActivity.setPaymentWebViewVisibility(false);
                    DialogHelper.showAlertDialog(BaseActivity.currentActivity, getString(R.string.error_message));
                    return true;
                }
                else {
                    return false;
                }
            }

            /*
            // Canceled because response json from PostFinance fr apple Webview is not valid
            @Override
            public void onPageFinished(WebView view, String url) {
                view.loadUrl("javascript:PaymentResponseHandler.handlePaymentResponse" +
                        "(document.getElementsByTagName('pre')[0].innerHTML);");
            }
             */

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                DialogHelper.showAlertDialog(BaseActivity.currentActivity, getString(R.string.error_message));
            }
        });

        paymentWebView.loadUrl(ApiConstants.PAYMENT_WEBVIEW_URL_POSTFINANCE_TWINT + orderId);
        // Canceled because response json from PostFinance fr apple Webview is not valid
        //paymentWebView.addJavascriptInterface(new PaymentJavaScriptInterface(getActivity()), "PaymentResponseHandler");
    }

    private String fixEndTime(String endTime){
        try {
            String[] time = endTime.split(":");
            int hour = Integer.parseInt(time[0]);
            int minute = Integer.parseInt(time[1]);
            if(minute < 15){
                minute += 60;
                if(hour == 0){
                    hour += 24;
                }
                hour -= 1;
            }
            minute -= 15;
            String hourString = String.valueOf(hour);
            if(hourString.length() == 1){
                hourString = "0" + hourString;
            }
            String minuteString = String.valueOf(minute);
            if(minuteString.length() == 1){
                minuteString = "0" + minuteString;
            }
            return hourString + ":" + minuteString;
        }catch (Exception e){
            e.printStackTrace();
            return endTime;
        }
    }

    /*
    // Canceled because response json from PostFinance fr apple Webview is not valid
    class PaymentJavaScriptInterface {

        private Context ctx;

        PaymentJavaScriptInterface(Context ctx) {
            this.ctx = ctx;
        }

        @JavascriptInterface
        public void handlePaymentResponse(String jsonString) {
            try{
                if(jsonString.contains("success") && jsonString.contains("message") && jsonString.contains("error_code") && jsonString.contains("data")){
                    Gson gson = new Gson();
                    PostfinancePaymentResponseModel postfinancePaymentResponseModel = gson.fromJson(jsonString, PostfinancePaymentResponseModel.class);

                    if(postfinancePaymentResponseModel.isSuccess()){
                        createOrderActivity.onNextClicked(FRAGMENT_NAME);
                    }
                    else {
                        DialogHelper.showAlertDialog(BaseActivity.currentActivity, postfinancePaymentResponseModel.getMessage());
                    }
                }
            }
            catch (Exception e){
                DialogHelper.showAlertDialog(BaseActivity.currentActivity, getString(R.string.error_message));
                Crashlytics.logException(e);
            }
        }
    }
    */
}
