package ch.pizzalink.android.fragment.createOrder;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ch.pizzalink.android.R;
import ch.pizzalink.android.activity.BaseActivity;
import ch.pizzalink.android.activity.CreateOrderActivity;
import ch.pizzalink.android.adapter.recycler.PaymentMethodsRecyclerAdapter;
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.SessionHelper;
import ch.pizzalink.android.interfaces.RecyclerItemClickListener;
import ch.pizzalink.android.model.PaymentMethodModel;
import ch.pizzalink.android.model.PaymentMethodsResponseModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

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

public class PaymentMethodFragment extends CreateOrderBaseFragment {

    @BindView(R.id.paymentMethodsRecyclerView) RecyclerView paymentMethodsRecyclerView;

    @BindString(R.string.alert_choose_payment_method) String choosePaymentMethodText;

    private ArrayList<PaymentMethodModel> paymentMethodList = new ArrayList<>();
    private PaymentMethodsRecyclerAdapter paymentMethodsRecyclerAdapter;

    private PaymentMethodModel selectedPaymentMethod;

    public static final java.lang.String FRAGMENT_NAME = "paymentMethodFragment";

    public PaymentMethodFragment() {}

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

    @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_payment_method, container, false);
        ButterKnife.bind(this, view);
        initViews();
        getPaymentMethods();
        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:

                if(selectedPaymentMethod == null){
                    DialogHelper.showAlertDialog(BaseActivity.currentActivity, choosePaymentMethodText);
                    break;
                }
                createOrderActivity.setSelectedPaymentMethod(selectedPaymentMethod);
                createOrderActivity.onNextClicked(FRAGMENT_NAME);
                break;
        }
    }

    private void initViews(){
        initRecyclerView();
    }

    private void getPaymentMethods(){
        DialogHelper.showLoadingDialog();
        Call<ResponseObject<PaymentMethodsResponseModel>> call = ApiService.apiInterface.getPaymentMethods(
                SessionHelper.getCustomerToken().getToken());
        call.enqueue(new Callback<ResponseObject<PaymentMethodsResponseModel>>() {
            @Override
            public void onResponse(Call<ResponseObject<PaymentMethodsResponseModel>> call, Response<ResponseObject<PaymentMethodsResponseModel>> response) {
                DialogHelper.hideLoadingDialog();
                if(response.isSuccessful() &&
                        response.body().getData() != null &&
                        response.body().isSuccess()){
                    fillAndNotifyAdapter(response.body().getData().getPaymentMethodsList());
                }
                else {
                    ApiErrorUtils.parseError(response);
                }
            }

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

    private void fillAndNotifyAdapter(ArrayList<PaymentMethodModel> paymentMethodModels){
        PaymentMethodModel.checkNull(paymentMethodModels);
        PaymentMethodModel.sort(paymentMethodModels);
        paymentMethodList.clear();
        paymentMethodList.addAll(paymentMethodModels);
        if(paymentMethodList.size() != 0){

            CreateOrderActivity createOrderActivity = (CreateOrderActivity) getActivity();
            if(createOrderActivity.getSelectedPaymentMethod() == null){
                paymentMethodList.get(0).setSelected(true);
                selectedPaymentMethod = paymentMethodList.get(0);
            }
            else {
                for(PaymentMethodModel paymentMethodModel : paymentMethodModels){
                    if(paymentMethodModel.getCode().equals(createOrderActivity.getSelectedPaymentMethod().getCode())){
                        paymentMethodModel.setSelected(true);
                        selectedPaymentMethod = paymentMethodModel;
                        break;
                    }
                }
            }

        }
        paymentMethodsRecyclerAdapter.notifyDataSetChanged();
    }

    private void initRecyclerView(){
        paymentMethodsRecyclerAdapter = new PaymentMethodsRecyclerAdapter(paymentMethodList, new RecyclerItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                for (PaymentMethodModel paymentMethodModel : paymentMethodList){
                    paymentMethodModel.setSelected(false);
                }
                paymentMethodList.get(position).setSelected(true);
                selectedPaymentMethod = paymentMethodList.get(position);
                paymentMethodsRecyclerAdapter.notifyDataSetChanged();
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(BaseActivity.currentActivity);
        paymentMethodsRecyclerView.setLayoutManager(layoutManager);
        paymentMethodsRecyclerView.setAdapter(paymentMethodsRecyclerAdapter);
    }
}