package ch.pizzalemon.android.fragment.createOrder;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ch.pizzalemon.android.R;
import ch.pizzalemon.android.activity.BaseActivity;
import ch.pizzalemon.android.activity.CreateOrderActivity;
import ch.pizzalemon.android.adapter.recycler.ShippingMethodsRecyclerAdapter;
import ch.pizzalemon.android.api.ApiErrorUtils;
import ch.pizzalemon.android.api.ApiService;
import ch.pizzalemon.android.api.ResponseArray;
import ch.pizzalemon.android.helper.DialogHelper;
import ch.pizzalemon.android.helper.PriceHelper;
import ch.pizzalemon.android.helper.SessionHelper;
import ch.pizzalemon.android.interfaces.RecyclerItemClickListener;
import ch.pizzalemon.android.model.ShippingMethodModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

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

public class ShippingMethodFragment extends CreateOrderBaseFragment {

    @BindView(R.id.shipmentMethodInfoTextView) TextView shipmentMethodInfoTextView;
    @BindView(R.id.shippingMethodsRecyclerView) RecyclerView shippingMethodsRecyclerView;

    @BindString(R.string.alert_choose_shipping_method) String chooseShippingMethodText;

    private ArrayList<ShippingMethodModel> shippingMethodList = new ArrayList<>();
    private ShippingMethodsRecyclerAdapter shippingMethodsRecyclerAdapter;

    private ShippingMethodModel selectedShippingMethodModel;

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

    public ShippingMethodFragment() {}

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

    @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_shipping_method, container, false);
        ButterKnife.bind(this, view);
        initViews();
        getShippingMethods();
        return view;
    }

    @OnClick(R.id.nextTextView)
    protected void onClick(){

        if(selectedShippingMethodModel == null){
            DialogHelper.showAlertDialog(BaseActivity.currentActivity, chooseShippingMethodText);
            return;
        }

        CreateOrderActivity createOrderActivity = (CreateOrderActivity) BaseActivity.currentActivity;
        createOrderActivity.setSelectedShippingMethod(selectedShippingMethodModel);
        createOrderActivity.onNextClicked(FRAGMENT_NAME);
    }

    private void initViews(){
        String shipmentMethodInfoText =
                getString(R.string.shipping_method_info) +
                " " +
                PriceHelper.getPriceWithCurreny(SessionHelper.getSelectedStore().getMinimumPrice());
        shipmentMethodInfoTextView.setText(shipmentMethodInfoText);
        initRecyclerView();
        previousTextView.setVisibility(View.GONE);
    }

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

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

    private void fillAndNotifyAdapter(ArrayList<ShippingMethodModel> shippingMethodModels){
        ShippingMethodModel.checkNull(shippingMethodModels);
        shippingMethodList.clear();
        shippingMethodList.addAll(shippingMethodModels);
        if(shippingMethodList.size() != 0){

            CreateOrderActivity createOrderActivity = (CreateOrderActivity) BaseActivity.currentActivity;

            if(createOrderActivity.getSelectedShippingMethod() == null){
                shippingMethodList.get(0).setSelected(true);
                selectedShippingMethodModel = shippingMethodList.get(0);
            }
            else {
                for(ShippingMethodModel shippingMethodModel : shippingMethodModels){
                    if(shippingMethodModel.getCode().equals(createOrderActivity.getSelectedShippingMethod().getCode())){
                        shippingMethodModel.setSelected(true);
                        selectedShippingMethodModel = shippingMethodModel;
                        break;
                    }
                }
            }

        }
        shippingMethodsRecyclerAdapter.notifyDataSetChanged();
    }

    private void initRecyclerView(){
        shippingMethodsRecyclerAdapter = new ShippingMethodsRecyclerAdapter(shippingMethodList, new RecyclerItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                for(ShippingMethodModel shippingMethodModel : shippingMethodList){
                    shippingMethodModel.setSelected(false);
                }
                shippingMethodList.get(position).setSelected(true);
                selectedShippingMethodModel = shippingMethodList.get(position);
                shippingMethodsRecyclerAdapter.notifyDataSetChanged();
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(BaseActivity.currentActivity);
        shippingMethodsRecyclerView.setLayoutManager(layoutManager);
        shippingMethodsRecyclerView.setAdapter(shippingMethodsRecyclerAdapter);
    }
}