package ch.pizzalink.android.fragment.createOrder;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.HashMap;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ch.pizzalink.android.R;
import ch.pizzalink.android.activity.CreateOrderActivity;
import ch.pizzalink.android.api.ApiEndPoints;
import ch.pizzalink.android.api.ApiErrorUtils;
import ch.pizzalink.android.api.ApiService;
import ch.pizzalink.android.api.ResponseObject;
import ch.pizzalink.android.helper.DialogHelper;
import ch.pizzalink.android.helper.PriceHelper;
import ch.pizzalink.android.helper.SessionHelper;
import ch.pizzalink.android.view.PizzalinkInfoView;
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) PizzalinkInfoView orderPersonFullnamePizzalinkInfoLayout;
    @BindView(R.id.orderShippingMethodPizzalinkInfoLayout) PizzalinkInfoView orderShippingMethodPizzalinkInfoLayout;
    @BindView(R.id.orderShippingAddressPizzalinkInfoLayout) PizzalinkInfoView orderShippingAddressPizzalinkInfoLayout;
    @BindView(R.id.orderPaymentMethodPizzalinkInfoLayout) PizzalinkInfoView orderPaymentMethodPizzalinkInfoLayout;
    @BindView(R.id.orderTotalPizzalinkInfoLayout) PizzalinkInfoView orderTotalPizzalinkInfoLayout;
    @BindView(R.id.orderNotePizzalinkInfoLayout) PizzalinkInfoView orderNotePizzalinkInfoLayout;

    @BindString(R.string.confirm_order) String confirmOrderText;

    public static final java.lang.String FRAGMENT_NAME = "orderSummaryFragment";
    private CreateOrderActivity createOrderActivity;

    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();
        return view;
    }

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

    private void initViews(){

        nextTextView.setText(confirmOrderText);

        createOrderActivity = (CreateOrderActivity) getActivity();

        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().getTotals().get(0).getText()));
        if(createOrderActivity.getOrderNote().isEmpty()){
            orderNotePizzalinkInfoLayout.setVisibility(View.GONE);
        }
        else {
            orderNotePizzalinkInfoLayout.setVisibility(View.VISIBLE);
            orderNotePizzalinkInfoLayout.setText(createOrderActivity.getOrderNote());
        }
    }

    private void createOrder(){
        DialogHelper.showLoadingDialog();
        Call<ResponseObject<Integer>> call = ApiService.apiInterface.createOrder(
                ApiEndPoints.API_CREATE_ORDER + SessionHelper.getCustomerToken().getToken(), getCreateOrderParams());
        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()){
                    createOrderActivity.onNextClicked(FRAGMENT_NAME);
                }
                else{
                    ApiErrorUtils.parseError(response);
                }
            }

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

    private HashMap<String, Object> getCreateOrderParams(){
        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", createOrderActivity.getOrderNote());
        return params;
    }
}
