package ch.pizzalemon.android.fragment.createOrder;

import android.os.Bundle;
import androidx.appcompat.widget.AppCompatRadioButton;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.ArrayList;

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.api.ApiConstants;
import ch.pizzalemon.android.helper.SharedPrefsHelper;
import ch.pizzalemon.android.model.cart.CartProductModel;
import ch.pizzalemon.android.view.AppEditText;

/**
 * Created by cimenmus on 28.10.2017.
 */

public class CreateOrderNoteFragment extends CreateOrderBaseFragment {

    @BindView(R.id.orderNotePizzalinkEditText)
    AppEditText orderNoteAppEditText;
    @BindView(R.id.slicePizzaLayout) LinearLayout slicePizzaLayout;
    @BindView(R.id.yesRadioButton) AppCompatRadioButton yesRadioButton;
    @BindView(R.id.noRadioButton) AppCompatRadioButton noRadioButton;

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

    public CreateOrderNoteFragment() {}

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

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

    @OnClick({R.id.yesLayout, R.id.noLayout,
            R.id.previousTextView, R.id.nextTextView})
    protected void onClick(View view){
        switch (view.getId()){
            case R.id.yesLayout:
                yesRadioButton.setChecked(true);
                noRadioButton.setChecked(false);
                break;
            case R.id.noLayout:
                noRadioButton.setChecked(true);
                yesRadioButton.setChecked(false);
                break;
            case R.id.previousTextView:
                createOrderActivity.onPreviousClicked(FRAGMENT_NAME);
                break;
            case R.id.nextTextView:
                /*
                if(isCartContainsAnyPizza(createOrderActivity.getCartInfo().getProducts())){
                    if(yesRadioButton.isChecked()){
                        createOrderActivity.setSlicePizza(true);
                    }
                    else {
                        createOrderActivity.setSlicePizza(false);
                    }
                }
                */
                createOrderActivity.setOrderNote(orderNoteAppEditText.getText());
                createOrderActivity.onNextClicked(FRAGMENT_NAME);
                break;
        }
    }

    private void initViews(){
        createOrderActivity = (CreateOrderActivity) BaseActivity.currentActivity;
        /*
        if(isCartContainsAnyPizza(createOrderActivity.getCartInfo().getProducts())){
            slicePizzaLayout.setVisibility(View.VISIBLE);
            if(createOrderActivity.getSlicePizza() != null){
                if(createOrderActivity.getSlicePizza()){
                    yesRadioButton.setChecked(true);
                    noRadioButton.setChecked(false);
                }
                else {
                    noRadioButton.setChecked(true);
                    yesRadioButton.setChecked(false);
                }
            }
        }
         */
        int hintResId = R.string.order_note_title_for_free_shipping;
        if(createOrderActivity.getSelectedShippingMethod().getCode().equals(ApiConstants.SHIPPING_METHOD_CODE_PICK_UP_FROM_STORE)){
            hintResId = R.string.order_note_title_for_self_pickup;
        }
        orderNoteAppEditText.setHint(hintResId);

        if(createOrderActivity.getOrderNote() != null){
            orderNoteAppEditText.setText(createOrderActivity.getOrderNote());
        }
    }

    private boolean isCartContainsAnyPizza(ArrayList<CartProductModel> cartProductList){
        boolean containsAnyPizza = false;
        outerloop:
        for(CartProductModel cartProductModel : cartProductList){
            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;
    }
}
/*

StringBuilder stringBuilder = new StringBuilder();
                if(SharedPrefsHelper.isCartContainsAnyPizza()){
                    stringBuilder.append(slicePizzaText).append(" : ");
                    if(yesRadioButton.isChecked()){
                        stringBuilder.append(yesText);
                    }
                    else {
                        stringBuilder.append(noText);
                    }
                    stringBuilder.append("<br/>");
                }
                stringBuilder.append(orderNotePizzalinkEditText.getText());
                createOrderActivity.setOrderNote(stringBuilder.toString());
 */